2

乗り越えられない問題があります。Selenium を使い始めたばかりで、簡単な JUnit テストを作成しました。(CMS管理パネルを開き、ログインしてログアウトします。)正しいユーザー名とパスワードを使用すると、魅力的に機能しますが、そうでない場合は、「ログアウト」ボタンを見つけるのを乗り越えることができません。私はそれをtry-catchブロックに入れました。:/ 実際には、例外をスローせずに if ステートメント (以下のコードを参照) で停止するだけであり、"driver.quit()" に到達しないため、ブラウザーを閉じてテストを終了することはありません。

私の問題について何か考えがある場合は、私を助けてください!

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;
        }
    }
}
4

2 に答える 2

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."); }

于 2012-11-30T07:53:38.160 に答える
0

まあ..あなたが使用しているのは、すべての要素を暗黙的に待機driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);することを意味します。したがって、プログラムはそのボタンが表示されるまで30秒間待機してから、さらに先に進みます。31 秒待つと、結果が表示されます。

于 2012-11-30T07:27:18.087 に答える