我必须将两个表作为QA进行比较。我在Selenium中使用以下代码提取两者的值。
仅供参考:我为两个表中的每一个使用了两个不同的浏览器窗口和不同的变量名称(即每个环境中有一个)
String first_part = ".//*[@id='Matrix']/tbody/tr[";
String second_part = "]/td[";
String third_part = "]/a";
//rows of the matrix table (Always 7)
for (int i=4; i<=7; i++){
//Columns of the matrix table (Always 4)
for (int j=1; j<=4; j++){
//Prepare final xpath for each cell value
String matrix_cell_path = first_part+i+second_part+j+third_part;
//Retrieve cell-value
String matrix_cell_data= driver.findElement(By.xpath(matrix_cell_path)).getText();
System.out.print("matrix_cell_data["+i+"]["+j+"]= "+matrix_cell_data + " ");
}
System.out.println("");
System.out.println("");
}
String first_part_Prod = ".//*[@id='Matrix']/tbody/tr[";
String second_part_Prod = "]/td[";
String third_part_Prod = "]/a";
//rows of the matrix table (Always 7)
for (int k=4; k<=7; k++){
//Columns of the matrix table (Always 4)
for (int l=1; l<=4; l++){
//Prepare final xpath for each cell value
String matrix_cell_path_Prod = first_part_Prod+k+second_part_Prod+l+third_part_Prod;
//Retrieve cell-value
String matrix_cell_data_Prod = second_driver.findElement(By.xpath(matrix_cell_path_Prod)).getText();
System.out.print("matrix_cell_data_Prod["+k+"]["+l+"]= "+matrix_cell_data_Prod + " ");
}
System.out.println("");
System.out.println("");
}
我想验证两个表中的每个单元格是否相等。怎么做?
我使用了以下检查,但显示错误:
matrix_cell_data无法解析为变量。
//Matrix Data - Content Check:
try {
matrix_cell_data = matrix_cell_data_Prod;
System.out.println("Great");
}catch (Exception ex2){
System.out.println("Exception"+ex2);
}
答案 0 :(得分:0)
我通常首先创建一个类,比如自己实现Comparable的MyMatrixData
public class MyMatrixData implements Comparable<MyMatrixData> {
private String columnOneName;
private String columnTwoName;
private String columnNName;
//With getters and setters (not shown)
@Override
public int compareTo(MyMatrixData actualData) {
assertEquals("Some helpful text", this.getColumnOneName(), actualData.getColumnOneName());
assertEquals("Some helpful text", this.getColumnTwoName(), actualData.getColumnTwoName());
assertEquals("Some helpful text", this.getColumnNName(), actualData.getColumnNName());
return 0;
}
}
接下来,我创建一个功能文件,其中包含导致实际数据显示在页面上的步骤。接下来是一个从表中获取实际数据并将其与预期进行比较的步骤。类似的东西:
Feature: Compare expected MatrixData to actual
Scenario: Some data driven or choice driven test
Given user "me" logs in to the Matrix system
When I navigate to the page that displays MatrixData
Then the displayed MatrixData matches the expected
| columnOneName | columnTwoName | columnNName |
| Killroy | was | here |
| Betty Bop | was | too |
步骤定义可能类似于
@Then ("^the displayed MatrixData matches the expected$)
public void the_displayed_matrixdata_matches_expected(List<MyMatrixData> expectedData) throws Throwable {
for (int i=1;i<expectedData.size(),i++) {
MyMatrixData actualMatrixRow = new MyMatrixData();
actualMatrixRow.setColumnOneName(aPageObject.getColumnOneName(i));
actualMatrixRow.setColumnTwoName(aPageObject.getColumnTwoName(i));
actualMatrixRow.setColumnNName(aPageObject.getColumnNName(i));
assertEquals("helpful msg", 0, expectedData.get(i).compareTo(actualMatrixRow));
}
}
我离开了页面对象方法,以便从HTML中获取数据作为练习。我通常还创建一个页面对象方法来获取实际的行数,以便我可以将它与预期的行数进行比较。也留作练习。 :-)您可能会注意到只有在compareTo中的所有断言都通过时才返回0。步骤def中的断言是更多文档,然后是其他任何内容。
我一遍又一遍地使用这种模式。我希望你觉得它很有用。