Some common reusable methods

Now that we know most of the frequently used methods with their signatures, let's create a few common reusable methods. We will create methods for sendKeys, click, selectByVisibleText, getText, and isDisplayed. These methods have been named as enterText, clickElement, selectValue, getElementText, and isElementDisplayed respectively. I have taken a few representatives. As an exercise, you can create similar methods as needed. These are called wrapper methods. Here, I have done sampling to avoid repeating code. Once you understand a representative method, you can code on similar lines.

Listed below are few commonly used methods.

  • enterText(WebElement,String): This wrapper method enters text into textboxes or text areas. There are two arguments. One is of type WebElement and the other is of type String. The following code snippet shows the enterText method:
      public void enterText(WebElement element,String text) {                                        
element.sendKeys(text);
}
  • clickElement(WebElement): This method clicks on the web element passed as an argument. The following code snippet shows the clickElement method:
      public void clickElement(WebElement element) {
element.click();
}

  • selectValue(WebElement, String): This method selects the text passed as an argument from the options of a Select object passed as the first argument. The declaration of the Select object should be done outside the method. The following code snippet shows the selectValue method:
      Select select = null;
public void selectValue(WebElement element, String text) {
select = new Select(element);
select.selectByVisibleText(text);
}
  • getElementText(WebElement): This method returns the visible text (not hidden by CSS) of the element passed as a parameter. The following code snippet shows the getElementText method:
      public String getElementText(WebElement element) {
return element.getText();
}
  • isElementDisplayed(WebElement): This method returns true if the WebElement passed as a parameter is displayed on the screen. Notice that the method returns a Boolean. The following code snippet shows the isElementDisplayed method:
      public boolean isElementDisplayed(WebElement element) {
return element.isDisplayed();
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.174.174