1

I seem to be having an issue when attempting to run tests in parallel using TestNG alongside Selenium Grid 2.

Although the right number of browsers are opened to match the amount of tests that I'm running , all instructions for all tests are being fired to the same browser window. For example, each test will open a page and attempt to log in. Four browser windows will open, but one browser window will navigate to the login page four times and then type the username in 4 times, whilst the rest of the browser windows remain inactive.

Here's how I'm starting grid:

java -jar selenium-server-standalone-28.0.jar -role hub
java -jar selenium-server-standalone-28.0.jar -webdriver.chrome.driver="*location*/chromedriver_mac" -role node 

This is how the suite xml is set up:

<suite name="testng" verbose="1" parallel="classes">
    <test name="chrome">
        <packages>
            <package name="login"/>
            <package name="lists"/>
        </packages>
    </test>
</suite>

And here's an example of how the tests are laid out:

public class login_logout extends TestBase {
    @Test
    public void login(){
        //initiates login page object and call super user login
        LoginPage login = LoginPage.navigateTo(driver, base_url)
        LoggedInPage loggedIn = login.superuserlogin();
        }
    }

Test Base is laid out as follows:

public class TestBase {
    public static WebDriver driver;
    public static DesiredCapabilitiess capabilities;
    @BeforeClass
    public static void setUp(){
        base_url = "*login page url*;
        capabilities = DesiredCapabilities.chrome();
        driver = new RemoteWebDriver(capabilities);
        driver.get(base_url);
    }
}

It's probably something really obvious that I'm missing but any help would be appreciated.

Thanks in advance.

1
  • Duplicate Question here Commented Feb 15, 2013 at 7:28

2 Answers 2

4

The driver object is static. So you have 4 initializations happening and 4 browsers launching but the driver being a static , it would hold only the reference to the last initialized browser and hence all your commands are being executed against the same driver. You can try exploring Threadlocal objects for your parallel runs.

Sign up to request clarification or add additional context in comments.

4 Comments

Yep static WebDriver object, changed now and grid is up and running smoothly. ++ for the Threadlocal suggestion. Thanks
@niharika_neo: can you give a small example i had tried but didn't succeed. during parallel executions i am running into weird issues like suddenly the tests of one test class are running on some other browser instance of some other tests.. Any help will be appreciated. TIA!!!
@MrunalGosar You would need to show what are you using in your code to help me understand what is going wrong.
@niharika_neo i solved my issue by using your solution of ThreadLocal. stackoverflow.com/questions/19067885/… check out here.
0

From my point of view, you made the right decision in choosing WebDriver and TestNG. But due to the fact these are really powerful tools there are some basics you should know.

In the first step, it's important to have some programming experience in general.

Secondly there are some specific tricks you can add.

Why don't you initialize a WebDriver in every single Test Class (either in the constructor or a @BeforeClass)?

Later on, you can also use the @DataProvider and @Factory pattern to do the configuration.

That's kinda cool!

2 Comments

If the driver is initialized in before class, then the parallel runs at method level would still cause issues..
That's right, personally I initialize in the constructor with a TestNG Factory and inject the settings with a DataProvider. I think that's a premier way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.