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

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

  3. Ankita says:

    Hi,

    Great Post. I wanted to know if it is possible to override the default Click(). I dont want to update my tests with a newly created method. I want that my method be called if I call the default Click().

    How can I do that?

  4. Bilal Ahmed says:

    Hi Karthik!

    I really appreciate your efforts for being helpful to all who just get started with selenium C#. I followed your tutorials and start automating my web application which is working perfect so far.
    Now I am facing a problem in handling dynamic table elements. Let me clearify it, there is a button and clicking on button dynamically creates a new table row having drop downs and input fields. I want to access input fields under each column and enter some text over there. How can i do that in c#
    Thanks in advance.

Leave a Reply

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