Extensions (Static 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.)…

    Ensure the correct ‘using’ Directives have been added (Of course, ‘ProjectName’ is used in place for the name of your project)…

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…

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

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…

WeElementToBeInvisible() 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 no longer visible…

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…

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…

    Let’s also add the javascript for ‘WdHighlightedColour’ 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.)…

    Ensure the correct ‘using’ Directives have been added (Of course, ‘ProjectName’ is used in place for the name of your project)…

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. Open up the ‘SearchScenarios.feature’ file, right-click on the ‘When I search for “Reddit”‘ step, and select ‘Go To Definition’
  2. Right-click on the ‘.SearchFor()’ method call and select ‘Go To Definition’ again
  3. Edit the ‘SearchFor()’ method so it uses the WeSendKeys() extension method from the ‘WebElementExtensions’ class (make sure the using directive for WebElementExtensions class is added)…

‘AssertSearchResultsDisplayed()’ method

  1. Open up the ‘SearchResultsPage.cs’ class file and edit the ‘AssertSearchResultsDisplayed()’ method so it uses the ‘WeElementIsDisplayed()’ extension method from the ‘WebElementExtensions’ class (make sure the using directive for WebElementExtensions is added)…

‘SelectFirstListedSearchResult()’ method

  1. Within the ‘SearchResultsPage.cs’ class file again, edit the ‘SelectFirstListedSearchResult()’ method so it uses the ‘WeClick()’ extension method from the ‘WebElementExtensions’ class (make sure the using directive for WebElementExtensions is added)…

‘AssertIAmOnRedditPage()’ method

  1. Open up the ‘RedditPage.cs’ class file and edit the ‘AssertIAmOnRedditPage()’ method so it uses the ‘WeElementIsDisplayed()’ extension method in its first assertion, from the ‘WebElementExtensions’ class (make sure the using directive for WebElementExtensions is added)…
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.