An interesting solution for Selenium using generics in C#

Recently a colleague approached me with a solution to handle a generic component used across a system that the company I work for produces.

The class he wrote is as follows

This class does achieve what he needed it to do. You can call a method to perform an action (in the example click a save button somewhere). However it breaks any use of the Navigation Pattern in Selenium and requires parsing a string to return the object you want back. This then needs to be cast into the correct page object. Annoying…

I suggested the following approach

This class allows to retain the navigator pattern as you can see in the test example below. In addition to this we are no longer parsing a string to create a return object and then casting it.

The class gives us two options. You can pass in the object you want return when instantiating an object from it or on each method.

Pretty nifty…..