Refactoring Custom methods of control libraries

In last two post we discussed creating custom methods for Setting values to the UI control and Getting values out from the UI control. The code we wrote was fine, but there were lot of redundant and not so necessary parameters passed in each and every methods, in this post we will try to refactor our code a little bit to to have

  • Reduced number of parameters
  • Strongly-typed parameters
  • More reusable

Here is the complete video of the above discussion

Here is the complete modified code discussed in the video

First try to create a class called PropertiesCollection and create an auto-implemented property for WebDriver as shown below

And assign the ChromeDriver to the PropertiesCollection.driver property, then instead of IWebDriver local instance variable on each method (Since those are also passed as parameters) , try to use the PropertyCollection.driver property.

Set Methods for controls like Textbox, Button, DropDownList box.

Get Methods for controls like Textbox and DropDownList box

Thanks for watching the video and reading the post!!

Please leave your comments and let me know if there is anything need to be improved in the post!!!

Karthik KK


  1. Shailaja says:

    Error 1: Inconsistent accessibility: parameter type ‘UnitTestProject3.PropertyType’ is less accessible than method ‘UnitTestProject3.seleniumSetMethods.EnterText(string, string, UnitTestProject3.PropertyType)’
    this error comes when i run my task..!! Please reply for solution..

    • Karthik kk says:

      I think your enum is within the SeleniumSetMethods class, make sure you copy paste the enum out from the class and make it as public so that it can be accessible.

      Karthik KK

  2. Rams says:

    Nice work Karthi and thanks for the post.

    From the dropdown when I selected the value, the program selected the correct value for the app but Console Write its display the First 2 characters only.

    return new SelectElement(PropertiesCollection.driver.FindElement(By.Name(element))).AllSelectedOptions.SingleOrDefault().Text;

    • Karthik kk says:

      Thanks Ram!

      I guess the code is very specific to get a single value.

      Using SingleOrDefault() method will always get only ONE value, its used mainly to not through any null reference exception.

      To get all value you can modify your code like this


      Karthik KK

Leave a Reply

Your email address will not be published. Required fields are marked *