ISTQB Certified Software Test Engineer

Thursday, March 29, 2012

Running a Selenium Script Parallelly using Web Driver

If we want to run a Selenium Script across multiple machines parallelly we are using Selenium Grid.
Using Selenium web driver we can achieve this parallel execution with the help of Remote Web driver class and starting Standalone jar on the remote machine(remote machine may be your local some times).
But with this approach you can run the script in multiple browsers in the same
machine.
The prerequisites to implement this using web driver are
1.Browsers to be installed in the machine
2.Standalone Selenium Server jar has to be started.
Here is the sample code


import static org.junit.Assert.assertTrue;

import java.net.MalformedURLException;
import java.net.URL;

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.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class GoogleTest {

public static WebDriver driver;
@BeforeClass
public static void main(String args[]) throws Exception{

DesiredCapabilities browsers[] ={DesiredCapabilities.firefox(),DesiredCapabilities.internetExplorer()};

for(DesiredCapabilities capabilities :browsers){

System.out.println("Executing Google Test in"+capabilities.getBrowserName());
driver =new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), capabilities);

driver.get("http://google.com");
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
assertTrue(driver.getTitle().contains("cheese!"));


}

}
}

4 comments:

  1. Hi Kumar,

    I read your blog, here you hav mentioned that if we want to un a Selenium Script across multiple machines parallelly
    we can use Selenium Grid.

    I am actually trying to acheieve similar things where I need to distribute my test suite(100 tcs) to multiple machine for the execution and i am looking for parallel execution.
    As mentioned I have started standalone jars on all of my virtual machines.

    Can you please clarify on what code changes will be needed to redirect it to different machines.
    I am using selenium wbdriver with junit.

    Your help is appreciated..

    ReplyDelete
  2. You have to make one machine has Hub,and others as nodes registering to the hub.Then you have to specify your test cases(Name of the test suite) in xml file

    ReplyDelete
  3. Hi,

    I want to do above things in perl. Please help in this?

    Thanks
    Nitin
    nitin_rajpal86@yahoo.com

    ReplyDelete
  4. does not works..m getting a exception "java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map" in new remote driver line..Please help.

    ReplyDelete