0

I have used implementation of ThreadLocal for Webdriver to run test methods in parallel from single Test class. I used BeforeMethod for LaunchingApplication and AfterMethod for teardown. Though my 6 different methods I have in Test class have different Thread IDs and 6 browser windows are opened, only one method is navigated to url and executing the method and close the browser window. Other 5 browser windows are just opened and no further actions are performed. I assume the only method that is exectued is having first thread ID.

My project in Github: https://github.com/venkatakarteek/Practice

Please help to see my code in BaseClass in src/test/java/com/Assignment/TestComponents TestCases in src/test/java/com/Assignment/TestCases/TestCases testng.xml - used to run the test methods in parallel

BaseClass

public class BaseClass {

    public WebDriver driver;

    protected static ThreadLocal<WebDriver> threadSafeDriver = new ThreadLocal<>();
    public HomePage homePage;

    public WebDriver browsersetup() 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(alwaysRun = true)
    public HomePage LaunchApplication() throws IOException {
        driver = browsersetup();
        threadSafeDriver.set(driver);
        System.out.println("Before Method Thread ID: " + Thread.currentThread().getId());
        driver = threadSafeDriver.get();
        homePage = new HomePage(driver);
        homePage.goTo();
        return homePage;
    }

    @AfterMethod(alwaysRun = true)
    public void tearDown() throws IOException {

        driver.close();
        System.out.println("After Method Thread ID: " + Thread.currentThread().getId());
        threadSafeDriver.remove();
    }

}

TestCases class

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

        System.out.println("Test1 Method Thread ID: " + Thread.currentThread().getId());
        homePage.checkIfElementIsDisplayed(homePage.emailElement);
        homePage.checkIfElementIsDisplayed(homePage.passwordElement);
        homePage.checkIfElementIsDisplayed(homePage.signInElement);
        homePage.emailElement.sendKeys("karteek@gmail.com");
        homePage.passwordElement.sendKeys("******");

    }

    @Test
    public void Test2() {

        System.out.println("Test2 Method Thread ID: " + Thread.currentThread().getId());

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

    }

    @Test
    public void Test3() throws InterruptedException {

        System.out.println("Test3 Method Thread ID: " + Thread.currentThread().getId());
        homePage.ScrolltotheElement(homePage.dropDownOption);
        homePage.checkDefaultSelectedValue();
        homePage.selectOption3();
    }

    @Test
    public void Test4() {

        System.out.println("Test4 Method Thread ID: " + Thread.currentThread().getId());
        homePage.ScrolltotheElement(homePage.enabledButtonElement);
        homePage.checkIfFirstButtonisEnabled();
        homePage.checkIfButtonisDisabled(homePage.disabledButtonElement);

    }

    @Test
    public void Test5() {

        System.out.println("Test5 Method Thread ID: " + Thread.currentThread().getId());
        homePage.ScrolltotheElement(homePage.test5Div);
        homePage.ExplicitWait(homePage.test5Button);
        homePage.clickOnButton();
        homePage.checkIfButtonisDisabled(homePage.test5Button);

    }

    @Test
    public void Test6() throws IOException {

        System.out.println("Test6 Method Thread ID: " + Thread.currentThread().getId());
        homePage.ScrolltotheElement(homePage.test6Div);
        String cellValue = homePage.findValueOfCell(driver, 2, 2);
        System.out.println(cellValue);

    }
}

XML used to run test methods in parallel

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test thread-count="6" name="Test" parallel="methods">
        <classes>

            <class name="com.Assignment.TestCases.TestCases" />

        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

I tried this ThreadLocal implementation by seeing the post from Linkedin

https://www.linkedin.com/pulse/selenium-parallel-testing-using-java-threadlocal-testng-shargo/

but getting parallel execution is not working for my code to run methods parallely

0 Answers0