Selenium WebDriver: Executing / Running UI Tests on a Remote Server


When do you need to run tests on remote servers -

    1. When your server doesn't have web browser and your tests need web browser to run successfully, in this case you will have to move to some other server which has web browser installed or run test remotely on the server where web browser is already installed.
    2. Or you want to run same test on different web browsers, which are installed on different remote servers having different Operating Systems in parallel.

There are different approaches to address above mentioned problems.

Either you can use RemoteWebDriver Server to solve first mentioned problem above (which will help you to run test on remote server) Or in both the cases you can use Selenium GRID.

Please find details below -

1. RemoteWebDriver Server

The RemoteWebDriver is composed of two pieces: a client and a server. The client is your WebDriver test and the server is simply a Java servlet, which can be hosted in any modern JEE app server. The server will always run on the machine with the browser you want to test. There are two ways to user the server: command line or configured in code.

a. Starting the Server from The Command Line
Once you have downloaded selenium-server-standalone-{VERSION}.jar place it on the computer with the browser you want to test. Then from the directory with the jar run the following

You can download 2.47.1 version directly or from here get the latest once (under Selenium Standalone Server section)

java -jar selenium-server-standalone-{VERSION}.jar

b. Once server is UP, you will see below message in your terminal window -

$ java -jar selenium-server-standalone-2.47.1.jar
23:04:53.282 INFO - Launching a standalone Selenium Server
23:04:53.619 INFO - Java: Oracle Corporation 25.60-b23
23:04:53.620 INFO - OS: Windows 7 6.1 amd64
23:04:53.628 INFO - v2.47.1, with Core v2.47.1. Built from revision 411b314
23:04:53.679 INFO - Driver class not found: com.opera.core.systems.OperaDriver
23:04:53.679 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
23:04:54.081 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
23:04:54.081 INFO - Selenium Server is up and running

c. Now try to connect to this server from your test, where you will be using RemoteWebDriver.
Please refer sample code below -


// We could use any driver here (I am using firefox driver)
DesiredCapabilities capabilities = DesiredCapabilities.firefox();

// ... but only if it supports javascript
// Make sure your browser supports javascript, otherwise you can't use RemoteWebDriver
capabilities.setJavascriptEnabled(true);

// Get a handle to the driver. This will throw an exception
// if a matching driver cannot be located
driver = new RemoteWebDriver(new URL("http://IP_ADDRESS_OF_REMOTE_SERVER:4444/wd/hub"), capabilities);

d. There will not be any change in your test code, all the statements in your test code should remain as it is.

e. Important Note while running Remote WebDriver server
The caller is expected to terminate each session properly, calling either Selenium#stop() or WebDriver#quit.
The selenium-server keeps in-memory logs for each ongoing session, which are cleared when Selenium#stop() or WebDriver#quit is called. If you forget to terminate these sessions your server may leak memory. If you keep extremely long-running sessions you will probably need to stop/quit every now and then (or increase memory with -Xmx jvm option)

// If using WebDriver then
driver.quit();

// If using Selenium RC then
selenium.stop();

2. Selenium Grid

As I mentioned earlier, grid can be used if you want to do cross browser testing (and want to save time by running single tests on multiple servers at the same time), and also want to make sure your application is working fine on different Operating Systems as well.

There is no change in Client side code except that now each client can specify capabilities and based on that GRIDs assign nodes to the client to run the test.

Steps involved:

a. Start Hub running below command (this will be any server were your hub will be running)

java -jar selenium-server-standalone-2.14.0.jar -role hub

Output:
$ java -jar selenium-server-standalone-2.47.1.jar -role hub
23:36:58.945 INFO - Launching Selenium Grid hub
2015-10-06 23:37:00.140:INFO:osjs.Server:jetty-7.x.y-SNAPSHOT
2015-10-06 23:37:00.164:INFO:osjsh.ContextHandler:started o.s.j.s.ServletContextHandler{/,null}
2015-10-06 23:37:00.172:INFO:osjs.AbstractConnector:Started SocketConnector@0.0.0.0:4444
23:37:00.173 INFO - Nodes should register to http://HUB_IP:4444/grid/register
23:37:00.173 INFO - Selenium Grid hub is up and running

b. Register/Start Nodes

java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://HUB_IP:4444/grid/register

Output :
$ java -jar selenium-server-standalone-2.47.1.jar -role node  -hub http://HUB_IP:4444/grid/register
23:30:33.509 INFO - Launching a Selenium Grid node
23:30:35.033 INFO - Java: Oracle Corporation 25.60-b23
23:30:35.034 INFO - OS: Windows 7 6.1 amd64
23:30:35.038 INFO - v2.47.1, with Core v2.47.1. Built from revision 411b314
23:30:35.068 INFO - Driver class not found: com.opera.core.systems.OperaDriver
23:30:35.068 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
23:30:35.094 INFO - Selenium Grid node is up and ready to register to the hub
23:30:35.113 INFO - Starting auto registration thread. Will try to register every 5000 ms.
23:30:35.114 INFO - Registering the node to the hub: http://HUB_IP:4444/grid/register
23:30:35.136 INFO - The node is registered to the hub and ready to use


HUB_IP = Server name / IP where Hub is running

c. Use Grid to run test from your client server (where you don't have any browser and you want to invoke test on remote server).

// We could use any driver for our tests...
DesiredCapabilities capabilities = DesiredCapabilities.firefox();

// ... but only if it supports javascript
capabilities.setJavascriptEnabled(true);

// Get a handle to the driver. This will throw an exception
// if a matching driver cannot be located
driver = new RemoteWebDriver(new URL("http://HUB_IP:4444/wd/hub"), capabilities);

If multiple nodes are registered to Hub then, based on client's connection inputs, HUB identifies on which nodes this test should run

For e.g. assume we have HUB running and two nodes are registered (one has Firefox/Linux and other node has Windows/Internet Explorer).
When one client invoke test with DesiredCapabilities = IE then HUB knows where this test should be running, and it assigns Node with IE browser to that client to run tests… and if DesiredCapabilities = Firefox then Hub assigns a Linux server for running test.

I hope this helps you!

Please comment if you have any question regarding the same. Thanks!
Related Posts Plugin for WordPress, Blogger...
Powered by Blogger