Tuesday, February 15, 2011

webdriver aka selenium-2 -Run tests in parallel.

Ever since I started exploring the selenium-2 aka webdriver one of the major question irritated me is the fact that webriver doesn't support to run the tests in parallel. But even then I stick with the most promising web automation tool. Seeing the active community and the active development effort of this magnificent team, I always had the strong believe that we will taste that feature in the near future itself.

Time has run away and selenium-2 matured from its initial alpha releases which span over 7 releases to the second beta release. Now you have the selenium-2b2 available for download.

Selenium has matured and it slowly but steady growing to support the tets to be run in parallel. Seeing some of the posts in blogs and some groups, I smell the blood and decided to give it shot. I wrote a test class using the testNG framework and decided to run that test in parallel mode. I couldn't believe why I didn't try this earlier! And the reason for my surprise is that the tests run so smoothly and I saw the browser windows poping up simultaneously and I saw the standard Google test running on them.


Here is what I did

I  wrote a test class using testNG annotations

import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class Parallel {
@BeforeClass
@Parameters ("browser"})
public void baseSetUp(String browser) {
driver = makeDriver(browser);
}


@Test
public void ffAndIe() throws InterruptedException {

driver.get("http://www.google.com");
Thread.sleep(2000);
driver.findElement(By.name("q")).sendKeys("Selenium");
driver.findElement(By.name("btnG")).click();
}

public WebDriver makeDriver(String browser) {
WebDriver driver = null;
if (browser.equals("ie")) {
driver = new InternetExplorerDriver();
} else if (browser.equals("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equals("chrome")) {
driver = new ChromeDriver();
}
return driver;
}

@AfterClass
public void tearDown() {
driver.quit();
}

}

And thats my testNG test class and Now I wrote a testNG suite.xml file inorder to run the tests.
And here its is



<suite name="DenZie's test suite" parallel="tests" thread-count="3" verbose="10">
<test name="Test on FF">
<parameter name="browser" value="firefox"></parameter>
<classes>
<class name="Parallel"></class>
</classes>
</test>
<test name="Test on IE">
<parameter name="browser" value="ie"></parameter>
<classes>
<class name="Parallel"></class>
</classes>
</test>
<test name="Test on FF- 2"></test>
<parameter name="browser" value="chrome"></parameter>
<classes>
<class name="Parallel"></class>
</classes>
</test>
</suite>


All you to do is go to eclipse and run the test against this suite.

And Enjoy the sight of all three browsers opps up load the google page and search for you what you most care about "selenium".

webdriver rocks :)



2 comments:

  1. Hi! I wrote a test usnig eclipse and openqa library. I simple create a class and wrote 2 function(2 tests) and call them in Main class.

    But those test are run in sequence. I would like to run them in parallel.

    I have found this page, with this simple example, but I have some questoins about it.

    I have also TestNG installed in Eclipse. So must I open new TestNG project, to put this code in? Because if I put this test in my Java project I have many errors.
    FIRST: @Parameters ("browser"}) // }
    SECOND: driver // Where are you define driver??

    If I use TestNG with Eclipse where I wrote a testNG suite.xml?? And in which folder must I put it in??

    Thanks for the answers.

    ReplyDelete
  2. Hi gglazar, did u found any solution to your problem of parallel running test methods in a class.?
    I tried to do it but got the following error.
    "
    java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated.
    Make sure to release the connection before allocating another one. "

    ReplyDelete