2012-04-11 69 views
5

我想使用selenium webdriver(java)測試一個站點,但該站點包含ajax,HTMLUnit沒有看到ajax內容。使用Ajax的WebDriver HTMLUnit問題

是否有解決方法?

實施例:

WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6); 
//login into your account 
this.login(driver); 
//click on edit Profile Link into an ajax-loaded tab 
driver.findElement(By.id("editProfile")).click(); 
//Result: org.openqa.selenium.NoSuchElementException 
+0

從我個人的經驗來看,HTMLUnitDriver javascript支持是相當糟糕的,而且仿真更糟。我停止使用它進行迴歸測試,因爲它引入了比它能夠檢測到的更多的錯誤:( – 2012-04-11 18:14:20

回答

5

使用與元件用Ajax響應之後必須出現相互作用之前等待條件。

WebDriverWait wait = new WebDriverWait(webDriver, 5); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_element"))); 

這使得webDriver在5秒內等待你的元素。這個問題之前曾被問到過。

+0

這個問題並沒有集中在WebDriver本身上,它集中在帶有ajax的HtmlUnitDriver上... – Krzys 2012-04-11 12:22:24

+2

WebDriver是一個接口,HtmlUnitDriver等等它的工作方式也一樣,不用擔心。 – 2012-04-11 15:33:06