英文:
Run tests in parallel using @Dataprovider
问题
以下是翻译好的部分:
我想要为10个用户运行登录测试。用户凭证已保存在Excel表中。因此,我想要一次性运行3个测试,即前3行将启动3个专用的Chrome浏览器,然后再运行3个,最后只运行1个。
但问题是,浏览器还会从不同的行中获取数据。
为了解决这个问题,我尝试在测试方法中使用synchronized
关键字,但浏览器不会并行打开,它们会按顺序打开、执行测试,然后退出。
我该如何解决这个问题?我希望每一行都有一个专用的Chrome浏览器。
public class DemoParallelTesting{
WebDriver wdriver;
@BeforeMethod
public synchronized void parallelDemo() throws Exception {
wdriver = new ChromeDriver();
wdriver.get("https://www.baseURL.com");
}
@Test(dataProvider = "loginData")
public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
// 进行登录操作
}
@DataProvider(name = "loginData", parallel = true)
public Object[][] getData() {
String filepath= System.getProperty("user.dir") + "/src/test/resources/testData/" + "loginData.xlsx";
Object data[][] = testData(filepath, "Sheet1");
return data;
}
public Object[][] testData(String filepath, String sheetName) {
// 读取Excel文件并返回数据
return data;
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" data-provider-thread-count="3">
<test name="DemoTest" parallel="methods">
<classes>
<class name="rough.DemoParallelTesting"/>
</classes>
</test>
</suite>
英文:
I want to run login test for 10 users. The user credentials are saved in an excel sheet. So, instead of running this test row after row, I want to run 3 in one go, means top 3 rows will have 3 dedicated chrome browsers launched, and then 3 more and then only 1.
But issue is, browsers picking data from different rows also.
To overcome this issue I tried using synchronized
keyword in Test Method but then browsers are not opening in parallel, they open sequentially, execute test and quit.
How can I fix this issue? I want one dedicated chrome browser for each row.
public class DemoParallelTesting{
WebDriver wdriver;
@BeforeMethod
public synchronized void parallelDemo() throws Exception {
// public void parallelDemo() throws Exception {
wdriver = new ChromeDriver();
wdriver.get("https://www.baseURL.com");
}
@Test(dataProvider = "loginData")
public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
// Do login
}
@DataProvider(name = "loginData", parallel = true)
public Object[][] getData() {
String filepath= System.getProperty("user.dir") + "/src/test/resources/testData/" + "loginData.xlsx";
Object data[][] = testData(filepath, "Sheet1");
return data;
}
public Object[][] testData(String filepath, String sheetName) {
// read excel file
return data;
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" data-provider-thread-count="3">
<test name="DemoTest" parallel="methods">
<classes>
<class name="rough.DemoParallelTesting"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
答案1
得分: 1
代替从Excel中获取数据,使用数据提供者类来执行此测试。在这里,您需要进行以下更改:
- 在Registration_data定义中添加数据提供者类:
@Test(dataProvider = "loginData", dataProviderClass = DataProviderSource.class)
public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
// 进行登录操作
}
- 在数据提供者类中创建以下方法:
@DataProvider(parallel = true)
public static Object[][] loginData() {
Object[][] param = new Object[10][2];
param[0][0] = test_user1;
param[0][1] = passUser1;
param[1][0] = test_user2;
param[1][1] = passUser2;
param[2][0] = test_user3;
param[2][1] = passUser3;
// ... 继续添加其他数据
}
英文:
Instead of picking up from the excel, use data provider class to do this test. Here, you need to change:
- Add Data provider class in the Registration_data definition
@Test(dataProvider = "loginData", dataProviderClass = DataProviderSource.class)
public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
// Do login
}
- Create method as below in dataprovider class:
@DataProvider(parallel = true)
public static Object[][] loginData() {
Object[][] param = new Object[10][2];
param[0][0] = test_user1;
param[0][1] = passUser1;
param[1][0] = test_user2;
param[1][1] = passUser2;
param[2][0] = test_user3;
param[2][1] = passUser3;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论