简体   繁体   中英

How to run selenium test in parallel using the same dataprovider

I have a scenario where i need to run my selenium test in parallel using the same data provider. From what i have read it is possible but could not get it to work.I have a hub and a node running on one machine and have another node running on another machine.

My DataProvider

// Data provider for Storage Rule Suite
@DataProvider(name = "StorageRuleDataProvider", parallel =true)
public static Object[][] getStorageData(Method m) {
    return TestUtil.getData(m.getName(), TestBase.storageSuite);
}

My Test

@Test(groups = { "CreateNewStorageRule" }, dependsOnGroups = { "StoragePage" }, dataProviderClass = TestDataProvider.class, dataProvider = "StorageRuleDataProvider", threadPoolSize = 20)
public void createNewStorageRuleTest(Hashtable<String, String> data){}

XML

<suite name="Storage Rule Suite" parallel="tests" data-provider-thread-count="20"  >

When i run the test in the xml file, i have two set of browser opening on each node but when it attempts to do a login, sometimes it enters the credentials twice in one browser and nothing on the other and sometimes nothing gets entered on one browser.

First, you have to use parallel="methods" to run your @Test methods in parallel. Second: I had a similar problem, that more Test methods got executed in the same browser and I solved it by making my WebDriver ThreadSafe.

What you describe is a classical example of non-thread-safe Selenium test automation framework. In most cases you solve this by having an instance of driver per test class, and running all tests from that class in single thread.

However, if you want to run content of single test class in multiple parallel threads, you need to redesign is-a & has-a relationships in your framework. Here is a detailed example of how this can be done:

http://automatictester.co.uk/2015/04/11/parallel-execution-on-method-level-in-selenium-testng-framework

Although, this may add extra work and additional compexity to your test automation. I'd think twice why you want to run Selenium test methods using data provider in parallel and try to answer the question if you really need to do that.

According to my experiences, if you start combining Data Providers with Selenium, you may have a problem with overall test approach. Perhaps you try to automate too much on UI level, instead of pushing the tests down the stack to eg API level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM