Java+Selenium 如何参数化验证Table表格数据
场景: 当我们编写脚本时候,需要验证某个表格某一列数据,或者多个列数据。 如果每验证一个就写一个方法,实在是太费事, 因此我们需要有参数化的思想,把某列数据看成固定的元素,然后去验证即可。
1. 示例Steps
@Then("^I gererate shift by cycle (.+) on the 查看修改班组 popup$") public void generateShift(String cycle) throws Exception { as.generateShiftByCycle(cycle); }
2.查看具体实现方法
//在设定排班规律页面中,在“循环周期”输入框内,输入数字,点击“生成小循环” public void generateShiftByCycle(String cycle) throws Exception { putInValue(By.id("dvCycleNum"), cycle); find(By.xpath("//div[@id='dvCycleInfo']//button[contains(text(),'生成小循环')]")).click(); By allTr = By.xpath("//div[@id='dvCycleInfo']//table/tbody//tr"); List<WebElement> trs = waitForMinimumNum(allTr, Integer.valueOf(cycle)); Assert.isEqual(Integer.valueOf(cycle), trs.size(), "生成的一个循环的排班总数"); }
3. 先去Table所有列标题,然后用List 集合显示所有元素。
4.查找某一列元素方法。
protected List<WebElement> waitForMinimumNum(final By by, final int minNum) { Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(Const.TIMEOUT, TimeUnit.SECONDS) .pollingEvery(Const.POLL, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class); wait.until(new Function<WebDriver, Boolean>() { public Boolean apply(WebDriver driver) { try { List<WebElement> list = driver.findElements(by); if (list.size() >= minNum) { return true; } else { return false; } } catch (Exception e) { return false; } } }); return driver.findElements(by); }