Home > Blockchain >  Webdriver instances - how to run multiple tests using one test base
Webdriver instances - how to run multiple tests using one test base

Time:06-18

I'm learning automation testing in Java using Selenium and Testng. I've came across the issue that I cannot really solve, maybe someone here knows the solution.

I'm trying to run multiple tests from one class using multisession.xml. That works only if I add separate instance to each test, something like this:

System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe"); WebDriver driver; driver = new ChromeDriver();

However, I want to use separate class (test base) that extends my class with tests in order to not duplicate code. If I do that, then my multisession.xml can only perform one test at the time and returns following exception: org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

My guess there is a problem with instances of webdriver. In my base initialization code looks like that:

public class TestBase {
public static WebDriver driver;
public static WebDriverWait wait;
public static Properties config;
public static Properties testdata;

Then setting up separate files with test data and initialization:

public static void initialization() {

    String url = config.getProperty("URL");
    String browser = config.getProperty("browser");
    String pageLoadTimeout = config.getProperty("pageLoadTimeout");
    String windowsMaximize = config.getProperty("windowsMaximize");
    String deleteAllCookies = config.getProperty("deleteAllCookies");
    String waitTimeout = config.getProperty("waitTimeout");


    switch (browser) {
        case "chrome":
            System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")  
                    "/src/main/resources/chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--incognito");

            driver = new ChromeDriver(options);

            break;
        case "firefox":
            System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")  
                    "/src/main/resources/geckodriver.exe");

            driver = new FirefoxDriver();

            break;
        case "msedge":
            System.setProperty("webdriver.edge.driver", System.getProperty("user.dir")  
                    "/src/main/resources/msedgedriver.exe");


            driver = new EdgeDriver();
            break;
        case "opera":
            System.setProperty("webdriver.opera.driver", System.getProperty("user.dir")  
                    "/src/main/resources/operadriver.exe");

            driver = new OperaDriver();
            break;

I will appreciate any help!

CodePudding user response:

StaleElementReferenceException is thrown when the element you're looking for is no longer attached to the DOM. Most probably you're trying to .findElement first, then DOM gets refreshed and you're trying to access it. This often happens with parallel tests execution if your page-class instances with elements are not thread-safe.

Try to use:

object _lock = new object();

        lock (_lock)
        {
            //your driver initialization/login
        }

You should also define how many threads will be executing your tests by modifying thread-count="5" parallel="methods"

  • Related