Selenium

Selenium Standalone Server Example

Photo of Petr ArsentevPetr ArsentevNovember 23rd, 2015Last Updated: April 9th, 2019
4 463 5 minutes read

1. Introduction

With this example we are going to demonstrate how to use and configure Selenium standalone servers (Selenium Grid). We are going to run the hub server and the two nodes. Each nodes will run the tests in different browsers.

Selenium Grid are the servers which compounds in distributed nodes. It offers you to run your selenium test on separate machines in different kinds environments. This is great opportunities, because your tests can run parallels and use different browsers for testing.
 
 

2. High level architecture

Below is shown high level architecture. It has the follow flow process. First we pass the tests to hub which send this tests to specific nodes where all tests will be executed.

Selenium Grid. High level
Selenium Grid. High level architecture

So all tests and nodes can be located on different machines. Such architecture can be scaled easily.

3. Configuration

Before we can start to configure the Selenium Grid we should download the necessary library. Selenium Grid consists only from one JAR file. Go to the official site link and download the Selenium Server JAR file –selenium-server-standalone-2.48.2.jar. This jar has the good help information. We should run this jar with the key-h that to print help information on screen.

java -jar selenium-server-standalone-2.48.2.jar -h

This help information has all explanation about supported keys. This keys are used for configuration the instance server. First of all we should run this jar with key–role hub. It means the this instance will be the hub server. It will be taken all receiving tests and distributed to the specific nodes server. The hub server is run on4444 port by default.

C:\Tools>java -jar selenium-server-standalone-2.48.2.jar -role hub10:29:14.270 INFO - Launching Selenium Grid hub2015-11-19 10:29:15.458:INFO::main: Logging initialized @1362ms10:29:15.479 INFO - Will listen on 444410:29:15.563 INFO - Will listen on 44442015-11-19 10:29:15.568:INFO:osjs.Server:main: jetty-9.2.z-SNAPSHOT2015-11-19 10:29:15.631:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@13f88ab{/,null,AVAILABLE}2015-11-19 10:29:15.770:INFO:osjs.ServerConnector:main: Started ServerConnector@646db9{HTTP/1.1}{0.0.0.0:4444}2015-11-19 10:29:15.771:INFO:osjs.Server:main: Started @1675ms10:29:15.772 INFO - Nodes should register to http://192.168.0.102:4444/grid/register/10:29:15.772 INFO - Selenium Grid hub is up and running

The secondary step is to run the node instance. It can be done with key-role node as shown below. The same time we should point where is located our hub server by key-hub http://localhost:4444/grid/register

C:\Tools>java -jar selenium-server-standalone-2.48.2.jar -role node  -hub http://localhost:4444/grid/register10:31:08.635 INFO - Launching a Selenium Grid node10:31:09.999 INFO - Java: Oracle Corporation 25.45-b0210:31:10.000 INFO - OS: Windows 7 6.1 x8610:31:10.009 INFO - v2.48.0, with Core v2.48.0. Built from revision 41bccdd10:31:10.089 INFO - Driver class not found: com.opera.core.systems.OperaDriver10:31:10.090 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered10:31:10.153 INFO - Selenium Grid node is up and ready to register to the hub10:31:10.215 INFO - Starting auto registration thread. Will try to register every 5000 ms.10:31:10.216 INFO - Registering the node to the hub: http://localhost:4444/grid/register10:31:10.254 INFO - The node is registered to the hub and ready to use

How you can see above the configuration process can be done by adding the keys in command line. But Selenium Server supports another variant of configuration by file configuration in JSON format.

Firstly we should create the file which has the name –firefox_node.json. It can have any appropriate name.

{  "capabilities":      [        {          "browserName": "*firefox",          "maxInstances": 1,          "seleniumProtocol": "WebDriver"        }      ],  "configuration":  {    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",    "maxSession": 5,    "port": 6543,    "host": 127.0.0.1,    "register": true,    "registerCycle": 5000,    "hubPort": 4444,    "hubHost": 127.0.0.1  }}

We pointed there that all tests should be run in firefox. Now we can run the new node instance with this configurations. We use-nodeConfig that to point which config file to use.

C:\Tools>java -jar selenium-server-standalone-2.48.2.jar -role node -nodeConfigfirefox_node.json11:36:22.804 INFO - Launching a Selenium Grid node11:36:23.789 INFO - Java: Oracle Corporation 25.45-b0211:36:23.789 INFO - OS: Windows 7 6.1 x8611:36:23.798 INFO - v2.48.0, with Core v2.48.0. Built from revision 41bccdd11:36:23.884 INFO - Driver class not found: com.opera.core.systems.OperaDriver11:36:23.885 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered11:36:23.973 INFO - Selenium Grid node is up and ready to register to the hub11:36:24.028 INFO - Starting auto registration thread. Will try to register every 5000 ms.11:36:24.029 INFO - Registering the node to the hub: http://127.0.0.1:4444/grid/register11:36:24.041 INFO - The node is registered to the hub and ready to use

Sometimes you can need only one instance server. For this reason you should run the selenium server without keys.
All tests will be executed in this instance in this case.

Another good opportunities are configure special browsers. For example, below we set the chrome browser environment.

Single Selenium Instance. Chrome Config.
Single Selenium Instance. Chrome Config.

We set the properties :-Dwebdriver.chrome.driver="c:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

Right now we have the three instances: one the hub and two nodes and one single server. Let’s start to run our tests.

4. Run tests

First of all we should create the new maven project.

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>ru</groupId><artifactId>parsentev</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>2.48.2</version></dependency></dependencies></project>

That we need to add the simple tests.

Want to master Selenium?
Subscribe to our newsletter and download theSeleniumProgramming Cookbookright now!
In order to get you prepared for your Selenium development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

ru\parsentev\SeleniumStantaloneServerTest.java

package ru.parsentev;import com.thoughtworks.selenium.DefaultSelenium;import com.thoughtworks.selenium.Selenium;import org.junit.Test;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.remote.RemoteWebDriver;import java.net.MalformedURLException;import java.net.URL;import static org.hamcrest.core.Is.is;import static org.junit.Assert.assertThat;/** * Tests for selenium standalone server. * @author parsentev * @since 19.11.2015 */public class SeleniumStandaloneServerTest {@Testpublic void executeFirefoxDriver() throws MalformedURLException {this.execute(DesiredCapabilities.firefox());}@Testpublic void executeChrome() throws MalformedURLException {this.execute(DesiredCapabilities.chrome());}private void execute(final DesiredCapabilities capability) throws MalformedURLException {WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);driver.get("http://www.javacodegeeks.com/");WebElement element = driver.findElement(By.name("s"));element.sendKeys("selenuim");element.submit();assertThat(driver.getTitle(),is("You searched for selenuim | Java Code Geeks"));driver.quit();}}

Now we can run the our test.

mvn clean test

We can see details information about tests process on the node logs or screens. You should see somethings similar:

12:14:25.891 INFO - Executing: [new session: Capabilities [{browserName=firefox, version=, platform=ANY}]])12:14:25.903 INFO - Creating a new session for Capabilities [{browserName=firefox, version=, platform=ANY}]12:14:30.143 INFO - Done: [new session: Capabilities [{browserName=firefox, version=, platform=ANY}]]12:14:30.196 INFO - Executing: [get: http://www.javacodegeeks.com/])12:14:34.283 INFO - Done: [get: http://www.javacodegeeks.com/]12:14:34.299 INFO - Executing: [find element: By.name: s])12:14:34.671 INFO - Done: [find element: By.name: s]12:14:34.689 INFO - Executing: [send keys: 0 [[FirefoxDriver: firefox on WINDOWS (2ca50141-8460-4012-bec4-b291e4042f55)] -> name: s], [selenuim]])12:14:34.774 INFO - Done: [send keys: 0 [[FirefoxDriver: firefox on WINDOWS (2ca50141-8460-4012-bec4-b291e4042f55)] -> name: s], [selenuim]]12:14:34.784 INFO - Executing: [submit: 0 [[FirefoxDriver: firefox on WINDOWS (2ca50141-8460-4012-bec4-b291e4042f55)] -> name: s]])12:14:39.270 INFO - Done: [submit: 0 [[FirefoxDriver: firefox on WINDOWS (2ca50141-8460-4012-bec4-b291e4042f55)] -> name: s]]12:14:39.281 INFO - Executing: [get title])12:14:39.311 INFO - Done: [get title]12:14:39.327 INFO - Executing: [delete session: a459baef-2980-4fcc-8093-4ff4eecbf03f])12:14:39.806 INFO - Done: [delete session: a459baef-2980-4fcc-8093-4ff4eecbf03f]

When the test is received by node it looks appropriate browser in local machine. Then the node opens this browser and starts to do the tests.

It can look like below:

Firefox selenium tests
Firefox selenium tests

Chrome selenium tests
Chrome selenium tests

5. Download the Code Project

Download
You can download the full source code of this example here:Selenium
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Petr ArsentevPetr ArsentevNovember 23rd, 2015Last Updated: April 9th, 2019
4 463 5 minutes read
Photo of Petr Arsentev

Petr Arsentev

Petr Arsentev has over 8 years of experience in java development. He participated in the development a few startup projects, which run successfully. He finished Moscow Power Engineering Institute (National Research University) at 2009. After he started to work in a local company as java developer and still keeps improving the knowledge about software developments. He focused on JVM languages like Java, Scala and related technologies and frameworks. He has developed the few courses about Java in Russian. He teaches students Java language too. This is his personal website http://parsentev.ru/
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.