I'm very new using TestNG and Java programming in general and I have a question in regards to runing testcases with dataprovider in parallel,
in order to run dataprovider test cases in multiple tabs in a single chrome window instead of many windows, I used selenium 4 which allows to open and switch to new tab using the folowing code
driver.switchTo().newWindow(WindowType.TAB);
so, I wrote this code which is successfully run the test cases in sequence mode, but when i run it in parallel mode, it implements each line separately, before moving on to the other line,
consequently, it opens the 3 tabs at the same time, afterwards, it opens the link in the last tab 3 times, and finally it tries to do the 3 searches at the same time, and therefore it cannot execute them.
I find as a result that testNG create a single webdriver to control all the tabs, which is logical to cause the failure of the tests in parallel mode.
https://i.stack.imgur.com/LV5KS.jpg
This is the code:
package com.mycompany.app;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class testTab {
WebDriver driver;
@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver", "C:\\Browsers drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
@BeforeMethod
public void newTab() {
driver.switchTo().newWindow(WindowType.TAB);
}
@Test(dataProvider = "getData")
public void testAmazon1(String search_word) {
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[@name='q']")).sendKeys(search_word + (Keys.RETURN));
}
@DataProvider(parallel=true)
public Object[][] getData() {
Object[][] data = new Object[3][1];
data[0][0] = "bihi";
data[1][0] = "boutfounast";
data[2][0] = "hmad l9rran";
return data;
}
}
This is XML file
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Sample Suite" parallel="methods" thread-count="3">
<test name="Amazon test">
<classes>
<class name="com.mycompany.app.testTab"></class>
</classes>
</test>
</suite>
Is there a way to create a webdriver for each test? I am sorry for not being precise and thank you for your answers.