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.

user2071812
  • 47
  • 2
  • 11
  • Duplicate Question [here](http://stackoverflow.com/questions/14827448/scripts-are-not-running-as-expected-while-running-them-using-selenium-webdriver/14832679#14832679) – Manigandan Feb 15 '13 at 07:28

2 Answers2

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.

RN.
  • 997
  • 4
  • 14
  • 31
niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • Yep static WebDriver object, changed now and grid is up and running smoothly. ++ for the Threadlocal suggestion. Thanks – user2071812 Feb 15 '13 at 16:05
  • @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!!! – Mrunal Gosar Sep 28 '13 at 17:17
  • @MrunalGosar You would need to show what are you using in your code to help me understand what is going wrong. – niharika_neo Sep 30 '13 at 02:27
  • @niharika_neo i solved my issue by using your solution of ThreadLocal. http://stackoverflow.com/questions/19067885/static-webdriver-instance-synchronization-in-java/19074040?noredirect=1#comment28211070_19074040 check out here. – Mrunal Gosar Sep 30 '13 at 15:11
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!

Franz Ebner
  • 4,951
  • 3
  • 39
  • 57
  • If the driver is initialized in before class, then the parallel runs at method level would still cause issues.. – niharika_neo Feb 18 '13 at 17:17
  • 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. – Franz Ebner Feb 19 '13 at 08:57