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

好吧..您正在使用driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);这意味着隐式等待所有元素。因此,您的程序会等待30秒以使该按钮出现,然后再继续。尝试等待 31 秒,然后您将看到结果。

于 2012-11-30T07:27:18.087 回答
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 回答