简体   繁体   English

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

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

In my application when user login first time then on first page username is displayed (data driven approach using @factory). 在我的应用程序中,当用户首次登录时,然后在首页上显示用户名(使用@factory的数据驱动方法)。 But if user logs out and log in again then a new pages comes with following text. 但是,如果用户注销并再次登录,则新页面将带有以下文本。

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

My question is how to check if this text -'Click Here' present then click on it and do same actions as mentioned in login function. 我的问题是如何检查是否显示此文本-'Click Here',然后单击它并执行登录功能中提到的相同操作。

I tried to implement if-else block to check if this webelement is displayed then click on it and do same action as in login function. 我尝试实现if-else块,以检查是否显示了该webelement,然后单击它并执行与登录功能相同的操作。 But it is giving error that 但这给了错误

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

Though I'm successfully able to achieve my result by specifying this element to click in Logout function. 尽管我可以通过指定此元素以单击注销功能来成功实现我的结果。 But when finally my test gets finished it always clicks on it. 但是,当我的测试最终完成时,它总是会点击它。

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

//function to check //检查功能

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

Please suggest a solution to check in login function everytime. 请提出每次登录功能的解决方案。

clickHere.isDisplayed() is giving NoSuchElementException as the element is not present on the UI on which you are trying to find it. clickHere.isDisplayed()给出NoSuchElementException因为您要在其上查找的UI上不存在该元素。
So, to solve your problem you can fetch the list of the element through pagefactory and then can find the size of that list, if the size is greater than 0, it means the element is present on the page else the element is not present. 因此,要解决您的问题,您可以通过pagefactory获取该元素的列表,然后找到该列表的大小,如果该大小大于0,则表示该元素存在于页面上,否则该元素不存在。

You need to make the following changes in your code and it would work fine then: 您需要在代码中进行以下更改,然后可以正常工作:
You need to fetch the element list using: 您需要使用以下方法获取元素列表:

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

And make the following changes in your code: 并在代码中进行以下更改:

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

Replace the (clickHere.isDisplayed()) with below. (clickHere.isDisplayed())替换为以下内容。

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

if you want to stick to your pagefactory then you can use the below approach 如果您想坚持自己的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");

if you would like to catch only NoSuchElementPresent exception you can update to catch only that. 如果您只想捕获NoSuchElementPresent异常,则可以更新以仅捕获该异常。

You can create BaseClass for you PageObject and impelent metchod isElementOnPage 您可以为您的PageObject和Baselet创建isClassOnPage的BaseClass

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

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

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

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