处理丢失的Web元素 - 页面对象模型

时间:2017-03-24 09:39:18

标签: selenium selenium-webdriver webdriver automated-tests

我在我的框架中使用页面对象模型。我已经定义了所有通过静态方法返回的webelements。例如:

public class Login_Page {

private static WebElement element = null;

public static WebElement Textbox_UserName (WebDriver driver)
{

    element = driver.findElement(By.xpath(".//div[@class='username']"));

    return element;

}

在我的测试中,我用它来输入用户名:

     @Test (priority=1)
  public static void Verify_Login() 
// Verify Logging in with right credentials
   {    
      driver.get("myurl");
      Login_Page.Textbox_UserName(driver).sendKeys
       }

我面临的挑战是: 假设应用程序中不存在此元素,比如xpath已更改,当执行Textbox_UserName方法时,我在Login_Page类中得到“NO such element Exception”。 因此,脚本甚至没有进入我的测试Case Verify_login()方法,我实际上将失败输入到我的报告中。

 if (Login_Page.TextBox_UserName(driver).isVisible()==true)
      {
          //PASS
      }else
      {
          //FAIL
              }

任何人都可以建议我如何从那里捕获“No Such element exception”并在我的报告中使用它,这是从我的if / else条件执行的? 我尝试使用Try / Catch块并返回异常,但由于返回类型是TextBox_Username()静态方法中的webElement,我无法做到。 任何帮助表示赞赏。在此先感谢。

3 个答案:

答案 0 :(得分:2)

我一直使用页面对象模型,但由于您遇到的问题(以及类似的问题),我的方式不同。我只在需要时抓住元素。因此,而不是抓住页面上的所有元素,例如PageFactory等等,我等到我要刮掉元素之前要点击一个元素。

下面是一个示例页面对象,显示我正在谈论的内容。

如果某个元素不存在,则在尝试单击该元素时会出现错误。您将看到异常消息中不存在该元素,您可以调查该页面并确定需要更改定位器的内容,您可以转到页面顶部并更改定位器,重建并运行测试。问题解决了。 :)

package sandbox;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class PageObject
{
    private WebDriver driver;
    private By waitForLocator = By.id("someId"); // this is a locator for some element on the page that is last to load

    private By buttonLocator = By.id("sampleId");
    private By usernameLocator = By.id("usernameId");
    private By productNameLocator = By.id("productId");

    public PageObject(WebDriver webDriver)
    {
        this.driver = webDriver;

        // wait for page to finish loading
        new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(waitForLocator));

        // see if we're on the right page
        if (!driver.getCurrentUrl().contains("samplePage.jsp"))
        {
            throw new IllegalStateException("This is not the XXXX Sample page. Current URL: " + driver.getCurrentUrl());
        }
    }

    public void clickButton()
    {
        driver.findElement(buttonLocator).click();
    }

    public String getProductName()
    {
        return driver.findElement(productNameLocator).getText();
    }

    public void setUsername(String username)
    {
        driver.findElement(usernameLocator).sendKeys(username);
    }
}

答案 1 :(得分:0)

您可以在代码中使用Try-Catch块Verify_Login方法

 @Test (priority=1)
 public static void Verify_Login() {
// Verify Logging in with right credentials
   try{
        //YOUR CODE
    }
   catch(NoSuchElementException nsee) {
        System.out.println("You are getting No Such Element Exception");          
        //Perform What you want if exception happens
    }
   catch(Exception e) {
        System.out.println("You are getting other Exception");          
        //Perform What you want if any other exception happens (Except NoSuchElementException )
    }
}

答案 2 :(得分:0)

如果你正在使用try catch,你可以像这样返回null:

public static WebElement Textbox_UserName (WebDriver driver){   
        WebElement element = null;
        try {
            element = driver.findElement(By.xpath(".//div[@class='username']"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return element;
}

然后

 if (Login_Page.TextBox_UserName(driver)!=null){
      //PASS
 }else{
      //FAIL
 }

您应该使用WebDriverWait来避免在浏览器加载时发现错误。

public static WebElement Textbox_UserName (WebDriver driver){   
     WebDriverWait wait = (new WebDriverWait(driver, 10);           
     WebElement element = null;

     try {
         wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//div[@class='username']"));
         element = driver.findElement(By.xpath(".//div[@class='username']"));
     } catch (Exception e) {
         e.printStackTrace();
     }
     return element;
}