Extensions (Static Methods)

Due to the way we structure our framework, we can create static methods that we can use as extensions 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 Extensions

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

weWaitForSeconds() Static Method

  1. Open up the ‘WebElementExtensions’ class in the ‘utils.extensions’ package and add the following static 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 a static 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() Static Method

  1. Add the following static method in the ‘WebElementExtensions’ 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() Static Method

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

weHighlightElement() Static Method

  1. Add the following static method in the ‘WebElementExtensions’ 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 ‘WebElementExtensions’ class and add a static import for ‘weHighlightedColour’ in the ‘Settings’ class

weClick() Static Method

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

weSendKeys() Static Method

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

weElementIsInvisible() Static Method

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

weGetAttribute() Static Method

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

WebDriver Extensions

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

wdHighlight() Static Method

  1. Open up the ‘WebDriverExtensions’ class in the ‘utils.extensions’ package and add the following static 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 ‘WebDriverExtensions’ 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() Static Method

  1. Open up the ‘WebDriverExtensions’ class in the ‘utils.extensions’ package and add the following static 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 a static 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() Static Method

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

wdFindElement() Static Method

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

wdClick() Static Method

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

wdSendKeys() Static Method

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

Refactoring our Test Scenario to use the Extensions

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 🙂

‘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() static method from the ‘WebElementExtensions’ class (you will have to add a static import to access all the static methods)…

‘assertSearchResultsDisplayed()’ method

  1. Open up the ‘SearchResultsPage’ class and edit the method to use the weElementIsDisplayed() static method from the ‘WebElementExtensions’ class (you will have to add a static import to access all the static methods)…

‘selectFirstListedSearchResult()’ method

  1. Open up the ‘SearchResultsPage’ class again
  2. Edit the ‘selectFirstListedSearchResult()’ method so it uses the weClick() static method (which already has an explicit wait in it, up to 10 seconds) from the ‘WebElementExtensions’ 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!

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.