2013-05-22 33 views
-1

我使用Junit 4和selenium WebDriver和Eclipse在ubuntu上運行我的測試。
當我運行我的測試中,我有這樣的錯誤:Ubuntu上的JUnit 4(WebDriver)

> org.openqa.selenium.StaleElementReferenceException: Element not found 
> in the cache - perhaps the page has changed since it was looked up 

當我調試我的測試它的作品。
這是我的測試:

package com.QD2.Login; 

import java.util.concurrent.TimeUnit; 
import org.junit.*; 
import static org.junit.Assert.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class Login {  

    private WebDriver driver;  
    private String baseUrl;  
    private StringBuffer verificationErrors = new StringBuffer(); 

    @Before public void setUp() throws Exception { 
    driver = new FirefoxDriver(); 
    baseUrl = "http://localhost:8088/"; 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test public void testUntitled() throws Exception { 
    driver.get(baseUrl + "/QD2/pages/accueil/login.xhtml#loaded"); 
    //driver.findElement(By.id("login")).clear(); 
    driver.findElement(By.xpath("//*[@id='login']")).sendKeys("admin"); 
    //driver.findElement(By.id("password")).clear(); 
    driver.findElement(By.id("password")).sendKeys("admin"); 
    driver.findElement(By.id("loginButton")).click(); 
    } 

    @After public void tearDown() throws Exception { 
    //driver.quit(); 
    String verificationErrorString = verificationErrors.toString(); 
    if (!"".equals(verificationErrorString)) { 
     fail(verificationErrorString); 
    } 
    } 
} 
+0

發生錯誤的位置? – e1che

+0

後頁面加載 –

回答

0

這是個例:我覺得這是從你#loaded這就是URL

driver.get(baseUrl + "/QD2/pages/accueil/login.xhtml#loaded"); 

嘗試沒有內部org.openqa.selenium.StaleElementReferenceException

WebElement element = driver.findElement(By.id("input123")); // get the input 
WebElement button= driver.findElement(By.id("btn123")); // this button sends the form 
button.click(); 
element.sendKeys("the cache is already released.."); // error here 

所以它並告訴我發生了什麼事。


更新:因爲該錯誤發生在你已經改變了源通過

[...] 
WebElement login = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.presenceOfElementLocated(By.id("login"))); 
login.sendKeys("admin"); 
[...] 

但通常等待不會幫助:

嘗試使用這樣的等待導航到其他頁面或發送表格等。

+0

當我累了沒有#加載頁面das'nt加載 但我使用> Thread.sleep(1000); (baseUrl +「/QD2/pages/accueil/login.xhtml#loaded」);它的工作 謝謝你的幫助:) –

+0

@dév更新了我的文章 – e1che