繁体   English   中英

Selenium WebDriver(Java):如何嵌套这些NoSuchElement异常测试?

[英]Selenium WebDriver (Java): How can I nest these NoSuchElement Exception tests?

操作系统:Windows 7 32bit ChromeDriver版本:2.30 Selenium Webdriver版本:3.4.0 Java 8

我尝试了几种不同的方法来清理此代码,而不必重复相同的try / catch块。 我正在尝试检查正在测试的页面上是否存在各种元素。 我可以正常地向控制台报告,此代码可以正常工作。 我遇到的问题是不优雅的代码。 有没有办法嵌套这些try / catch块,或将它们放在if / else循环中?

try {
            driver.findElement(By.xpath("/html/head/title"));
            System.out.println("Title found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("Title NOT FOUND...");
            // ex.printStackTrace();
        }

        try {
            driver.findElement(By.xpath("//*[@id='logindiv']"));
            System.out.println("Login form found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("Login form NOT FOUND...");
            // ex.printStackTrace();
        }

        try {
            driver.findElement(By.id("username"));
            System.out.println("'Username' field found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("'Username' form NOT FOUND...");
            // ex.printStackTrace();
        }

        try {
            driver.findElement(By.id("password"));
            System.out.println("'Password' field found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("'Password' form NOT FOUND...");
            // ex.printStackTrace();
        }

        try {
            driver.findElement(By.id("loginSubmit")).getText();
            System.out.println("Login button found...");
            Thread.sleep(1000);
        } catch (NoSuchElementException ex) {
            System.out.println("Login button NOT FOUND...");
            // ex.printStackTrace();
        }

一些东西...

  1. 我坚信异常应该是例外,这意味着异常不应用作流程控制。 docs进一步支持这一点,

    findElement不应用于查找不存在的元素,而应使用findElements(By)并声明零长度响应。

    所以你应该更换.findElement()try-catch.findElements()和测试空。 参见#2中的示例。

  2. 您确实应该使用一些Assert库,例如JUnit等。它使这些验证变得更加容易/简洁。

    这整个事情

     try { driver.findElement(By.id("username")); System.out.println("'Username' field found..."); Thread.sleep(1000); } catch (NoSuchElementException ex) { System.out.println("'Username' form NOT FOUND..."); // ex.printStackTrace(); } 

    应该/可能看起来像

     Assert.assertFalse(driver.findElements(By.id("username")).isEmpty(), "Validate Username field exists"); 
  3. 可以说它更快,等等。但是……看到人们使用诸如XPath之类的复杂功能仅通过ID定位元素,例如By.xpath("//*[@id='logindiv']") 这比By.id("logindiv")更简单易读。

  4. 您可以进行一些谷歌搜索以查看所有详细信息,但是Thread.Sleep()是一种不好的做法,应避免使用。 而是使用WebDriverWait 探索ExpectedConditions ,查看所有可以等待的内容并自由使用它。 在您发布到代码中的情况下,我看不出有任何理由等待任何这些,因此这是浪费了几秒钟的不必要时间。

  5. 您的最后一个示例是提取.getText()但未使用它。 由于您只是在检查按钮是否存在,因此可以安全地从最后删除.getText()

您可以尝试以下方法:

public void checkElementVisibile()throws InterruptedException {
    element = driver.findElement(By.xpath("/html/head/title"));
    Thread.sleep(1000);
    if(isElementVisibile(element))
        System.out.println("Title found...");
    else
        System.out.println("Title not found...");
    element = driver.findElement(By.xpath("//*[@id='logindiv']"));
    Thread.sleep(1000);
    if(isElementVisibile(element))
        System.out.println("Login form found...");
    else
        System.out.println("Login form not found...");
    element = driver.findElement(By.id("username"));
    Thread.sleep(1000); 
    if(isElementVisibile(element))
        System.out.println("'Username' field found...");
    else
        System.out.println("'Username' field not found...");
    element = driver.findElement(By.id("password"));
    Thread.sleep(1000);
    if(isElementVisibile(element))
        System.out.println("'Password' field found...");
    else
        System.out.println("'Password' field not found...");
}
public static boolean isElementVisibile(WebElement element){
    try {
        return element.isDisplayed();
    }catch (NoSuchElementException ex)
    {
        return false;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM