Not able to add selenium Webdriver/appium instances to thread local when running at methods level

30 Views Asked by At

We have created a framework to support for selenium Webdriver and appium, when I run at test level , its running fine but when we run test at methods level only one instance of the driver being added into ThreadLocal and other drivers/browsers are idle.

BaseTest Class:

@BeforeMethod(alwaysRun = true)
    @Parameters({"deviceName","platformType","browserName"})
    public void setUp(String deviceName,@Optional("mac") String platformType,@Optional("chrome")String browserName){

    isHeadLess=false

    DriverFactory.initializeWebDriver(WebBrowserName.valueOf(browserName.toUpperCase()), isHeadLess);
    driver= DriverManager.getDriver();
            
    }

DriverFactory class:

 public static void initializeWebDriver(WebBrowserName browserName,Boolean isHeadless) {
    RemoteWebDriver driver = null;
    switch (browserName) {
      case CHROME:
        driver = Drivers.createChromeDriver(isHeadless);
        break;
      case FIREFOX:
        driver = Drivers.createFirefoxDriver(isHeadless);
        break;
  
      default:
        throw new DriverInitializationException(
                "Platform name " + browserName + " is not found. Please check the platform name");
    }
    DriverManager.setAppiumDriver(driver)
  }

Driver class:

 public static RemoteWebDriver createChromeDriver(Boolean isHeadless){
        try {
            RemoteWebDriver driver = null;
            setupDriver(DriverManagerType.CHROME);
            ChromeOptions options = new ChromeOptions();
            if (isHeadless) {
                options.addArguments("--headless");
            }
            driver = new ChromeDriver(options);
            logger.info("created chrome driver in driver class");
            return driver;
        } catch (MalformedURLException e) {
          
        }
    }

DriverManager class:

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DriverManager {
  private static final Logger logger = LoggerFactory.getLogger(DriverManager.class);
  private static final ThreadLocal<RemoteWebDriver> threadLocalDriver = new ThreadLocal<>();

  public static RemoteWebDriver getDriver() {
    return threadLocalDriver.get();
  }

  public static void setAppiumDriver(RemoteWebDriver driver) {
    if (Objects.nonNull(driver)) {
      threadLocalDriver.set(driver);
    }
  }

  public static synchronized void unload() {
    threadLocalDriver.remove();
  }
}

At this line : driver = new ChromeDriver(options); all 3 browsers being launched simultaneously, after this line we can add only one driver instance into ThreadLocal

TestNG.xml

<suite name="Search Engine Tests" verbose="4" parallel="methods"> 
    <test name="Web chrome tests Google" enabled="true" parallel="methods" thread-count="4">         
        <parameter name="deviceName" value="WebChrome" />         
        <parameter name="platformType" value="Mac" />         
        <parameter name="browserName" value="chrome" />         
        <classes>             
            <class name="tests.SearchEngines">                 
                <methods>                     
                    <include name= "launchGoogle"/>                     
                    <include name= "launchYahoo"/>                     
                    <include name= "launchRediff"/>                 
                </methods>             
            </class>         
        </classes>     
    </test> 
</suite>

We would like to run in parallel using tesNG and thread-local at tests, methods, group level

as of now test level working fine but whereas at the method level, only one tests run on one browser, other browsers are idle

can you please correct my approach or code to run smoothly without any issues?

Scope: Mobile browser, Web browsers Tech Stack: Appium 2., Selenium webdriver 4., Java Client 9,TestNG, Allure

I do not want to confuse with my half-brain question, but what I have observed when I run test methods in parallel

<suite name="Search Engine Tests" verbose="4" parallel="methods">
<test name="Web chrome tests Google" enabled="true" parallel="methods" thread-count="4">

As mentioned in the testNG it should open 4 browsers and 4 driver instances but here 4 browsers are being launched but it returns only one WebDriver instance, so the other 3 opened browsers simply show idle so accordingly I can add only one instance to my local thread

I am not sure what's wrong here, so requesting someone look into this and assist me in fixing this issue

0

There are 0 best solutions below