【问题标题】:Selenium JUnit 4 test - usage of findElement()Selenium JUnit 4 测试 - findElement() 的用法
【发布时间】:2023-04-10 00:58:02
【问题描述】:

我有一个无法解决的问题。我刚开始使用 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;
        }
    }
}

【问题讨论】:

    标签: java eclipse selenium junit webdriver


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!结合您的 isElementPresent() 函数和方法 (1),它可以正常工作。
    【解决方案2】:

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

    【讨论】:

    • 有趣的是我等了超过 10 分钟,但什么也没发生。但是,使用 Eugene 编写的函数可以正常工作,因此原始的 isElementPresent() 可能并不合适。还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2012-12-18
    • 2023-03-15
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多