Does testContext get shared between parallel test threads?

568 views
Skip to first unread message

deepng

unread,
Feb 19, 2018, 3:46:45 AM2/19/18
to testng-users
Hi

I am planning to run my tests in parallel. 
Say I want to login as a user but want to login as 3 different users in different threads and run through the same tests for these users. 
I am using TestContext to hold the various entities related to the user session between different testMethods. 
I am wondering if the testContext is shared between the various threads here? 

⇜Krishnan Mahadevan⇝

unread,
Feb 19, 2018, 5:24:40 AM2/19/18
to testng...@googlegroups.com
ITestContext is shared by one or more @Test methods that are part of the same <test> tag.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

vishu handa

unread,
Mar 27, 2021, 6:49:45 AM3/27/21
to testng-users
Hi


i am doing a similar kind of thing but while running test cases in parallel my execution fails

what i am doing is as an example

i have BaseTest class which sets itestcontext.setattribute("driver",new chromedriver()) under beforeemethod and in aftermethod i remove that attribute , however in testclasses i have multiple tests and i am using webdriver by taking it from like 

@test()
public void test1(itestcontext itestcontext){
webdriver driver = (webdriver)itestcontext.getattribute("driver");
//and subsequently using it in my page object classes

}

@test()
public void test2(itestcontext itestcontext){
webdriver driver = (webdriver)itestcontext.getattribute("driver");
//and subsequently using it in my page object classes

}


@test()
public void test3(itestcontext itestcontext){
webdriver driver = (webdriver)itestcontext.getattribute("driver");
//and subsequently using it in my page object classes

}


my test cases are running fine in serial order , but while parallel execution, it fails and getting error like sessionnotfound, or unable to connect webdriverexception. could you please help me out on this.

you can check out the demoproject in given repository. adding a url





On Monday, February 19, 2018 at 3:54:40 PM UTC+5:30 Krishnan Mahadevan wrote:
ITestContext is shared by one or more @Test methods that are part of the same <test> tag.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

On Mon, Feb 19, 2018 at 2:16 PM, deepng <dee...@gmail.com> wrote:
Hi

I am planning to run my tests in parallel. 
Say I want to login as a user but want to login as 3 different users in different threads and run through the same tests for these users. 
I am using TestContext to hold the various entities related to the user session between different testMethods. 
I am wondering if the testContext is shared between the various threads here? 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

⇜Krishnan Mahadevan⇝

unread,
Mar 27, 2021, 7:04:53 AM3/27/21
to testng-users
The basic problem with your code is that you are trying to create a WebDriver instance in a BeforeMethod and then trying to share that with a "@Test" method via ITestContext.

ITestContext represents a "<test>". So it is common for all "@Test" methods that are found within the same "<test>" tag.
So when you run your "@Test" methods in parallel, you are basically causing "@Test" methods to try and share the same "WebDriver" instance since it's being written into the same "ITestContext". That is why you are having problems with your concurrent executions.

I would suggest that you rethink your approach.

Sometime back I built an annotation driven approach that is thread safe and lets you share a WebDriver instance cleanly.

You can take a look at it here: https://github.com/rationaleemotions/autospawn 

The other option would be for you to leverage a ThreadLocal variant of managing webdrivers.

I have detailed the approach in this blog post of mine : https://rationaleemotions.com/parallel_webdriver_executions_using_testng/ 


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


Reply all
Reply to author
Forward
0 new messages