Selenium

Selenium Standalone Server Example

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 on 4444 port by default.

C:\Tools>java -jar selenium-server-standalone-2.48.2.jar -role hub
10:29:14.270 INFO - Launching Selenium Grid hub
2015-11-19 10:29:15.458:INFO::main: Logging initialized @1362ms
10:29:15.479 INFO - Will listen on 4444
10:29:15.563 INFO - Will listen on 4444
2015-11-19 10:29:15.568:INFO:osjs.Server:main: jetty-9.2.z-SNAPSHOT
2015-11-19 10:29:15.631:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletC
ontextHandler@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 @1675ms
10:29:15.772 INFO - Nodes should register to http://192.168.0.102:4444/grid/regi
ster/
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/register
10:31:08.635 INFO - Launching a Selenium Grid node
10:31:09.999 INFO - Java: Oracle Corporation 25.45-b02
10:31:10.000 INFO - OS: Windows 7 6.1 x86
10:31:10.009 INFO - v2.48.0, with Core v2.48.0. Built from revision 41bccdd
10:31:10.089 INFO - Driver class not found: com.opera.core.systems.OperaDriver
10:31:10.090 INFO - Driver provider com.opera.core.systems.OperaDriver is not re
gistered
10:31:10.153 INFO - Selenium Grid node is up and ready to register to the hub
10:31:10.215 INFO - Starting auto registration thread. Will try to register ever
y 5000 ms.
10:31:10.216 INFO - Registering the node to the hub: http://localhost:4444/grid/
register
10: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 -nodeConfig
firefox_node.json
11:36:22.804 INFO - Launching a Selenium Grid node
11:36:23.789 INFO - Java: Oracle Corporation 25.45-b02
11:36:23.789 INFO - OS: Windows 7 6.1 x86
11:36:23.798 INFO - v2.48.0, with Core v2.48.0. Built from revision 41bccdd
11:36:23.884 INFO - Driver class not found: com.opera.core.systems.OperaDriver
11:36:23.885 INFO - Driver provider com.opera.core.systems.OperaDriver is not re
gistered
11:36:23.973 INFO - Selenium Grid node is up and ready to register to the hub
11:36:24.028 INFO - Starting auto registration thread. Will try to register ever
y 5000 ms.
11:36:24.029 INFO - Registering the node to the hub: http://127.0.0.1:4444/grid/
register
11: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.

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 {

	@Test
	public void executeFirefoxDriver() throws MalformedURLException {
		this.execute(DesiredCapabilities.firefox());
	}

	@Test
	public 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=firefo
x, version=, platform=ANY}]
12:14:30.143 INFO - Done: [new session: Capabilities [{browserName=firefox, vers
ion=, 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 (2ca
50141-8460-4012-bec4-b291e4042f55)] -> name: s], [selenuim]]
12:14:34.784 INFO - Executing: [submit: 0 [[FirefoxDriver: firefox on WINDOWS (2
ca50141-8460-4012-bec4-b291e4042f55)] -> name: s]])
12:14:39.270 INFO - Done: [submit: 0 [[FirefoxDriver: firefox on WINDOWS (2ca501
41-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-4ff4eecb
f03f])
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

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Alisha
6 years ago

Selenium testing is one of the best framework for testing the web applications. You can easily automate your test to find the bugs at early stage

QAwerk
6 years ago

Thank you for this very example. Selenium automates browsers. That’s it! What you do with that power is entirely up to you. I love it and I like that Grid is still in use, and works with both WebDriver and RC. I’m firm on that Selenium is the present and the future of automated browser management. If you want to be considered a professional in the field of automated testing of web applications – it is essential for you to own this tool.

W3Softech
5 years ago

Needed to compose one simple thing yet thanks for the suggestions that you are contributed here. Would like to read this blog regularly to get more updates regarding selenium

Stigntech
4 years ago

Thank you for the valuable information.

Back to top button