I had earlier covered in my blog on how to execute your RC cases in selenium grid. But with the commencement of Selenium-2/Webdriver, grid setup has been changed. In the following blog I will cover how to set-up your grid and the changes that will be required for easy execution of cases.
Following things are required:
1. Selenium 2.xx version server jar and Java library. The latest one can be downloaded from the link: Selenium-2
2. Java 1.6 and above
3. TestNg jar . You can download it from the link: TestNG
4. Eclipse with TestNG plugin installed(optional)
5. ChromeDriver. Can be downloaded from: ChromeDriver
The Test Code
Following is an example of test-class that have a test case to search google for "testing" and verifying after clicking it on a link.
For Hub:
For a firefox based node:
For google-chrome based node:
You can do configurations for environment like OS (Linux,Windows), browser, browser-version and all and then you can run you cases on particular type of environment by configuring them accordingly. More details can be found at the following URL:
http://code.google.com/p/selenium/wiki/Grid2
Following things are required:
1. Selenium 2.xx version server jar and Java library. The latest one can be downloaded from the link: Selenium-2
2. Java 1.6 and above
3. TestNg jar . You can download it from the link: TestNG
4. Eclipse with TestNG plugin installed(optional)
5. ChromeDriver. Can be downloaded from: ChromeDriver
The Test Code
Following is an example of test-class that have a test case to search google for "testing" and verifying after clicking it on a link.
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.Selenium;
import junit.framework.Assert;
public class Google {
private Selenium selenium;
@Parameters({"browser","port"})
@BeforeClass
public void beforeClass(String browser,String port){
DesiredCapabilities capability= new DesiredCapabilities();
capability.setBrowserName(browser);
try {
WebDriver driver= new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capability);
selenium = new WebDriverBackedSelenium(driver, "http://www.google.com");
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void search() {
selenium.open("/");
selenium.type("id=lst-ib", "testing");
selenium.click("//input[@value='Google Search']");
for (int second = 0;; second++) {
if (second >= 60)
Assert.fail("timeout");
try {
if (selenium
.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia"))
break;
} catch (Exception e) {
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
selenium.click("link=Software testing - Wikipedia, the free encyclopedia");
for (int second = 0;; second++) {
if (second >= 60)
Assert.fail("timeout");
try {
if (selenium.isTextPresent("Software testing"))
break;
} catch (Exception e) {
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@AfterClass
public void afterClass(){
selenium.stop();
}
}
In the above class I am using the TestNG "Parameter" property to provide different data set to the "BeforeClass" method "beforeClass". The beforeClass method accepts two properties "browser" and a "port".
These values are used for initialization of driver and in-turn for initialization of selenium object. In the above code I am using the "WebDriverBackedSelenium" class for creation of the selenium object, so that its easy for guys who had worked on Selenium-1 to understand the code. If you want the code to be purely WebDriver, you can directly use the "driver" object for defining your test-cases.
The main part in this test case is how the driver object is being created:DesiredCapabilities capability= new DesiredCapabilities();
capability.setBrowserName(browser);
WebDriver driver= new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capability);
The above code creates an object of DesiredCapability and then set the browser value to it. Now using this "capability" object I am creating the webdriver object using the "RemoteWebDriver" class. This tells the selenium on which browser the test-case needs to run and where the server is located. In this example I am assuming the server to be running locally. In case it is on different system the "localhost" needs to be re-placed with the ip of the said system.
TestNG configuration
For parallel execution you need to use the TestNG configuration. Following is an "testng.xml" file for the above said test class. The said configuration executes the test-cases across different browser.
<suite name="Selenium TestNG Suite" parallel="tests"
thread-count="5">
<test name="Selenium TestNG - 1">
<parameter name="browser" value="firefox" />
<parameter name="port" value="4444" />
<classes>
<class name="com.test.testng.Google" />
</classes>
</test>
<test name="Selenium TestNG - 2">
<parameter name="browser" value="chrome" />
<parameter name="port" value="4444" />
<classes>
<class name="com.test.testng.Google" />
</classes>
</test>
</suite>
In the above configuration, I am configuring TestNG to run "tests" in parallel. Also there are two different tests inside a suite. For each test a different "browser" parameter value has been configured.
Selenium-Grid server
Now start your grid using the following commands. Run each command in a seperate terminal or command prompt by going to the directory containing your selenium-server-standalone-2.x.x.jar. In the following example I am using the 2.7.0 version of selenium.
For Hub:
java -jar selenium-server-standalone-2.7.0.jar -role hub
For a firefox based node:
java -jar selenium-server-standalone-2.7.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=firefox
For google-chrome based node:
java -Dwebdriver.chrome.driver=/path/to/chromedriver -jar selenium-server-standalone-2.7.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=chrome
Before running the above command you need to provide the chrome-driver path to the property "-Dwebdriver.chrome.driver".
Now run your testng.xml from eclipse by selecting it -> Right click -> Run as -> TestNG suite(this will work only if you have TestNG plugin installed in your eclipse.)
Or you can choose other ways to execute "testng.xml" like from command prompt, using ant or maven.
Once you run the above testng.xml. TestNG will execute the cases from the Google class on grid across different browsers firefox and google-chrome as configured.
You can do configurations for environment like OS (Linux,Windows), browser, browser-version and all and then you can run you cases on particular type of environment by configuring them accordingly. More details can be found at the following URL:
http://code.google.com/p/selenium/wiki/Grid2