Helper Methods

Due to the way we structure our framework, we can create static methods that we can use as helpers on methods involving different object types, such as clicking on a WebElement, explicitly waiting for a WebDriver, or waiting for a WebElement to be displayed or clickable etc.

WebElement Helpers

We will begin each method name with ‘we’ (for WebElement).

weWaitForSeconds() method

  1. Open up the ‘WebElementHelpers’ class in the ‘utils.helpers’ package and add the following method for making the WebDriver wait for a specified number of seconds…
  2. Make sure the import for ‘WebDriverWait’ and the static import for browser() method have been added above the class…
  3. Add an integer variable set to a value of 10 for ‘sec’, and make it private to the class it is in, just above the weWaitForSeconds() method…

weElementIsDisplayed() method

  1. Add the following method in the ‘WebElementHelpers’ class to check that a given WebElement is displayed, after waiting the specified amount of time from the weWaitForSeconds() method …
  2. Also make sure the import for ‘ExpectedConditions’ is added above the class, as well as an import for ‘WebElement’…

weElementToBeClickable() method

  1. Add the following method in the ‘WebElementHelpers’ class to check that a given WebElement is clickable, after waiting the specified amount of time from the weWaitForSeconds() method…

weHighlightElement() method

  1. Add the following method in the ‘WebElementHelpers’ class to highlight a given WebElement using JavaScript…
  2. Also make sure that an import for JavascriptExecutor has been added above the class…
  3. You will notice that an error is thrown due to not knowing what ‘weHighlightedColour’ is, so let’s add that in the ‘Settings’ class of the ‘utils.selenium’ package (of course you can also customise this to change the colour and size of the border etc.)…
  4. Finally, go back to the ‘WebElementHelpers’ class and add a static import for ‘weHighlightedColour’ in the ‘Settings’ class

weClick() method

  1. Add the following method in the ‘WebElementHelpers’ class to wait for a given WebElement to be clickable, highlight the clickable WebElement, and then click the WebElement…

weSendKeys() method

  1. Add the following method in the ‘WebElementHelpers’ class to wait for a given text field WebElement to be displayed, clear the text field if necessary (if ‘clearFirst’ is true), and then input a given String into the text field…

weElementIsInvisible() method

  1. Add the following static method in the ‘WebElementHelpers’ class to wait for a given WebElement to not be visible anymore…

weGetAttribute() Static Method

  1. Add the following static method in the ‘WebElementHelpers’ class to find a specified attribute of a given element…

WebDriver Helpers

We will begin each method name with ‘wd’ (for WebDriver).

wdHighlight() method

  1. Open up the ‘WebDriverHelpers’ class in the ‘utils.helpers’ package and add the following method for highlighting a given locator using JavaScript…
  2. You will notice that an error is thrown due to not knowing what ‘wdHighlightedColour’ is, so let’s add that in the ‘Settings’ class of the ‘utils.selenium’ package (of course you can also customise this to change the colour and size of the border etc.)…
  3. Next, go back to the ‘WebDriverHelpers’ class and add a static import for ‘wdHighlightedColour’ in the ‘Settings’ class…
  4. Of course, also check that the imports for ‘WebDriver’, ‘WebElement’, ‘JavaScriptExecutor’ and ‘By’ have been added above the class, as well as a static import again for the browser() method in the ‘Drivers’ class of the ‘utils.selenium’ package…

wdElementIsDisplayed() method

  1. Open up the ‘WebDriverHelpers’ class in the ‘utils.helpers’ package and add the following method for checking that a highlighted locator is displayed…
  2. Make sure the import for ‘WebDriverWait’ and ‘ExpectedConditions’ have been added above the class…
  3. Add an integer variable set to a value of 10 for ‘sec’, and make it private to the class it is in, just above the weWaitForSeconds() method…

wdElementIsInvisible() method

  1. Add the following method in the ‘WebDriverHelpers’ class to wait for a given locator to not be visible…

wdFindElement() method

  1. Add the following method in the ‘WebDriverHelpers’ class to find a displayed element by its locator…

wdClick() method

  1. Add the following method in the ‘WebDriverHelpers’ class to click on an element which has been found by its locator…

wdSendKeys() method

  1. Add the following method in the ‘WebDriverHelpers’ class to wait for a given text field element (which has been found by its locator) to be displayed, clear the text field if necessary (if ‘clearFirst’ is true), and then input a given String into the text field…

Refactoring our Test Scenario to use the Helpers

We can now use our extensions (static methods) to replace previous lines of code in our test methods.  Over time, this helps us follow DRY principles (Don’t Repeat Yourself) as well as make our code easier to read 🙂

Adding instances of the Helper classes to our Base page

  1. Open up the BasePage class and add the following  in the class to instantiate our Helper classes and allow all pages that inherit from the base page access to them too…

‘searchFor()’ method

  1. Open up the ‘SearchScenarios.feature’ file, right-click on the ‘When I search for “Reddit”‘ step, and select ‘Go To’ –> ‘Declaration’
  2. Right-click on the ‘.searchFor()’ method call and select ‘Go To’ –> ‘Declaration’ again
  3. Edit the ‘searchFor()’ method so it uses the weSendKeys() method from the ‘WebElementHelpers’ class…

‘assertSearchResultsDisplayed()’ method

  1. Open up the ‘SearchResultsPage’ class and edit the method to use the weElementIsDisplayed() method from the ‘WebElementHelpers’ class…

‘selectFirstListedSearchResult()’ method

  1. Open up the ‘SearchResultsPage’ class again
  2. Edit the ‘selectFirstListedSearchResult()’ method so it uses the weClick() method (which already has an explicit wait in it, up to 10 seconds) from the ‘WebElementHelpers’ class…

You can run your tests in CLI (command line interface) by entering
mvn test
or
gradle test
respectively.

Congratulations on completing this blog series.

Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!

Previous Article

Next Article

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.