我写了一个测试使用webdrive这样的:的webdriver /硒2不正确找到标记文字primefaces组件
//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
结束竞争条件,但无法找到类似的东西。
你们认为我该怎么办?我错过了这个解决方案?
我遇到的问题不在于查找页面中的元素,而是因为它返回的元素不是我在点菜后排序的那个元素。 – AlwaysLearning 2011-05-16 21:06:25