WebDriver: How to check if an page object web element exists?

后端 未结 8 1571
鱼传尺愫
鱼传尺愫 2020-12-25 14:13

How to check if an Element exists, when using Page Objects with webdriver.

So far I am doing it this way.

DefaultPage defaultPage = PageFactory.initE         


        
相关标签:
8条回答
  • 2020-12-25 14:19

    I'm using this pattern, works fine for me:

    public void login() 
    {
        if (!loginButton.isDisplayed())
        {
            throw new IllegalStateException("Login button is not displayed!");
        } else
        {
            loginButton.click();    
        }        
    }
    

    or:

    public boolean loginButtinIsDisplayed() {
        try {
            this.loginButton.getTagName();
            return true;
        } catch (NoSuchElementException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-25 14:21

    The problem is the pattern itself. It uses @FindBy annotation (used by PageFactory to init the fields that must be wrapped by Proxy) that replaces the standard elements with their proxy instances which contain InvocationHandler.

    Each time you try to access a field, annotated with @FindBy, the invocation handler tries to find the element using the default ElementLocator.The problem is that the ElementLocator.findElement() method throws an TimeoutException / NoSuchElementException if there are no elements presented in the DOM.

    public WebElement findElement(SearchContext context) {
       List<WebElement> allElements = findElements(context);
       if (allElements == null || allElements.isEmpty())
          throw new NoSuchElementException("Cannot locate an element using "
          + toString());
       return allElements.get(0);
    }
    

    Therefore, each time you need to check whether an element is displayed or not you have to search for a List of elements and check its size.

    @FindBy(css = "div.custom")
    private List<WebElement> elements
    ...
    
    public isElementPresented(){
       return elements != null && elements.size > 0
    }
    

    Another way to solve this problem is to create your own implementation of LocatingElementHandler and ElementLocator

    So, if you need your own isDisplayed() method to return false instead of Exception, you have to replace the findElement() method in ElementLocator with something like that:

    ...
    List<WebElement> elements = searchContext.findElements(by)
    if(elements != null && elements.size() > 0){
       List<WebElement> visibleElements = []
       elements.each {
          if(it.displayed){
             visibleElements.add(it)
          }
       }
       if(visibleElements.size() > 0){
          return visibleElements.get(0)
       }
    }
    return null
    ...
    

    And add new conditions to LocatingElementHandler.invoke()

    Something like:

    element = locator.findElement()
    if(element == null){
       if(method.name == "isDisplayed"){
          return false
       }
    }
    
    0 讨论(0)
  • 2020-12-25 14:22

    @Ralph: I do it the same way: try/catch. I've never found another way. You could swap out the try/catch block in a super class and design it generic. In other words: You could write a method which expects an object of type WebElement. This method contains the try/catch block and return true/false...

    So I wrote the following public method in the test framework's super class and am now able to use it in every page object:

    public boolean isElementExisting(WebElement we) {
        try {
            we.isDisplayed();
            return true;
        } catch(NoSuchElementException e) {
            LOGGER.severe("Element does not exist.");
            return false;
        }
    }
    

    I don't know why this is not implemented in WebDriver...

    Otherwise you could use WebDriverWait.

    0 讨论(0)
  • 2020-12-25 14:29

    try this is defiantly work in pom

    public boolean isPrebuiltTestButtonVisible() {
        try {
    
            if (preBuiltTestButton.isEnabled()) {
    
                return true;
    
            } else {
    
                return false;
            }
    
        } catch (Exception e) {
    
            e.printStackTrace();
            return false;
        }
    }
    

    this will definitely work in page object model surround with try catch

    0 讨论(0)
  • 2020-12-25 14:38

    Arquillian has implemented that feature in Graphene extension.

    Check ElementLocatorConditionFactory.isPresent() function.

    They more or less do what you wrote in your question (from ExpectedConditions.findElement in selenium-support.jar) :

    try {
        return driver.findElement(by);
    } catch (NoSuchElementException e) {
        throw e;
    } catch (WebDriverException e) {
        // [...] some log
        throw e;
    }
    
    0 讨论(0)
  • 2020-12-25 14:40

    Using C# bindings:

    using System.Collections.Generic;
    using System.Linq;
    
    public class DefaultPage 
    {
        [FindsBy(How = How.Id, Using = "link_i_user_create")]
        private IList<IWebElement> userCreateMenuLink;
    
        public bool isUserCreateMenuLinkPresent()
        {
            return userCreateMenuLink.Any();
        }
    }
    

    You're telling Selenium to grab all elements that match that Id and put them into a List of IWebElement. You then call .Any() on the list which evaluates to true if at least one IWebElement was found.

    0 讨论(0)
提交回复
热议问题