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

时间:2017-07-10 21:22:50

标签: java selenium-webdriver

操作系统:Windows 7 32位 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();
        }

2 个答案:

答案 0 :(得分:1)

一些事情......

  1. 我坚信异常应该是例外,这意味着异常不应该被用作流量控制。这进一步得到了支持by the docs

      

    不应该使用findElement来查找不存在的元素,使用findElements(By)并断言零长度响应。

    所以你应该用SELECT LATITUDE, LONGITUDE FROM coordinates WHERE (address = ? AND community = ?) or (text = ? AND community::text LIKE ? ) or (text LIKE ? AND community::text LIKE ?) 替换.findElement()try-catch并测试为空。参见#2中的例子。

  2. 你真的应该使用像JUnit等一些.findElements()库。它使这些验证变得更容易/更清晰。

    这整件事

    Assert

    应该/看起来像

    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();
    }
    
  3. 可以说它的速度更快,等等......但是......看到人们使用像XPath这样复杂的东西只能通过ID定位元素,这让我感到很痛苦,例如Assert.assertFalse(driver.findElements(By.id("username")).isEmpty(), "Validate Username field exists"); 。这比By.xpath("//*[@id='logindiv']")更简单易读。

  4. 您可以通过谷歌搜索查看所有细节,但By.id("logindiv")是一种不好的做法,应该避免。而是使用Thread.Sleep()。探索WebDriverWait以查看可以等待的所有内容并自由地使用它。在您在代码中发布的案例中,我没有理由等待其中任何一项,以免浪费几秒钟的时间。

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

答案 1 :(得分:0)

你可以试试这个:

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;
    }
}
相关问题