Using C# Extension methods for Selenium

In the last post we discussed working with custom library methods customization, we optimized the library code a lot, but still using C# Extension methods, we can optimize the code way beyond the way it look like earlier while we started discussing writing a custom library method post here

C# Extension methods

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Our extension method hooks to IWebElement type and will look like this

Here is the complete video of the above discussion

Here is the complete code from the above video.

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!!!

Thanks,
Karthik KK

4 comments

  1. monica says:

    Hi

    This is very good explanation for C# code. Do you have videos to refactor code in java.

    Thank you so much for your help !

    Monica

  2. Oliver Ferguson says:

    Hi

    After completing and running the code for part 11, I am receiving the following error message: OpenQA.Selenium.NoSuchElementException:could not find element:By.Name:Initial.

    Are you able to resolve. And also how do you retrieve values using the Selenium.Get method.

    • Anil says:

      I also had same issue. So, I used button.Submit() instead of button.Click() inside the method Clicks…and this resolved the issue.

Leave a Reply

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