EdXX
EdXX

Reputation: 890

Run selenium tests on different browsers via TestNG

Can anyone help me because I really don't get it:

That is my TestNG file:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="GRID SAMPLE TEST" parallel="tests" verbose="3" thread-count="2">
    <test name="GRID EXECUTION WITH  CHROME" parallel="classes" verbose="3" thread-count="2">
    <parameter name ="browserType" value="chrome"/>
        <classes>
            <class name ="Tests"/>
            <class name ="Tests2"/>
        </classes>
    </test>
    <test name="GRID EXECUTION WITH  FF" parallel="classes" verbose="3" thread-count="2">
    <parameter name ="browserType" value="firefox"/>
        <classes>
            <class name ="Tests"/>
            <class name ="Tests2"/>
        </classes>
    </test>
</suite>

And that is my setUp method:

RegistrationPage rp;

    @BeforeClass
    @Parameters("browserType")
    public void setUp(String browserType) throws InterruptedException  {
        rp = new RegistrationPage();

        if (browserType.equalsIgnoreCase("firefox")) {
            Configuration.browser = "firefox";
        } else if (browserType.equalsIgnoreCase("chrome")){
            Configuration.browser = "chrome";
        }
        Configuration.baseUrl = "http://demoqa.com/registration/";

    }

I'm using Selenide which is a wrapper on Selenium but that's not the point. The point is that I want the same tests to be run on chrome (2 here) and firefox (to here). When I run them on debug then everything is okay and 2 tests are done on FF and another 2 on CHROME. But when I run them without debug just normally (right click on xml and run as testNG) then all my tests are done either on FF only (4) or on CHROME only (4)!

Can anyone please explain why? Did I do something wrong here?

Upvotes: 0

Views: 455

Answers (2)

Edvins
Edvins

Reputation: 426

The reason it currently fails is - you try to run the tests in parallel when using the XML (as opposed to Debug mode, which, I assume, runs in a single thread).

Both tests (test elements from TestNG XML) are executed almost simultaneously, both setting Configuration.browser field, which is defined as static, meaning - only one instance of it exists and gets overwritten by whichever thread gets executed later.

This can be fixed by either executing the tests sequentially or by manually creating a separate WebDriver instance (according to parameter browserType) for each thread, and setting it using WebDriverRunner.setWebDriver(), instead of Configuration.browser.

For example:

@BeforeTest
@Parameters("browserType")
public void setUp(String browserType) throws InterruptedException  {
    rp = new RegistrationPage();

    Configuration.baseUrl = "http://demoqa.com/registration/";

    if (browserType.equalsIgnoreCase("firefox")) {
        WebDriverRunner.setWebDriver(new FirefoxDriver());
    } else if (browserType.equalsIgnoreCase("chrome")){
        WebDriverRunner.setWebDriver(new ChromeDriver());
    }

}

Hope this helps.

Upvotes: 1

dangi13
dangi13

Reputation: 1275

use :

@BeforeTest

annotation and then it will pick up different browser as you have mentioned in your <test> tag in testNG.xml

Upvotes: 0

Related Questions