Extension Methods

Due to the way we structure our framework, we can create extensions 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 in the ‘WebElementExtensions.cs’ class file with ‘We’ (for WebElement).

Before we begin, make sure to make the WebElementExtensions class ‘public static’.

WeHighlightElement() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for making the WebDriver wait for a specified number of seconds to use JavaScript to highlight a given element with an appropriate border…

    Let’s also add the javascript for ‘WeHighlightedColour’ in the ‘Settings.cs’ class file of the ‘Utils.Selenium’ namespace (of course you can also customise this to change the colour and size of the border etc.)…

    The whole Settings class should look like below at this point…

WeElementIsEnabled() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for making the WebDriver wait for a specified number of seconds to see if an element is enabled…

    The whole thing should look like below now…

WeSelectDropdownOptionByIndex() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for selecting a dropdown option based on its index position, if that element is enabled…

WeSelectDropdownOptionByText() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for selecting a dropdown option based on the string/text of that option, again if that element is enabled…

WeSelectDropdownOptionByValue() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for selecting a dropdown option based on the value of that option, again if that element is enabled…

WeGetAttribute() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for getting the value of a specified attribute on an element…

WeElementIsDisplayed() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for making the WebDriver wait for a specified number of seconds to see if an element is displayed…

WeSendKeys() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for sending keys / a given string to an IWebElement (and clearing the IWebElement first of existing text if necessary), after first waiting to see if the element is displayed…

WeElementToBeClickable() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for making the WebDriver wait a set amount of time to see if an element is clickable…

WeClick() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for clicking on an element, after having first waited for the element to be clickable and then highlighted the element with a border using JavaScript…

WeSwitchTo() Extension Method

  1. Open up the ‘WebElementExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for switching focus to a different iFrame, after having first waited for the iFrame to be clickable and having highlighted it with a border…

The whole thing should look like below…

WebDriverExtensions

We will begin each method name in the ‘WebDriverExtensions.cs’ class file with ‘Wd’ (for WebDriver).

Again, let’s make the WebDriverExtensions class ‘public static’ first.

WdHighlight() Extension Method

  1. Open up the ‘WebDriverExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for using WebDriver’s ‘By’ locator to highlight a located element…

WdFindElement() Extension Method

  1. Open up the ‘WebDriverExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for making WebDriver wait a set amount of time to find a located element…

    Ensure the correct ‘using’ Directives have been added again after this 2nd method…

WdSendKeys() Extension Method

  1. Open up the ‘WebDriverExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for sending keys / a string to a found element…

WdClickByIndex() Extension Method

  1. Open up the ‘WebDriverExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for clicking an element within a list of elements, based on its index position…

WdClick() Extension Method

  1. Open up the ‘WebDriverExtensions.cs’ class file in the ‘Utils.Extensions’ namespace and add the following extension method for clicking on an element, after having found it…

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. Edit the ‘SearchFor()’ method in the BasePage.cs file so it uses the WdSendKeys() extension method, with the whole thing looking like below…

‘When they view the first result’ assertion

  1. Open up the ‘RedditSteps.cs’ class file and edit the assertion in the ‘When they view the first result’ step so it uses the ‘WeElementIsDisplayed()’ extension method from the ‘WebElementExtensions’ class (make sure the using directive for WebElementExtensions is added), with the whole thing now looking like below…

‘SelectFirstListedSearchResult()’ method

  1. Within the ‘SearchResultsPage.cs’ class file, edit the ‘SelectFirstListedSearchResult()’ method so it uses the ‘WdClickByIndex()’ extension method…

‘Then they see the Reddit home page’ assertion

  1. Open up the ‘RedditSteps.cs’ file and edit the assertion in the ‘Then they see the Reddit homepage’ step so it uses the ‘WeElementIsDisplayed()’ extension method…

Run the test scenario and it should now pass. We can run our tests using the command…

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

Previous 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.