java - Selenium JUnit 4 测试 - findElement() 的用法

标签 java eclipse selenium junit webdriver

我遇到了一个无法解决的问题。我刚开始使用 Selenium,并用它做了一个简单的 JUnit 测试。 (打开 CMS 管理面板,登录并注销。)如果我使用正确的用户名和密码,它就像一个魅力,但如果我不这样做,它就无法找到“注销”按钮,但是我已经把它放到 try-catch block 中了。 :/实际上它只是在 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;
        }
    }
}

最佳答案

好吧,问题是在您按下“登录”按钮后页面应该呈现。 所以在你的情况下我会尝试不同的等待机制:

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

关于java - Selenium JUnit 4 测试 - findElement() 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13636909/

相关文章:

java - 如何在android中动态设置布局

java - 如果您在开发模式下运行 GAE 项目,实体会保存在哪里?

java - 在eclipse中运行tomcat上的maven项目时找不到请求资源404错误

javascript - 从javascript onclick返回值到变量或使用onclick更改全局变量

python - 进入otp页面时如何自动社交登录

java - 如何创建使用 XStream 的 Arquillian 测试?

java - Android 检索联系人数据时出现 RuntimeException

python - 使用 Selenium 和 BeautifulSoup 提取 iFrame 内容

java - Java 中的 JAVA_OPTS "-D"标志的值是否有最大大小限制?

java - 更改 eclipse 项目中 jar 的顺序