Java Selenium 测试:如何从 CSV 中读取特定行和/或行以将数据提供给 TestNG 测试
Java Selenium testing: How to read in a specific line and or row from a CSV to feed in data to a TestNG test
我正在做一个即将完成的项目。我知道我的方法和代码总体上有效,但我仍然坚持如何从 CSV 文件中读取特定的行或列。
示例 - 我的 CVS 看起来像这样...
- 标题一,URL1,登录名1,密码1 |
- 标题二,URL2,登录名2,密码2 |
- 标题三,URL3,登录名3,密码3 |
- ...等等
这是我在 CSV 文件中读取的@DataProvider
//opens and reads in CVS file from resource folder
@DataProvider
public Iterator<Object[]> expectedTitles() throws IOException {
List<Object []>testData = new ArrayList<>();
String[] data = null;
BufferedReader br = new BufferedReader(new FileReader("src/main/resources/expectedTitles.csv"));
String line;
while ((line = br.readLine()) != null){
data = line.split(",");
testData.add(data);
}
return testData.iterator();
}
对于 CSV 中的每一行数据,我还有一个 @Test 方法,看起来像这样。
//executes sideNavAboutLink test
@Test(dataProvider = "expectedTitles")
public void sideNavAboutLink(String pageTitle){
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
AboutLink page = new AboutLink(driver);
page.loadPage();
page.clickSideAbout(); //clicks on link
page.validateURLSideNav(); //Validates URL
page.validateTitleSideNav(pageTitle); //Validates Page Title
}
目前这一切或多或少都有效,因为我没有完全填写我的 CSV,但它只有 pageTitle,但就像我上面所说的,我希望能够调用任何给定的行或列以摆脱冗余代码。我看过其他一些示例,但我还没有想出如何使其适应我上面的代码。
请提供任何帮助,谢谢。
我认为您的测试方法可以使用数组本身而不是第一个字符串,这样您就可以将测试方法中的数组值与索引一起使用并获取任何行/列值。
public void sideNavAboutLink(String args[]){
会带入整个数组,然后我们可以得到:
String pageTitle = args[0];
String url = args[1];
String userName = args[2];
String password = args[3];
在您的测试方法中,并在您的函数调用中使用这些变量。
这是一步一步的例子。
我有一个包含 3 个测试数据集的 CSV 文件。我创建了一个函数 return 基于提供的标题的特定数据集。我们有 3 个独立的数据提供者来为每个测试用例提供服务。现在我们可以运行一个测试数据一个测试用例。
我会希望它能帮到你。
CSV 文件数据:
基于提供标题读取特定行的功能(可根据需要定制)
private String[][] expectedTitles(String titleName) throws IOException {
String[][] testData = null;
String[] data = null;
String line = null;
BufferedReader br = new BufferedReader(new FileReader("...\yourfilepath\data.csv"));
while ((line = br.readLine()) != null){
data = line.split(",");
testData= new String[1][data.length];
if(data[0].equalsIgnoreCase(titleName))
{
for(int i =0; i<data.length; i++)
{
testData[0][i] = data[i];
}
break;
}
}
return testData;
}
测试数据专用的DataProvider
@DataProvider(name = "GoogleDataprovider")
public Object[][] googleDataprovider() throws IOException {
Object[][] arrayObject = expectedTitles("Google");
return arrayObject;
}
@DataProvider(name = "MicrosoftDataprovider")
public Object[][] microsoftDataprovider() throws IOException {
Object[][] arrayObject = expectedTitles("Microsoft");
return arrayObject;
}
@DataProvider(name = "WallmartDataprovider")
public Object[][] wallmartDataprovider() throws IOException {
Object[][] arrayObject = expectedTitles("Wallmart");
return arrayObject;
}
使用来自数据提供者的特定测试数据的测试用例(1 个数据集的 1 个测试用例)
@Test(dataProvider="GoogleDataprovider")
public void testGoogleData(String title, String url, String domain) {
Assert.assertEquals("Google", title);
Assert.assertEquals("www.google.com", url);
Assert.assertEquals("Search engine", domain);
}
@Test(dataProvider="MicrosoftDataprovider")
public void testMicrosoftData(String title, String url, String domain) {
Assert.assertEquals("Microsoft", title);
Assert.assertEquals("www.microsoft.com", url);
Assert.assertEquals("Operating System", domain);
}
@Test(dataProvider="WallmartDataprovider")
public void testWallmartData(String title, String url, String domain) {
Assert.assertEquals("Wallmart", title);
Assert.assertEquals("www.wallmart.com", url);
Assert.assertEquals("Retail", domain);
}
输出:
PASSED: testGoogleData("Google", "www.google.com", "Search engine")
PASSED: testMicrosoftData("Microsoft", "www.microsoft.com", "Operating
System") PASSED: testWallmartData("Wallmart", "www.wallmart.com",
"Retail")
我正在做一个即将完成的项目。我知道我的方法和代码总体上有效,但我仍然坚持如何从 CSV 文件中读取特定的行或列。
示例 - 我的 CVS 看起来像这样...
- 标题一,URL1,登录名1,密码1 |
- 标题二,URL2,登录名2,密码2 |
- 标题三,URL3,登录名3,密码3 |
- ...等等
这是我在 CSV 文件中读取的@DataProvider
//opens and reads in CVS file from resource folder
@DataProvider
public Iterator<Object[]> expectedTitles() throws IOException {
List<Object []>testData = new ArrayList<>();
String[] data = null;
BufferedReader br = new BufferedReader(new FileReader("src/main/resources/expectedTitles.csv"));
String line;
while ((line = br.readLine()) != null){
data = line.split(",");
testData.add(data);
}
return testData.iterator();
}
对于 CSV 中的每一行数据,我还有一个 @Test 方法,看起来像这样。
//executes sideNavAboutLink test
@Test(dataProvider = "expectedTitles")
public void sideNavAboutLink(String pageTitle){
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
AboutLink page = new AboutLink(driver);
page.loadPage();
page.clickSideAbout(); //clicks on link
page.validateURLSideNav(); //Validates URL
page.validateTitleSideNav(pageTitle); //Validates Page Title
}
目前这一切或多或少都有效,因为我没有完全填写我的 CSV,但它只有 pageTitle,但就像我上面所说的,我希望能够调用任何给定的行或列以摆脱冗余代码。我看过其他一些示例,但我还没有想出如何使其适应我上面的代码。
请提供任何帮助,谢谢。
我认为您的测试方法可以使用数组本身而不是第一个字符串,这样您就可以将测试方法中的数组值与索引一起使用并获取任何行/列值。
public void sideNavAboutLink(String args[]){
会带入整个数组,然后我们可以得到:
String pageTitle = args[0];
String url = args[1];
String userName = args[2];
String password = args[3];
在您的测试方法中,并在您的函数调用中使用这些变量。
这是一步一步的例子。
我有一个包含 3 个测试数据集的 CSV 文件。我创建了一个函数 return 基于提供的标题的特定数据集。我们有 3 个独立的数据提供者来为每个测试用例提供服务。现在我们可以运行一个测试数据一个测试用例。
我会希望它能帮到你。
CSV 文件数据:
基于提供标题读取特定行的功能(可根据需要定制)
private String[][] expectedTitles(String titleName) throws IOException {
String[][] testData = null;
String[] data = null;
String line = null;
BufferedReader br = new BufferedReader(new FileReader("...\yourfilepath\data.csv"));
while ((line = br.readLine()) != null){
data = line.split(",");
testData= new String[1][data.length];
if(data[0].equalsIgnoreCase(titleName))
{
for(int i =0; i<data.length; i++)
{
testData[0][i] = data[i];
}
break;
}
}
return testData;
}
测试数据专用的DataProvider
@DataProvider(name = "GoogleDataprovider")
public Object[][] googleDataprovider() throws IOException {
Object[][] arrayObject = expectedTitles("Google");
return arrayObject;
}
@DataProvider(name = "MicrosoftDataprovider")
public Object[][] microsoftDataprovider() throws IOException {
Object[][] arrayObject = expectedTitles("Microsoft");
return arrayObject;
}
@DataProvider(name = "WallmartDataprovider")
public Object[][] wallmartDataprovider() throws IOException {
Object[][] arrayObject = expectedTitles("Wallmart");
return arrayObject;
}
使用来自数据提供者的特定测试数据的测试用例(1 个数据集的 1 个测试用例)
@Test(dataProvider="GoogleDataprovider")
public void testGoogleData(String title, String url, String domain) {
Assert.assertEquals("Google", title);
Assert.assertEquals("www.google.com", url);
Assert.assertEquals("Search engine", domain);
}
@Test(dataProvider="MicrosoftDataprovider")
public void testMicrosoftData(String title, String url, String domain) {
Assert.assertEquals("Microsoft", title);
Assert.assertEquals("www.microsoft.com", url);
Assert.assertEquals("Operating System", domain);
}
@Test(dataProvider="WallmartDataprovider")
public void testWallmartData(String title, String url, String domain) {
Assert.assertEquals("Wallmart", title);
Assert.assertEquals("www.wallmart.com", url);
Assert.assertEquals("Retail", domain);
}
输出:
PASSED: testGoogleData("Google", "www.google.com", "Search engine") PASSED: testMicrosoftData("Microsoft", "www.microsoft.com", "Operating System") PASSED: testWallmartData("Wallmart", "www.wallmart.com", "Retail")