简体   繁体   中英

Few problems, when running a test method using multiple threads with TestNG

I am facing a problem, when running a test method using multiple threads with TestNG.

The test method is ran two times using two threads with below configuration:

@Test(threadPoolSize=2,invocationCount=2)
public void mainRun() { ... }

It is launching two browsers for executing the test method in parallel as expected. After launching the browsers, it has to type some text in a text box. So, during this step, the text was sent to text box 'twice' in one browser and the other browser was idle.

Can someone please guide me how to overcome this problem?

Your driver is being stored in a static property, therefore both threads are accessing it simultaneously, which would account for the behaviour you are seeing.

Making the driver a class instance property would only resolve the issue if TestNG creates a new class instance for each invocation rather than share the same class instance. I do not know how TestNG works in this way, so cannot say whether it will fix your problem.

If you want to have the same webdriver test, run many times in parallel threads using @invocationCount, then your test method must be isolated and atomic, and using a static webdriver does not support this.

To allow static methods to access the webdriver instances, then you will have to pass the webdriver object into the static method as an argument;

eg

public static void doSomethingFoo(WebDriver driver) {

    // do something to the driver
}

There are other ways run parallel threads of different tests methods in TestNG such as creating a suite xml file, but even this will have the same problem caused by the use of a static property to store the webdriver instance.

Perhaps the use of this static property is not a good idea.

I am wondering if your driver object is a class property and is therefore shared between invocations? Potentially the two threads are referencing the same property. Although I am not sure how TestNG works when running a method multiple times, this is a problem when running different methods in the same class in parallel.

trying defining the propery at method level and let me know what happens.

I don't understand your problem enough (how are you instantiating and running the commands on both drivers)...but I believe I know what you want.

If you want to do parallel testing with TestNG with multiple drivers, then I believe that you need to use @dataProvider .

@dataProvider(parallel=true, name= "drivers")
public Object[][] getDrivers(){
    return new Object[][]{{firefoxDriver},{chromeDriver}};
}

@Test(dataProvider="drivers")
public void mainRun(WebDriver driver){
    test goes here
}

What this does:

Before each invocation of any @Test that has that dataProvider, it will run your getDrivers() function, and get the drivers.

Its a two-dimensional array because the first dimension are the sets of data you pass in to the test, while the second dimension is that set of data you want to pass in.

If this is not what you are looking for...then please be more specific in what you want.

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