javaseleniumautomationtestngparallel-execution

TestNG Parallel execution always fails when i run more than one testcase, Among all only one testscript was passing


I am using Testng for parallel execution of my web testcase. Totally i am having 5 classes.

  1. BaseClass - for initializing and closing of my browser
  2. Core class - Mediator for all drivers initialized
  3. Reusable methods - Click, settext, gettext... [extends Step #2 Core class, so driver comes from there only]
  4. Page Object Class - To store all locators like name,ID,xpath.Uses all those reusable methods like click, gettext,settext.
  5. Main Test Class.

Base Class

public class TestNGBase {
ThreadLocal<WebDriver> localdriver = new ThreadLocal<>();

@BeforeMethod
public void initialize(){
    System.setProperty("webdriver.chrome.driver","C:\\SeleniumTest\\chromedriver.exe");
    localdriver.set(new ChromeDriver());
}

public WebDriver driver(){
    Core.setDriver(localdriver.get());
    return localdriver.get();
}

@AfterMethod
public void teardown(){
    localdriver.get().close();
    localdriver.remove();
}
}

Core Class:

public class Core {

protected static WebDriver driver;

public static void setDriver(WebDriver driverr) {
    driver = driverr;
    
}
}

Reusable Class:

public class WebMethods extends Core {
public WebMethods() {
    
}

public static void Click(By by) {              
        driver.findElement(by).click();       
}

PageObject Class

public class pagemethods(){

By login = By.name("login");

public void login(){
WebMethods.click(login);}
}

MainTestclass1 : Will use above Pageobject MainTestclass2 : Will use above Pageobject MainTestclass3 : Will use above Pageobject

So in above 3 testcase when i trigger all those using testng.xml file. 3 new browser gets initialized and it successfully opens the url. But when i start using the all those reusable methods such as click(). Out of 3 Testcase, any of the two testcase is always getting failed.

I think problem starts Core class as it receives all drivers at the same time. It's collapsing something.

Can some one help me to solve this parallel execution failure problem.

Thanks


Solution

  • Try to not make the main class static. Create a class that makes an instance of the class and then executes. When you make a static class, the method is hanging off of that class, not an instance.

    E.g.

    public WebDriver
    {
    
    WebDriver myWebDriver = new WebDriver();
    myWebDriver.whateverMethod();
    
    }
    

    When using threads avoid static. Try that first.