我寫了一個測試使用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