Working with WebElement
Now that we know how to locate the element from the website and extract them as WebElement, let us dive into some useful methods that we can use to simulate the user element interactions on the website.
We will cover following topic in this code note.
- WebElement Methods
› WebElement Methods¶
We will cover 9 essential WebElement methods as we covered in the lecture. For a complete list of methods, please refer to the Java documentation (link).
To shorten the length of each code sample, we will assume that the following driver initiating code is already typed and ready before each of the codes.
📓String getText( )
This method extracts the element's text content and returns it as String data.
📓void click()
This method invokes the click action on the element if it's a clickable element like a button or link.
📓void sendKeys(String text)
This method inserts string to the element if the element accepts text input from the user.
|
📓void clear()
This method clears out any texts ( if present ) from the element that accepts text inputs. We will use this method to clear out texts from email & password inputs, as well as text areas.
|
📓boolean isDisplayed()
This method is used to verify that the element is visible to the user. It will return true boolean value if the element is visible, false boolean value if the element is not visible.
📓boolean isEnabled()
This method is used to determine whether an element is interactable to the user. If the element is interactable, it will return true value, if the element is not interactable and disabled the false value will be returned.
📓boolean isSelected()
This method is used to determine whether an element is in the state of checked or selected by the user. You can use this method for a checkbox or radio button on a website right after you have checked them to see if its state has changed to selected.
📓String getAttribute(String name)
An attribute is a key and value pair that helps the web developer to give identification, name, or other information to the element they are programming. Occasionally, it is necessary for a tester to print out the attribute value of the element as part of their test case.
All the highlighted parts of the <input> element is an attributes and the blue text enclosed in a double quotation is the attribute value. So this <input> element has over 12 attributes.
📓 String getTagName()
This method will give us the element's tag name as a String data. Tag is responsible for everything you see on the website. Picture is rendered by <img> tag, and button is displayed by <button> tag etc. Sometimes, we need to get a tag name and display them in our test.
As you can see, the highlighted [Sign Up] button is rendered by the <button> tag.