1
votes

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.

2
Duplicate Question hereManigandan

2 Answers

4
votes

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.

0
votes

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!