2012-11-29 49 views
2

我有一個問題,我無法通過。我剛開始使用Selenium,並且用它做了一個簡單的JUnit測試。 (打開CMS管理面板,登錄並註銷。)如果我使用正確的用戶名和密碼,它就像一個魅力,但如果我不這樣做,它只是無法克服找到「註銷」按鈕,但是我已將它放入try-catch塊。 :/實際上它只是通過if語句停止(請參閱下面的代碼)而不拋出異常,並且它不會關閉瀏覽器並完成測試,因爲它永遠不會達到「driver.quit()」。Selenium JUnit 4測試 - 使用findElement()

如果您對我的問題有任何意見,請幫助我!

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 JavaExp { 
    private WebDriver driver; 
    private String baseUrl; 
    private StringBuffer verificationErrors = new StringBuffer(); 
    @Before 
    public void setUp() throws Exception { 
     driver = new FirefoxDriver(); 
     baseUrl = "http://urlhere.com/"; 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void testJValami() throws Exception { 
     driver.get(baseUrl + "/administrator/"); 
     driver.findElement(By.id("mod-login-username")).clear(); 
     driver.findElement(By.id("mod-login-username")).sendKeys("admin"); 
     driver.findElement(By.id("mod-login-password")).clear(); 
     driver.findElement(By.id("mod-login-password")).sendKeys("almakfa"); 
     driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button 
     if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link 
      System.out.println("Got it."); 
     } else { 
      System.out.println("Not found."); 
     } 
    } 

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

    private boolean isElementPresent(By by) { 
     try { 
      driver.findElement(by); 
      return true; 
     } catch (NoSuchElementException e) { 
      return false; 
     } 
    } 
} 

回答

0

嗯,問題是,你按'登錄'按鈕頁面應該呈現。 所以你的情況我會嘗試不同的等待機制:

public boolean isElementPresent(By selector) 
    { 
     return driver.findElements(selector).size()>0; 
    } 
  1. driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link System.out.println("Got it."); } else { System.out.println("Not found."); }
  2. driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button Thread.sleep(1000); if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link System.out.println("Got it."); } else { System.out.println("Not found."); }

  3. public WebElement fluentWait(final By locator){ Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(org.openqa.selenium.NoSuchElementException.class); WebElement foo = wait.until( new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); } } ); return foo; } ; driver.findElement(By.linkText("Belépés")).click(); //Click on the login Button fluentWait(By.linkText("Kilépés")); if(isElementPresent(By.linkText("Kilépés"))) { //Looking for the Log out link System.out.println("Got it."); } else { System.out.println("Not found."); }

+0

謝謝!通過您的isElementPresent()函數和方法(1)的組合,它可以正常工作。 – AZS

0

嗯..你正在使用driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);這意味着所有元素隱含的等待。因此,您的程序會等待該按鈕出現,然後進一步執行。嘗試等待31秒,然後你會看到結果。

+0

有趣的是,我等了超過10分鐘,什麼也沒有發生。但是,尤金寫的這個功能起作用了,所以可能原來的isElementPresent()並不合適。不管怎麼說,還是要謝謝你! – AZS