2
votes

I've got two nodes with same configurations(Win7, ie9). I set testNG to execute two tests one each node at the same time. Selenium Grid does open two browsers(one each node) at the same time, but the tests seem to be executed in just one browser -- the username is typed in in the same browser twice. And if I set thread-count=1, both tests succeed.

The testNG test suite xml file:

<suite name="ExampleTest" parallel="tests" thread-count="2"> 
    <test name="ExampleTest1">
        <classes>
            <class name="com.mycompany.testsuites.GridTest">
                <methods>
                    <include name="test1"></include>
                </methods>
            </class>
        </classes>
    </test>
    <test name="ExampleTest2">
        <classes>
            <class name="com.mycompany.testsuites.GridTest">
                <methods>
                    <include name="test2"></include>
                </methods>
            </class>
        </classes>
    </test>

The test class GridTest :

public class GridTest {

    private WebDriver driver;
    String hubUrl = "http://12.11.14.15:4444/wd/hub";

    @BeforeClass
    public void beforeClass() throws MalformedURLException {
        DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
        driver = new RemoteWebDriver(new URL(hubUrl), capability);
        Page.setUp(driver);  //Page is the base class of all page objects.
                             //The static setUp method assign a WebDriver object 
                             //to static Page.driver, which shared by all pages.
    }

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

    @Test
    public void test1() {
        driver.get("http://12.11.15.16");
        LoginPage login = new LoginPage();
        login.setUp();                  // set up the Login Page
        login.txtUsername().clear();    //clear the username input box
        login.txtUsername().sendKeys("username");    // input user name
        // ...
    }

    @Test
    public void test2() {
        driver.get("http://12.11.15.16");
        LoginPage login = new LoginPage();
        login.setUp();
        login.txtUsername().clear();
        login.txtUsername().sendKeys("username");
        // ...
    }
}

I use selenium-server-standalone-2.41.0.jar for the hub and nodes.

3

3 Answers

7
votes

The behaviour you are seeing is correct.

The driver is created in @beforeClass and this is only executed once, therefore only one browser is created which is used by both tests concurrently.

If you were to change to @beforeMethod, then a browser would be created per test BUT this would still fail as the same variable (driver) is being used to store the driver instance. You would see two browsers open but both tests executing in the second.

To run parallel at a test level then you need to ensure each test is entirely encapsulated such as creating its own driver within its own context, and this would not allow you use set up and tear down.

Usually I would always run parallel at class level, as this approach allows you to still use set up and tear down to create the driver instance. I would recommend changing your XML to run parallel=classes. Obviously you have to have multiple classes but I assume in reality you have more then two tests to run!

0
votes

I would put test1() in one class and put test2() in a separate class instead of trying to parallelize by method. Then, TestNG will have no problem forking it. Just look at how I did it here.

If you are wanting to fork by method, it can be done but it is much trickier to configure in TestNG. I personally have never got it work. When I try parallel by method, it doesn't seem to do anything for me... it acts the same as parallel by classes. I know that it is just a matter of configuration though.

In fact, I was recently experiementing in this area in this project but I remain unsatisfied as to how it ultimately works.

Of course, you probably already know this, you need to make sure you have configured your Grid Node to support 2 or more threads, otherwise it will be constrained to the default value ( 1?) even though you are explicitly forking.

0
votes

I had the same problem because a static WebDriver driver. After removed the static call the parallel="tests" worked fine;

Try to remove your private call and execute the same configuration.