0

Same script works when executing sequentially, butt parallel execution fails. When i try to execute the test in parallel, all the browsers launch but after couple of steps only one browser able to complete the flow and other browsers get stuck at some point randomly

Error : org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? Build info: version: '4.8.1', revision: '8ebccac989' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.6' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [null, findElement {using=xpath, value=//span[text()='My Info']}] Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 113.0.5672.127, chrome: {chromedriverVersion: 113.0.5672.63 (0e1a4471d5ae..., userDataDir: C:\Users\Siva\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:54574}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:54574/devtoo..., se:cdpVersion: 113.0.5672.127, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true} at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:145) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543) at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162) at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:60) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344) at pages.BasePage.waitForElementVisible(BasePage.java:19) at pages.MyInfoPage.filldataSave(MyInfoPage.java:26) at tests.HRMTest.Test2(HRMTest.java:35) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139) at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:677) at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:221) at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50) at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:969) at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194) at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148) at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:833)

Here is the complete framework and all the classes listed, can someone look into this issue

BaseTest Class

public class BaseTest { public WebDriver driver; private static ThreadLocal<WebDriver> drivertl = new ThreadLocal<WebDriver>(); public WebDriverWait wait; public ExtentTest test; public ExtentReports report; public PageParent pageParent; Logger log = Logger.getLogger(BaseTest.class); @BeforeTest public void beforeClass() { String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()); String Name = System.getProperty("user.dir") + "/Reports/Report" + timestamp + ".html"; report = new ExtentReports(Name); } @AfterTest public void adterTest() { report.flush(); report.close(); } public static WebDriver getDriver(){ return drivertl.get(); } @BeforeMethod public void setup() throws InterruptedException { WebDriverManager.chromedriver().setup(); ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); drivertl.set(new ChromeDriver(options)); getDriver().manage().window().maximize(); getDriver().manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); Thread.sleep(5000); test = report.startTest("Test1"); pageParent = new PageParent(getDriver(), wait, test); log.info("Starting"); } @AfterMethod public void afterMethod(ITestResult result) { if(result.getStatus()==ITestResult.SUCCESS) { test.log(LogStatus.PASS , "Successful"); }else { test.log(LogStatus.FAIL, "Test Fialed"); } getDriver().close(); } } 
public class HRMTest extends BaseTest { @Test public void Test1() throws InterruptedException { // getDriver().get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login"); //Login System.out.println("Test1-Before Login"); pageParent.getInstance(LoginPage.class).login("Admin", "admin123"); System.out.println("Test1-After Login"); Thread.sleep(10000); //filldata pageParent.getInstance(MyInfoPage.class).filldataSave("Siva", "S"); Thread.sleep(10000); } @Test public void Test2() throws InterruptedException { // getDriver().get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login"); //Login System.out.println("Test2-Before Login"); pageParent.getInstance(LoginPage.class).login("Admin", "admin123"); System.out.println("Test2-After Login"); Thread.sleep(10000); //filldata pageParent.getInstance(MyInfoPage.class).filldataSave("Hanish", "S"); Thread.sleep(10000); } @Test public void Test3() throws InterruptedException { // getDriver().get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login"); //Login System.out.println("Test3-Before Login"); pageParent.getInstance(LoginPage.class).login("Admin", "admin123"); System.out.println("Test3-After Login"); Thread.sleep(10000); //filldata pageParent.getInstance(MyInfoPage.class).filldataSave("Riyansh", "S"); Thread.sleep(10000); } } 
public class PageParent { public static WebDriver driver; public static WebDriverWait wait; public static ExtentTest test; public PageParent(WebDriver driver,WebDriverWait wait,ExtentTest test) { PageParent.driver=driver; PageParent.wait =wait; PageParent.test=test; } public<TestPage extends BasePage> TestPage getInstance(Class<TestPage> pageClass) { try { return pageClass.getDeclaredConstructor(WebDriver.class,WebDriverWait.class,ExtentTest.class).newInstance(this.driver,this.wait,this.test); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } } 
public class BasePage extends PageParent { public BasePage(WebDriver driver, WebDriverWait wait, ExtentTest test) { super(driver, wait, test); // TODO Auto-generated constructor stub } public void waitForElementVisible(By locator) { driver.findElement(locator).isDisplayed(); } public void doClick(By locator) { driver.findElement(locator).click(); } public void doActionsClick(By locator) { Actions actions = new Actions(driver); actions.moveToElement(driver.findElement(locator)).click().build().perform(); } public void doScrollToElement(By locator) { JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].scrollIntoView(true);", driver.findElement(locator)); } public void doActionsMouseHover(By locator) { Actions actions = new Actions(driver); actions.moveToElement(driver.findElement(locator)).build().perform(); } public void dosendkeys(By locator,String Value) { driver.findElement(locator).sendKeys(Value); } } 
 public class LoginPage extends BasePage{ public LoginPage(WebDriver driver, WebDriverWait wait, ExtentTest test) { super(driver, wait, test); // TODO Auto-generated constructor stub } By username = By.name("username"); By password = By.name("password"); By submit = By.xpath("//button[@type='submit']"); public void login(String User, String Password) { waitForElementVisible(username); dosendkeys(username, User); waitForElementVisible(password); dosendkeys(password , Password); doActionsClick(submit); } } 
public class MyInfoPage extends BasePage { public MyInfoPage(WebDriver driver, WebDriverWait wait, ExtentTest test) { super(driver, wait, test); // TODO Auto-generated constructor stub } //By By myInfo = By.xpath("//span[text()='My Info']"); By firstName = By.name("firstName"); By middleName = By.name("middleName"); By lastName = By.name("lastName"); By save = By.xpath("(//button[@type='submit'])[1]"); public void filldataSave (String FirstName, String LastName) { System.out.println("Before My Info"); waitForElementVisible(myInfo); doActionsClick(myInfo); System.out.println("Clicked My Info"); waitForElementVisible(firstName); dosendkeys(firstName, FirstName); System.out.println("Entered FirstName"); waitForElementVisible(lastName); dosendkeys(lastName, LastName); System.out.println("Clicking Save"); doActionsClick(save); } } 

This test works in sequential execution, but failing in parallel execution

    1 Answer 1

    1

    Despite you have "thread-local" webdriver you still have problem with public PageParent pageParent; since it is shared across your threads.

    So basically the last started thread overwrites the pageParent reference with the page object bound to its thread-local driver and all the parallel threads running instruction pageParent.blahBlah(..); basically communicate with a single web-driver session instead of communicating with their own one.

    The dirty solution could be to wrap your PageParent with ThreadLocal as well. But honestly I would revisit your entire architecture as it looks thread-risky.

    1
    • We didnt plan for Parallel execution in the beginning and went ahead with this Architecture, however we are planning to implement Parallel execution and looking for solutionsCommentedJun 14, 2023 at 5:42

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.