Thursday 6 June 2013

SeleniumGrid+TestNg Script Execution

 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.
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>



No comments:

Post a Comment