如何验证登录模块中不同页面上的特定 Web 元素

How to verify specific web element on different page in login module

在我的应用程序中,当用户首次登录然后在第一页上显示用户名(使用@factory 的数据驱动方法)。但是,如果用户注销并再次登录,则会出现一个带有以下文本的新页面。

You're signed out now.
Click here to sign in again.

我的问题是如何检查此文本 -'Click Here' 是否存在,然后单击它并执行与登录功能中提到的相同的操作。

我尝试实现 if-else 块来检查是否显示此网络元素,然后单击它并执行与登录功能相同的操作。但它给出了

的错误
org.openqa.selenium.NoSuchElementException: Cannot locate an element using xpath=//a[@href='/Account/Login']
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html

虽然我通过指定此元素以单击注销功能成功地实现了我的结果。但是当我的测试最终完成时,它总是点击它。

 @FindBy(xpath="//a[@href='/Account/Login']")
WebElement clickHere;

//要检查的函数

if (clickHere.isDisplayed())
      {   
          clickHere.click();
          username.sendKeys(strUsername);
          nextBtn.click();
          password.sendKeys(strPassword);
          loginButton.click();
          System.out.println("Successfully Logged");
      }
      else
          {

          username.sendKeys(strUsername);
          nextBtn.click();
          password.sendKeys(strPassword);
          loginButton.click();
          System.out.println("Successfully Logged");
          }

请提出一个每次登录功能都签到的解决方案。

(clickHere.isDisplayed()) 替换为以下内容。

if(driver.findElements(By.xpath("//a[@href='/Account/Login']") ).size() != 0)

如果你想坚持你的 pagefactory 那么你可以使用下面的方法

// below line will click on the "Click Here" link if only it's present
try {clickHere.click();}catch(Exception e) {}
username.sendKeys(strUsername);
nextBtn.click();
password.sendKeys(strPassword);
loginButton.click();
System.out.println("Successfully Logged");

如果您只想捕获 NoSuchElementPresent 异常,您可以更新以仅捕获该异常。

clickHere.isDisplayed() 给出 NoSuchElementException,因为该元素不存在于您试图找到它的 UI 上。
因此,要解决您的问题,您可以通过 pagefactory 获取元素列表,然后可以找到该列表的大小,如果大小大于 0,则表示该元素存在于页面上,否则该元素不存在。

您需要在代码中进行以下更改,然后才能正常工作:
您需要使用以下方式获取元素列表:

@FindAllBy(xpath="//a[@href='/Account/Login']")
List<WebElement> clickHere;

并在您的代码中进行以下更改:

if (clickHere.size()>0){   
      clickHere.get(0).click();
      username.sendKeys(strUsername);
      nextBtn.click();
      password.sendKeys(strPassword);
      loginButton.click();
      System.out.println("Successfully Logged");
 }
 else{
      username.sendKeys(strUsername);
      nextBtn.click();
      password.sendKeys(strPassword);
      loginButton.click();
      System.out.println("Successfully Logged");
 }

您可以为您的页面对象创建 BaseClass 并实现方法 isElementOnPage

基础class:

public class BasePage {

    private WebDriver driver;

    public BasePage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    protected boolean isElementOnPage(WebElement webElement) {
        try {
            webElement.getTagName();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

你的Class:

public class PageClass extends BasePage {

    @FindBy(xpath="//a[@href='/Account/Login']")
    private WebElement clickHere;

    public PageClass(WebDriver driver) {
        super(driver);
    }

    public PageClass YourMethod(){
        if(isElementOnPage(clickHere)){
            clickHere.click();
            // your logic here
        }else {
            // your logic here
        }

        return this;
    }
}