Home > front end >  Is it better to use WebElement.isDisplayed() or WebElement.getText() and compare the results?
Is it better to use WebElement.isDisplayed() or WebElement.getText() and compare the results?

Time:01-18

If I'm trying to see if a field on a web page is displaying some text, and I have the following function to dynamically do this:

public boolean isInformationFound(String info){
By infoText= By.xpath("//h5[text()='The info is:']/following::td[text()[contains(.,'" info "')]]");
        return findElementBy(infoText).isDisplayed();
}

and in my test case:

Assert.assertEquals(foo.isInformationFound("hello"), true)

alternatively, what I could do is:

public String getInfo(){
By infoText= By.xpath("//h5[text()='The info is:']/following::td");
        return findElementBy(infoText).getText();
}

and then in my test case function, I would do something like:

String expected info = "hello"
Assert.assertEquals(info, foo.getInfo())

Which method is better practice?

CodePudding user response:

To assert text results, we must use getText(). The explanation is as follows:

There are 3 methods in Selenium to check the visibility scope of an element

  1. isEnabled
  2. isDisplayed
  3. isSelected

At times our tests fail because an element is displayed or exists in DOM, but it does not exist on the web page. So, we handle such cases by checking the visibility of the web element.

WebDriver already has a predefined getText() method, which retrieves either the textContent (for Mozilla) or the innerText (for Chrome etc) of the element.

Your case involves text verification so it's better to go for getText. For web element visibility verification you can use isDisplayed.

CodePudding user response:

WebElement.isDisplayed()

Probes if the element displayed or not within the HTML DOM.

From the specifications:

The visibility of an element is guided by what is perceptually visible to the human eye. In this context, an element’s displayedness does not relate to the visibility or display style properties.

The approach recommended to implementors to ascertain an element’s visibility was originally developed by the Selenium project, and is based on crude approximations about an element's nature and relationship in the tree. An element is in general to be considered visible if any part of it is drawn on the canvas within the boundaries of the viewport.

The element displayed algorithm is a boolean state where true signifies that the element is displayed and false signifies that the element is not displayed. To compute the state on element, invoke the Function.[[Call]](null, element, false), with bot.dom.isShown as the this value. If doing so does not produce an error, return the return value from this function call. Otherwise return an error with error code unknown error.

This function is typically exposed to GET requests with a URI Template of

/session/{session id}/element/{element id}/displayed.

WebElement.getText()

Gets the visible (not hidden by CSS) text of this element, including sub-elements.

From the specifications:

The Get Element Text command intends to return an element’s text “as rendered”. An element’s rendered text is also used for locating a elements by their link text and partial link text.

One of the major inputs to this specification was the open source Selenium project. This was in wide-spread use before this specification written, and so had set user expectations of how the Get Element Text command should work. As such, the approach presented here is known to be flawed, but provides the best compatibility with existing users.


Conclusion

Now usage of either of the methods depends on the requirement as well as on the structure of the framework, perhaps individual choice, as isDisplayed() return a boolean and getText() returns the text.

  • Related