Selenium - Yash-777/SeleniumDriverAutomation GitHub Wiki
Selenium is one of the most popular automation testing frameworks to automate web-applications on javascript enabled browsers.
Selenium is designed in a way to test GUI[Elements position] and functional[entering text, on-click navigation, mouse over events… ] aspects of the web application based on Application's DOM.
It features are
- Cross Browser compatibility ( Firefox, Chrome, Internet Explorer, Safari, etc…)
- platform compatibility (Windows, Mac OS, Linux etc.)
Due to its existence in the open source community, it has become one of the most accepted tools amongst the testing professionals.
- it's free and open source.
- have a large user base and helping communities.
Selenium is not just a single tool or a utility, rather a package of several testing tools and for the same reason, it is referred to as a Suite. Each of these tools is designed to cater different testing and test environment requirements.
- supports distributed testing.
The suite package constitutes the following sets of tools:
Selenium Integrated Development Environment (IDE) | Selenium Remote Control (RC) | Selenium WebDriver latest tool | Selenium Grid |
---|---|---|---|
It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. | Selenium RC is a server that allows the user to create test scripts in the desired programming language. | WebDriver is a different tool altogether that has various advantages over Selenium RC. WebDriver directly communicates with the web browser and uses its native compatibility to automate. | Selenium Grid is used to distribute your test execution on multiple platforms and environments concurrently. |
supports only Firefox | It allows executing test scripts with a varied range of versions of Mozilla Firefox, Google Chrome, Internet Explorer and Opera. | Same as RC | Same as RC |
Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run in. | It doesn't support record and playback feature | Same as RC | Same as RC |
WebDriver doesn't require any server start before start execution. You can see that WebDriver
acts just as a normal Java library does: it's entirely self-contained, and you don't need to remember to start any additional processes or run any installers before using it.
WebDriver driver = new FirefoxDriver();
Running Standalone Selenium Server: stack-overflow POST
Command Line: Form Jar Source Main-Class is org.openqa.grid.selenium.GridLauncher
public static void main(String[] args) throws Exception {
CommandLineOptionHelper helper = new CommandLineOptionHelper(args);
GridRole role = GridRole.find(args);
// If -role option is omitted the server runs standalone
...
}
As per Grid Role
class - Selenium server can run in one of the following roles:
- NOT_GRID [If -role option is omitted the server runs
standalone
not being a part of a grid]- HUB [as a hub of a Selenium grid]
- NODE [as a node of a Selenium grid]
NOT GRID [standalone server]
D:\SeleniumJar_Source>java -jar selenium-server-standalone-2.53.0.jar -help
-------------------------------
Running as a standalone server:
-------------------------------
Usage: java -jar selenium-server.jar [-interactive] [options]
-port <nnnn>: the port number the selenium server should use (default 4444)
-log <logFileName>: writes lots of debug information out to a logfile and disables logging to console
D:\SeleniumJar_Source>java -jar selenium-server-standalone-2.53.0.jar -port 4454 -log D:\SeleniumDriverAutomation\Drivers\log\standalone_NONGRID.log
org.openqa.selenium.server.SeleniumServer - RemoteWebDriver instances should connect to: http://127.0.0.1:4454/wd/hub
Writing debug logs to D:\SeleniumDriverAutomation\Drivers\log\standalone_NONGRID.log
NOTE: Place the browser related drivers at the location where the selenium-server-standalone-*.jar
is available.
-
RC
(Selenium interface)
- IP:Port/selenium-server
:
Selenium Remote Control ( RC ) requires server to be started before executing the test scripts. RC server injects Selenium core (JavaScript) into the browser. The core (daemon process) receives commands from a server by your test scripts and executes them in a browser then results to your program. But cannot handle moving mouse cursors & mobile execution.
Java Client Driver:
Client-Configures your browser to use the Selenium Server as an HTTP proxy and passes browser commands to server through "/selenium-server/RemoteRunner.html?sessionId=1234"
, clients may send Driver Command requests to selenium server over HTTP as follows:
URL like : chrome://src/content/RemoteRunner.html?sessionId=197059bf9da94d5fada1af1bd04f3572&multiWindow=true&baseUrl=https%3A%2F%2Fwww.google.co.in%2F&debugMode=false&driverUrl=http://localhost:4444/selenium-server/driver/
Code & Requests: RC Server continuously runs. we create a daemon and from that communicate with the browser.
public class SeleniumRC_SeleniumInterface {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException, IOException {
com.thoughtworks.selenium.Selenium daemon =
new DefaultSelenium("127.0.0.1", 4444, "*firefox", "https://www.google.co.in/");
daemon.start();
daemon.windowMaximize();
daemon.open("/");
daemon.type("q", "selenium rc");
daemon.click("btnG");
Thread.sleep( 1000 * 7 );
daemon.close();
daemon.stop();
}
}
Driver Requesting URL like : GET:http://localhost:4454/selenium-server/driver/?cmd=open&1=https://www.google.co.in/&sessionId=197059bf9da94d5fada1af1bd04f3572
-
RC
(WebDriver
interface)
- IP:Port/wd/hub/static/resource/hub.html
:
Start Selenium Server with Java Code
:
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import java.net.BindException;
import java.net.ServerSocket;
public class SocketServer {
static String driverEXEPath = "D:\\SeleniumDriverAutomation/Drivers/Chrome/2.24/chromedriver.exe";
static String driverEXEPathIE = "D:\\SeleniumDriverAutomation/Drivers/IExplore/32/IEDriverServer.exe";
public static SeleniumServer server;
public static void main(String[] args) throws Exception {
startNonGrid_Server();
}
public static void startNonGrid_Server() throws Exception {
try {
RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setPort( 4454 );
ServerSocket serverSocket = new ServerSocket( rcc.getPort() );
if ( !serverSocket.isClosed() ) {
System.out.println("PORT is in use..., So closing the stream on this port.");
serverSocket.close();
}
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, driverEXEPath);
System.setProperty(InternetExplorerDriverService.IE_DRIVER_EXE_PROPERTY, driverEXEPathIE);
server = new SeleniumServer(false, rcc);
server.start();
System.out.println("Static HUB HTML URL : http://127.0.0.1:4444/wd/hub/static/resource/hub.html");
System.out.println("Please Enter to stop service.");
System.in.read();
System.in.read();
server.stop();
} catch (BindException e) {
System.out.println("Selenium server already up, will reuse...");
} catch(IOException e) {
e.printStackTrace();
} catch (Exception e) {
System.err.println("Could not create Selenium Server because of: " + e.getMessage());
e.printStackTrace();
}
}
}
NOTE: Place the browser related drivers in Class Path.
HTTP Method | Path | Summary |
---|---|---|
POST | /session | Create a new session. |
POST | /session/:sessionId/url | Navigate to a new URL. |
Requests:
http://127.0.0.1:4454/wd/hub/session/:sessionId
/url
{ "url" : "http://demo.opencart.com/index.php?route=account/login" }
HTTP Method | Path | Summary |
---|---|---|
GET | /session/:sessionId/screenshot | Take a screenshot of the current page. |
POST | /session/:sessionId/execute | Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. |
Requests:
http://127.0.0.1:4454/wd/hub/session/:sessionId
/execute
{ "script" : "document.querySelectorAll( arguments[0] )[0].click();", "args" : ['input.btn'] }
(OR)
{ "script" : "document.querySelectorAll('input.btn')[0].click();", "args" : [] }
Selenium Grid can be used to execute same or different test scripts on multiple platforms and browsers concurrently so as to achieve distributed test execution, testing under different environments and saving execution time remarkably. [ - IP:Port/grid/console
]
Configure the HUB « Grid HUB Running as a standalone server:
Hub is a central point which distributing tests on several node-machines, which are registered to it for parallel execution.
D:\SeleniumJar_Source>java -jar selenium-server-standalone-2.53.0.jar -role hub -help
-------------------------------
Running as a grid hub:
-------------------------------
Usage: java -jar selenium-server.jar -role hub [options]
-host: <IP | hostname> : usually not needed and determined automatically. For exotic network configuration, network with VPN, specifying the host might be necessary.
-port: <xxxx> : the port the remote/hub will listen on. Default to 4444.
D:\SeleniumJar_Source>java -jar selenium-server-standalone-2.53.0.jar -host 127.0.0.1 -port 4444 -role hub
-nodeTimeout 6000 -timeout=20 -browserTimeout=60 -log "D:\\HUB_Log.txt"
NODE | RC registration by Command Line & System properties to HUB.
Registration URL « //IP:Port/grid/register
D:\SeleniumJar_Source>java -jar selenium-server-standalone-2.53.0.jar -role node -help
(OR)
D:\SeleniumJar_Source>java -jar selenium-server-standalone-2.53.0.jar -role RC -help
-------------------------------
Running as a grid node:
-------------------------------
Usage: java -jar selenium-server.jar -role node [options]
java -jar selenium-server-standalone-2.53.0.jar -host 127.0.0.1 -port 4456 -role node
-hub http://localhost:4454/grid/register -hubHost 127.0.0.1 -hubPort 4454
-browser "browserName=firefox, version=39.0, firefox_binary=C:\Program Files (x86)\Mozilla Firefox\firefox.exe, maxinstance=2, platform=WINDOWS"
-browser "browserName=chrome, version=54.0, platform=WINDOWS"
-browser "browserName=internet explorer, version=11, platform=WINDOWS"
-Dwebdriver.ie.driver="D:\\Drivers\\IEDrivers\\iedriver64\\IEDriverServer.exe"
-Dwebdriver.chrome.driver="D:\\Drivers\\chromedrivers\\2.24\\chromedriver.exe"
-Dwebdriver.firefox.logfile="D:\\FirefoxInstance.txt"
-Dwebdriver.chrome.logfile="D:\\ChromeInstance.txt"
-Dwebdriver.ie.logfile="D:\\IEInstance.txt"
browser Instance INFO:
browserName=chrome, version=53.0, maxinstance=1, platform=WINDOWS Command Line parameters with System Property values -Dproperty=value -Dchrome=driverAddress (If value is a string that contains spaces, you must enclose the string in double quotes)
Node registration with a JSON configuration file.
D:\SeleniumJar_Source>java -jar selenium-server-standalone-2.53.0.jar -role node
-hub http://127.0.0.1:4454/grid/register
-nodeConfig registerNode.json
-Dwebdriver.chrome.driver="D:\\Drivers\\chromedrivers\\2.24\\chromedriver.exe"
-Dwebdriver.ie.driver="D:\\Drivers\\IEDrivers\\iedriver64\\IEDriverServer.exe"
-Dwebdriver.firefox.logfile="D:\\FirefoxInstance.txt"
-Dwebdriver.chrome.logfile="D:\\ChromeInstance.txt"
-Dwebdriver.ie.logfile="D:\\IEInstance.txt"
JSON FILE: [registerNode.json]
{
"capabilities":
[
{
"browserName": "firefox",
"version": "39.0",
"firefox_binary": "C:\Program Files (x86)\Mozilla Firefox\firefox.exe",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "chrome",
"version": "54.0",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "internet explorer",
"version": "11",
"maxInstances": 1,
"seleniumProtocol": "WebDriver"
}
],
"configuration":
{
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": 5555,
"host": "127.0.0.1",
"register": true,
"registerCycle": 5000,
"hubPort": 4454,
"hubHost": "127.0.0.1"
}
}
public class SeleniumRC_GRID {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
URL remoteHUBAddress = new URL("http://127.0.0.1:4454/wd/hub");
DesiredCapabilities registerdNODEChrome = DesiredCapabilities.chrome();
registerdNODEChrome.setVersion("54.0");
registerdNODEChrome.setPlatform(Platform.WINDOWS);
WebDriver driver = new RemoteWebDriver(remoteHUBAddress, registerdNODEChrome);
driver.get("https://www.google.com");
Thread.sleep( 1000 * 10 );
driver.close();
driver.quit();
}
}
@see