SeleniumWebDriver(Java):如何嵌套这些NoTouchElement异常测试?

SeleniumWebDriver(Java):如何嵌套这些NoTouchElement异常测试?,java,selenium-webdriver,Java,Selenium Webdriver,操作系统:Windows7 32位 ChromeDriver版本:2.30 Selenium Webdriver版本:3.4.0 爪哇8 我尝试了几种不同的方法来清理代码,而不必重复相同的try/catch块。我试图检查我正在测试的页面上是否存在各种元素。我可以优雅地向控制台报告,这段代码确实可以正常工作。 我现在遇到的问题是代码不太好。有没有办法嵌套这些try/catch块,或者将它们放入if/else循环中 try { driver.findElement(By.xp

操作系统:Windows7 32位 ChromeDriver版本:2.30 Selenium Webdriver版本:3.4.0 爪哇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();
        }
有几件事

  • 我坚信异常应该是异常的,这意味着异常不应该被用作流控制。这一点得到了进一步的支持

    findElement不应用于查找不存在的元素,而应使用findElements(By)和断言零长度响应

    因此,您应该用
    .findElement()
    替换
    .findElement()
    并测试是否为空。参见#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");
    
  • 可以说它更快,等等,但是。。。看到人们使用像XPath这样复杂的东西只按ID定位元素,这让我很伤心,例如,
    by.XPath(“/*[@ID='logindiv']”)
    。这比.id(“logindiv”)更简单、更容易阅读

  • 您可以通过谷歌搜索查看所有详细信息,但是
    Thread.Sleep()
    是一种不好的做法,应该避免。改用
    WebDriverWait
    。探索
    ExpectedConditions
    ,查看所有可以等待的内容,并充分利用它们。在您在代码中发布的案例中,我看不出有任何理由等待这些,因此这是几秒钟不必要的浪费时间

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

  • 有几件事

  • 我坚信异常应该是异常的,这意味着异常不应该被用作流控制。这一点得到了进一步的支持

    findElement不应用于查找不存在的元素,而应使用findElements(By)和断言零长度响应

    因此,您应该用
    .findElement()
    替换
    .findElement()
    并测试是否为空。参见#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");
    
  • 可以说它更快,等等,但是。。。看到人们使用像XPath这样复杂的东西只按ID定位元素,这让我很伤心,例如,
    by.XPath(“/*[@ID='logindiv']”)
    。这比.id(“logindiv”)更简单、更容易阅读

  • 您可以通过谷歌搜索查看所有详细信息,但是
    Thread.Sleep()
    是一种不好的做法,应该避免。改用
    WebDriverWait
    。探索
    ExpectedConditions
    ,查看所有可以等待的内容,并充分利用它们。在您在代码中发布的案例中,我看不出有任何理由等待这些,因此这是几秒钟不必要的浪费时间

  • 最后一个示例是拉取
    .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;
        }
    }
    
    您可以尝试以下方法:

    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;
        }
    }