在实际测试项目中,咱们常常要进行操做后要对页面文本元素进行比对来验证测试结果。下面我是写了一个公共方法,测试项目中要进行文本比对的地方都调用该方法。css
具体实现步骤以下:java
第一步:在基础类BaseTest中,写一个“页面刷新后文本比对”方法witeForPageRefeshByText()。app
代码以下: ide
public void witeForPageRefeshByText(WebDriver driver, final String cssSelector, final String text, final boolean exist) { // int index = exist == true ? 0 : -1; WebElement lasttr = (new WebDriverWait(driver, 40)) .until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver d) { WebElement tbody = driver.findElement(By .cssSelector(cssSelector)); if (exist == true) { if (tbody.getText().indexOf(text) > 0) { return tbody; } else { return null; } } else { if (tbody.getText().indexOf(text) == -1) { return tbody; } else { return null; } } } }); }
第二步:在测试case中,继承BaseTest基础类(extends BaseTest)。在要作结果比对的地方调用“页面刷新后文本比对”方法witeForPageRefeshByText()。测试
2.1新增菜单后,刷新页面,比对结果,代码以下:code
//新增菜单操做后,刷新页面,比对结果 witeForPageRefeshByText(driver, "#data_table > tbody", code,true);
2.2删除菜单记录后,刷新页面,比对结果,代码以下:继承
//删除后刷新数据列表界面,比较名称是否存在 witeForPageRefeshByText(driver, "#data_table > tbody", code, false);