how to make driver as thread safe to run methods of a class in parallel in selenium java

555 Views Asked by At

I have 3 test methods using driver from Base class. I tired to run these methods in parallel but getting failures. Reponse to my problem is appreciated. Thanks

Class having 3 test methods

public class TestCases extends BaseClass {
    @Test
    public void Test1() {

        
        homePage.checkIfElementIsDisplayed(homePage.emailElement);
        homePage.checkIfElementIsDisplayed(homePage.passwordElement);
        homePage.checkIfElementIsDisplayed(homePage.signInElement);
        homePage.emailElement.sendKeys("[email protected]");
        homePage.passwordElement.sendKeys("******");

    }

    @Test
    public void Test2() {

        homePage.checkValuesInListGroup();
        homePage.checkSecondListItem();
        homePage.checkSecondListItemBadgeValue();


    }

    @Test
    public void Test3() throws InterruptedException {

        
        homePage.ScrolltotheElement(homePage.dropDownOption);
        homePage.checkDefaultSelectedValue();
        homePage.selectOption3();
    }
}

Base Class

public class BaseClass {

    public WebDriver driver;
    public HomePage homePage;

    public WebDriver setup() throws IOException {
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream(
                System.getProperty("user.dir") + "\\src\\main\\resource\\GlobalData.Properties");
        prop.load(fis);
        String browserName = System.getProperty("browser") != null ? System.getProperty("browser")
                : prop.getProperty("browser");

        if (browserName.contains("chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
        }

        else if (browserName.contains("edge")) {
            WebDriverManager.edgedriver().setup();
            driver = new EdgeDriver();
        } else if (browserName.contains("firefox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
        }
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.manage().window().maximize();
        return driver;


    }

    @BeforeMethod
    public HomePage LaunchApplication() throws IOException {
        driver = setup();
        homePage = new HomePage(driver);
        homePage.goTo();
        return homePage;
    }

    @AfterMethod
    public void tearDown() throws IOException {

        driver.close();
        
    }

I tried creating ThreadLocal Class for WebDriver as

ThreadLocal<WebDriver> threadSafeDriver=new ThreadLocal<WebDriver>();

and use this in setup() method of BaseClass by writing

threadSafeDriver.set(driver);

but this didnot really help

1

There are 1 best solutions below

2
On

Most likely you are using the TestNG framework. One of the differences between JUnit and TestNG is that JUnit creates a new class instance for each test method by default but TestNG creates a single instance for all test methods in the class.

You can see the parallel option in TestNG suite (see docs) but there is no way to force TestNG to create a new instance for each test.

The simplest solution is to switch to JUnit framework. Then the code from the example should work.