The framework is a software that will be used for scripting. It will be used in a completely separate solution and project. When developing a framework, a question to keep in mind is “can a non-technical person write scripts against the framework?”. Consider, a complex web application, which requires over a thousand test scripts to be written. Also, Imagine these test scripts rely on hundreds of identified page objects represented as concrete classes. I am sure you will agree it would be quite difficult for an Automation tester or anyone trying to script, to keep track of relevant classes to instantiate (Even with proper naming). Furthermore, you will have the ‘new’ keyword all over the scripts. As a rule of thumb;

  • If they have to ask you which classes they need to instantiate in the script to use the framework, then, the framework is hard to use.
  • Situation where Automation tester is using for loops or having to do any fancy coding stuff in Test scripts, then your framework is too complicated
  • If they are using advanced OOP concepts in scripts?  e.g. Generics in c#, then the framework is not serving its purpose.

Agenda:

  • Why ‘new keyword’ is ugly in Test Scripts
  • Hide Creation logic of our instantiated page object class
  • Delegating Web Element initialization to a different class

Pre-requisite/ Tools:

  • Previous Posts
  • Intermediate to advance C#/ OOP concept and knowledge
  • The Application Under Test on Instant WordPress

Hide Creation logic of our instantiated page object class:

For the moment we are going to look at a simple solution to this ‘new’ problem to get us going. I will still cover how we can refactor for extensibility in later posts. Even though i said simple, to be honest, this will have a huge impact and revolutionize how your Test scripts are written. This idea is borrowed from the Creational pattern, Factory pattern; however, our current situation does not satisfy a situation to use it. We will still leverage the idea however using concrete classes.

Let’s flip to visual studio

In the Web project, Create a new class. I will call mine to reflect the Organization and platform of the application being tested.  TatWeb.cs.

Description

  • Add relevant namespaces, TatAutomationFramework.Web.Pages.
  • Declare a static public property of the type of our page object HeaderPObjectTwo and name it HeaderPage.  (concrete type for now).
  • Then we return an instance of the class.

Usage

  • Open the HeaderFunctionTests.cs class in the. Test Project.
  • Let’s comment out or delete our previous tests containing the ‘new’ keyword.
  • Then we declare 2 methods as shown.
  • Ensure the AUT is Running by starting up instant WordPress
Start Instant wordPress
  • When you run the tests they should just pass like previous tests.

Test Scripts Execution_Factory class

Evaluation

As we can see, we no longer create object in our scripts. We have leverage intellisense in our IDE, to display available page/s that the Test Script writer can use.

Test Scripts Intellisense

Now, instead of remembering various classes, all they need to remember is to start every script from ‘TatWeb’.  and they will technically be prompted to select which page they want to script against. As you can see, even a non-technical colleague can write scripts against the AUT using the framework.

Delegating Web element initialization to a different class:

To conclude this post, we will finally take out the initialization of elements from our Page Objects. This will make it look cleaner and also give it less responsibility so to say.

  • In the Web Project create a new class call it DoInitialize.cs (or anything).

Description

Inport relevant namespaces i.e. OpenQA.Selenium and OpenQA.Selenium.Support.PageObjects

  • This Class requires the instance of webdriver
  • We have declared a generic method which returns a type with parameter-less constructor
  • The class is then passed in, with the driver instance as parameter in the page factory method ‘InitElement’
  • The instance of Class with initialized Webelements is returned.

Usage

  • Firstly, let’s go to the page object in question Pages/HeaderPObjectTwo.cs in the .Web project and remove the extra initialization code in the constructor
  • Then we want to go to our TatWeb class and modify the declared property to return an instance of the HeaderPObjectTwo class with the elements instantiated.
  • Run the Test and ensure it passes.
Test Scripts Execution_Generics
Test Scripts Execution_Generics

Evaluation

As you can see, all we are left with is just Page element properties and the functions in the PO class.

This final modification we have done, made our page object look cleaner. Most importantly, the scripts run exactly the same with no modification needed. Finally, remember, the script solution will be completely separate from the framework solution, as this will be on individual project level.

Next we will Do more automation, create more Page Objects and Discuss leveraging Method chaining to make writing scripts easier and fluent.

Completed Files:

Click Here to Download the Complete Working Solution.

Click here to 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!

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