How do we share same Webdriver session through out our test suite?

38 views
Skip to first unread message

Mansi

unread,
Aug 22, 2017, 5:00:29 AM8/22/17
to Selenium Users
**Query# What is the best solution to share same WebDriver object through out our tests? Meaning if we are running a test suite where each test performs a different operation, how do we maintain the same browser session through out?**

## Meta -
OS:  
<MAC OS, Windows>
Selenium Version:  
<WebDriver 3.4 -->
Browser:  
Firefox,Chrome

Browser Version:  
<Chrome 60.0 (64-bit) -->


Sample Code goes like this -

public class BaseClass {
static WebDriver driver;
@BeforeMethod
public void openBrowser() throws Exception{
driver=SelectBrowser.StartBrowser("firefox","http://automationpractice.com/");
Login objLogin=PageFactory.initElements(driver, Login.class);
driver=objLogin.TestLogin();
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
@Test
public void PlaceNewOrder() throws Exception{
PlaceAnOrder objPlaceOrder=PageFactory.initElements(driver, PlaceAnOrder.class);
objPlaceOrder.PlaceOrder();
}

Expected# I wish to use the same webdriver object that I am initializing in the @BeforeMethod, in the other @Test too. 

Actual# But when the control moves to @Test, it opens a new browser session. 

Krishnan Mahadevan

unread,
Aug 22, 2017, 11:01:53 AM8/22/17
to seleniu...@googlegroups.com

If you would like to share the same WebDriver across two or more @Test annotated methods in the same test class, then annotate your openBrowser() method with an @BeforeClass annotation.

 

 

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 "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/b0ed0868-c203-41e4-9d16-cc07f65b1a26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mansi

unread,
Aug 22, 2017, 10:01:48 PM8/22/17
to Selenium Users
Hi Krishnan, 

I tried @BeforeClass annotation, but it still opens 2 browser sessions in the below example - one during login and another for PlaceOrder.

Thanks,
Manaswini Das


On Tuesday, August 22, 2017 at 7:00:29 PM UTC+10, Mansi wrote:
**Query# What is the best solution to share same WebDriver object through out our tests? Meaning if we are running a test suite where each test performs a different operation, how do we maintain the same browser session through out?**

## Meta -
OS:  
<MAC OS, Windows>
Selenium Version:  
<WebDriver 3.4 -->
Browser:  
Firefox,Chrome

Browser Version:  
<Chrome 60.0 (64-bit) -->


Sample Code goes like this -

public class BaseClass {
static WebDriver driver;
@BeforeClass
public void openBrowser() throws Exception{
driver=SelectBrowser.StartBrowser("firefox","http://automationpractice.com/");
Login objLogin=PageFactory.initElements(driver, Login.class);
driver=objLogin.TestLogin();
}
@AfterClass

⇜Krishnan Mahadevan⇝

unread,
Aug 22, 2017, 10:04:20 PM8/22/17
to Selenium Users

Can you please share the complete code ? Am guessing that your webdriver instantiation is happening somewhere else


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Reply all
Reply to author
Forward
0 new messages