Still focusing on our goal to make scripting easier, another issue that could potentially prevent that goal is when script writers need to declare variables. This is not a huge issue but if dealt with, it can make scripting even fun. We will use a OOP concept called method chaining.

Agenda:

  • Prettier Scripts with Method Chaining, eliminate need for variables
  • APIs for the Menu Page Object

Pre-requisite/ Tools:

  • Visual Studio 2015
  • Previous Posts
  • Intermediate to advance C# knowledge/ OOP concept (classes, Methods, Properties)
  • The Application Under Test on Instant WordPress

Here is the Wikipedia definition.

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.

In the context of Test automation, method chaining will enable calls to methods in sequence, and it can make tests more easy to read and kind of pretty. Reference to the returned page Object, could be its containing class or other classes representing other page objects.

APIs for the Menu Page Object

Let’s develop the public methods for the Menu Page Object to explain this further.

From our previous model we have agreed that the menu section will be treated as a separate page object as shown.

Let’s note down some details.

page objects Aut

 

Page Objects Class Name Notes Functions
Menu Page Object MenuPObject.cs  At least 7 Page Objects Participants Displays Menu Items
Navigates to other pages

Description

  1. First thing is to create our Menu page object, in the Page folder of our .Web Project
  2. In the Menu page object, we import relevant namespaces
  3. We declare 7 properties to represent the links
  4. We create 7 methods that simply Navigates to our required pages. Note each method returns an instance of Page Objects with elements initialized using the DoInitialize.cs helper class.
  5. I have used PartialLinkText, as the locator, just so most people can follow. If following in a different language, I would suggest using other locators.
  6. Under each of the other destination classes, we will declare a single method to verify we are on the correct page. There are few ways to do this. 2 of which include;
    • We can get the browser title property after navigation through the driver instance – Demonstrated in the Support Page Object
    • We can verify that a unique element or element property on the page is displayed. – Demonstrated in the news Page Object
Method chaining - Test scripts
Method chaining – Test scripts

As I have always maintained, there are numerous ways a problem can be solved, however I would suggest to keep things simple, until it is required to refactor and complicate it further. Instead of doing anything fancy, we will declare 7 methods to navigate to each page.

Support Page Object Using the driver instance to get the browser title

News Page Object using a web element for verification

Under the helper class, TatWeb.cs, we have included additional property to return instance of the MenuPObject.

Usage

  1. In the Test Project, back to NavigationTests.cs, lets get rid of the previous method to navigate to google.
  2. Create two tests to verify News page and Support page navigation
  3. As usual, we start from TatWeb, select the required page (MenuPage) as you can see we have a list of all the methods available for navigation. Also after selecting the relevant page, we can call methods straight away from the page instance.
  4. Ensure Instant WordPress is running and run the tests.

Method chaining - Test scripts

Method chaining - Test scripts

Method chaining - Test scripts

Evaluation

We made each of the methods from our Menu Page object class, return other page object. This made it possible to call methods from the instances of these other pages. The chain ends where we get the data regarding our expected results and we make assertions. In this case, the text property of an element unique to the page or the title of the page.

It is usually suggested to navigate directly to the particular page we need to reach. But for the moment this is an easy way to demonstrate Method chaining usage. I will continue this demonstration with a scenario, where same action on an element could lead to different outcomes or need to return different page objects. Example is the Login Form.

Completed Files:

Click Here to Download the Complete Working Solution.

Make sure you subscribe below and be the first to be notified when this is published.

Any thoughts, questions, comments, addition, or anything you don’t like, do not hesitate to leave a comment or contact me. Thank you!

Further reading: wikipedia

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.

Show Buttons
Hide Buttons