Selenium Grid With Example [Code Snippet]
Selenium-Grid allows us to run our tests on different machines against different browsers in parallel. Check out more in this article!
Join the DZone community and get the full member experience.
Join For FreeSelenium-Grid allows us to run our tests on different machines against different browsers in parallel. It means running multiple tests at the same time against different machines running different browsers and operating systems. In short Selenium Grid supports distributed test execution.
Two possible scenarios when we may need of selenium Grid to use which are as below:
- To run the tests against multiple browsers, multiple versions of browser, and browsers running on different operating systems.
- To reduce the time it takes for the test suite to complete a test pass.
Let us see how it works:
A grid consists of a single hub, and one or more nodes. Both are started using the selenium-server.jar executable. in this example I am using selenium-server-standalone-2.43.1.jar
Place the above jar in some directory and make sure java is installed at that machine and execute the below command from command line to start the hub:
java -jar selenium-server-standalone-2.43.1.jar -role hub
Node can be at same place or different machine as per your requirement, in this example I am placing at same machine and executing the below command as below:
java -jar selenium-server-standalone-2.43.1.jar -role node -hub http://localhost:4444/grid/register
If your node is at different machine then replace the 'localhost' with the hub IP address and default port is 4444 where hub actually listens.
Get the below jar to write your java test cases:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>2.25.0</version>
</dependency>
UISeleniumGridTest.java
import static junit.framework.Assert.assertEquals;
import java.net.URL;
import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
* This class is responsible to test the UserInterface using Selenium Grid concept
*/
public class UISeleniumGridTest {
private static final Logger log = Logger.getLogger(UISeleniumGrid.class);
private static Wait<WebDriver> wait;
private static DesiredCapabilities capabillities;
private static WebDriver driver;
@BeforeClass
public static void setUp() throws Exception {
capabillities = DesiredCapabilities.firefox();
/** URL is the selenium hub URL here **/
driver = new RemoteWebDriver(new URL("http://ip_of_the_machine_where_hub_running:port/wd/hub"), capabillities);
capabillities.setBrowserName("firefox");
wait = new WebDriverWait(driver, 6000);
}
/**
* To test the UI
* @throws Exception
*/
@Test
public void testUI() throws Exception {
/** Your application URL which you want to test **/
driver.get("http://localhost:8090/myapp/index.html");
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
log.info("Please be patience .... Searching ...");
return webDriver.findElement(By.tagName("title")) != null;
}
});
log.info(driver.findElement(By.tagName("body")).getText());
assertEquals("URL must be http://localhost:8090/myapp/index.html", "http://localhost:8090/myapp/index.html", driver.getCurrentUrl());
/** put other asserts as well **/
}
@AfterClass
public static void tearDown() throws Exception {
driver.quit();
}
}
You can place your java test cases on Linux machine (if required) and hub and node at windows, and it would be all good.
Hope it will help to understand the Selenium Grid.
Opinions expressed by DZone contributors are their own.
Comments