Home > Back-end >  How to solve the error of data provider if only one browser is taking the complete input data?
How to solve the error of data provider if only one browser is taking the complete input data?

Time:12-20

I'm trying to achieve parallel execution through data provider, but unable to do it. can someone help me with this.
The error is two browsers are opening but the data is going to only one browser.

Here, is my program code:

package PracticePrograms;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class DataProviderSample1{
    
    public WebDriver driver;
    
    @BeforeMethod
    public void setUp() throws InterruptedException {

        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();       
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(6));   
        driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");

    }
    
    @Test(dataProvider = "inputData")
    public void testLogin(String UserName, String Password ) throws InterruptedException {
    
        driver.findElement(By.name("username")).sendKeys(UserName);
        driver.findElement(By.name("password")).sendKeys(Password);
        driver.findElement(By.xpath("//button[@type='submit']")).click();
    }
    
    @DataProvider(name = "inputData", parallel = true)
    public static Object[][] loginData() {
        Object[][] data = new Object[2][2];
        data[0][0] = "Admin";
        data[0][1] = "admin123";

        data[1][0] = "admin";
        data[1][1] = "admin123";

        return data;
    }
    
     @AfterMethod
        public void tear_down() throws InterruptedException {
           driver.quit();            
        }  

}

I appreciate u r help on this.

CodePudding user response:

Despite your method logic runs in parallel all your threads still use the same instance of your test class. Hence they share the same instance of your driver field.

This leads to the case when the setUp() executed "after" overwrites the value set by setUp that has been executed "before"..

So to overcome this you need to introduce ThreadLocal variable. Instead of public WebDriver driver; you would have ThreadLocal<WebDriver> driver = new ThreadLocal<>();

Now instead of driver = new FirefoxDriver(); you would have driver.set( new FirefoxDriver());

After you have set that driver to ThreadLocal, wherever you obtain its value you have to do driver.get(). For example:

  driver.get().manage().deleteAllCookies();
  driver.get().manage().timeouts().implicitlyWait(Duration.ofSeconds(6));   
  driver.get().get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
  • Related