'Selenium Parallel Execution NoSuchSessionException: invalid session id
I am trying to execute tests cases in parallel with TestNG. My test cases will use the same page and sometimes the same page objects. I close the webdriver after every scenario, however, I am getting an invalid session ID on random test cases. I checked the session IDs and it seems like the method is trying to use the most recently closed session to try and locate the web element. Does this mean that my drivers are not threadsafe? My DriverFactory class is as below
DriverFactory Class
public class DriverFactory{
private DriverFactory() {
}
private static DriverFactory instance = new DriverFactory();
public static DriverFactory getInstance() {
return instance;
}
ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>();
public WebDriver getDriver() {
return driver.get();
}
public void setDriver(WebDriver driverParm) {
driver.set(driverParm);
}
public void closeBrowser() {
driver.get().close();
driver.remove();
}
}
In my Page Object classes, I created a constructor with the WebDriver. An example of my page object classes is as follows
Page Object Class
public class ExamplePageOne{
private WebDriver driver;
public ExmaplePageOne(WebDriver driver){
this.driver=driver;
PageFactory.initElements(driver,this);
}
@FindBy(how=How.XPATH, using = "//xpath here")
WebElement ButtonOne;
@FindBy(how=How.XPATH, using = "//xpath here")
WebElement ButtonTwo;
public void clickOnButtonOne(){
ButtonOne.click()
}
public void clickOnButtonTwo(){
ButtonTwo.click()
}
public void validate(){
//validate code here
}
}
In my step definition files, I create the instance of the page object class whenever I want to use it. I have also tried to create the instance out of the step methods but that did not work too.
Step Definition Class
public class StepDef{
@Given("Given step")
public void given_step(){
//block of code here
}
@When("When step")
public void when_step(){
ExamplePageOne pageOne = new ExamplePageOne(DriverFactory.getInstance.getDriver());
pageOne.clickOnButtonOne;
}
@Then("Then step")
public void then_step(){
ExamplePageOne pageOne = new ExamplePageOne(DriverFactory.getInstance.getDriver());
pageOne.validate;
}
}
In my hooks class, I set up and tear down the browser.
Hooks class
public class Hooks{
@Before
public void setup(){
DriverFactory.getInstance.setDriver(myOpenBrowserMethod);
WebDriver driver = DriverFactory.getInstance.getDriver();
driver.get("url here")
}
@After
public void teardown(){
DriverFactory.getInstance.closeBrowser();
}
}
Sorry if the code looks kinda weird, mostly typing this with memory. Is there something that I am doing wrong or missing? I thought my logic made sense but unfortunately not. Any help is appreciated, thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
