【发布时间】:2011-05-10 20:58:09
【问题描述】:
我使用 webdrive 编写了一个测试,如下所示:
//put stuff in the database
fillDatabaseWithParticipants();
WebDriver driver = new FirefoxDriver();
doLogin(driver);
//finds the search button and clicks it
WebElement searchButton = driver.findElement(By.xpath("/html/body/div/div[8]/div[2]/form/table/tbody/tr[5]/td/button"));
searchButton.click();
//clicks the first column of a Primefaces dataTable component to order the content in ascending order
driver.findElement(By.xpath(("/html/body/div/div[8]/div[2]/form/div/div/table/thead/tr/th"))).click();
//gets the first table division of the first table row
WebElement firstTableDivision = driver.findElement(By.xpath("/html/body/div/div[8]/div[2]/form/div/div/table/tbody/tr/td"));
Assert.assertEquals("Should be the same name", "A name inserted by me in the database", firstTableDivision.getText());
测试的作用是打开 Firefox 浏览器,进入站点并登录。然后单击页面的搜索按钮并等待数据表出现。当数据表与结果一起出现时,测试单击第一列以按升序对其进行排序。然后它执行assertEquals() 来查看名字是否确实是名字(我在测试开始时插入到数据库中的名字)。
我遇到的问题是,这个测试只有在我在调试模式下运行时才有效。如果我在 Eclipse 中使用 Run As... jUnit 测试运行它,firstTableDivision.getText() 的返回值是第一次呈现表格划分时的第一个内容。
我尝试像这样使用ExpectedCondition:
ExpectedCondition e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
WebElement changingElement = d.findElement(By.xpath("/html/body/div/div[8]/div[2]/form/div/div/table/tbody/tr/td"));
System.out.println(changingElement.getText());
if(changingElement.getText().equalsIgnoreCase("A Name inserted by me")){
return true;
} else {
return false;
}
}
};
org.openqa.selenium.support.ui.Wait<WebDriver> w = new WebDriverWait(driver, 15);
w.until(e);
也试过driver.manage().timeouts().implicitlyWait(new Long(15), TimeUnit.SECONDS);。
我知道,我在那里写的预期条件很糟糕,但这只是为了看看它是否会起作用。这两种方法的结果是一样的。返回值是 dataTable 首次呈现时的名字。
我搜索了这种问题,发现它可能是FirefoxDriver和浏览器中的DOM之间的竞争条件。
我想知道 primefaces 是否有任何可以收听的事件,我可以将其放入 ExpectedCondition 以结束比赛条件,但找不到类似的事件。
你们觉得我应该怎么做?我错过了任何解决方案吗?
【问题讨论】:
标签: java junit automated-tests primefaces webdriver