Warning: fopen(/tmp/SghXTngBZPli-ZPxpT9.tmp): failed to open stream: Disk quota exceeded in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 139
Warning: unlink(/tmp/SghXTngBZPli-ZPxpT9.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
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.
public static void EnterText(this IWebElement element, string value)
{
element.SendKeys(value);
}
public static void Clicks(this IWebElement element)
{
element.Click();
}
public static void SelectDropDown(this IWebElement element, string value)
{
new SelectElement(element).SelectByText(value);
}
Get Methods for controls like Textbox and DropDownList box
public static string GetText(IWebElement element)
{
return element.GetAttribute("value");
}
public static string GetTextFromDDL(IWebElement element)
{
return new SelectElement(element).AllSelectedOptions.SingleOrDefault().Text;
}
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
Post Author:
Karthik kk
8 Replies to “Using C# Extension methods for Selenium”
Hi
This is very good explanation for C# code. Do you have videos to refactor code in java.
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.
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().
seems like its not possible:
an extension method never takes priority over an instance method with a suitable signature, and never participates in polymorphism (GetHashCode is a virtual method).
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.
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
great help to learn easily
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.
I also had same issue. So, I used button.Submit() instead of button.Click() inside the method Clicks…and this resolved the issue.
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?
https://stackoverflow.com/questions/899539/is-there-any-way-in-c-sharp-to-override-a-class-method-with-an-extension-method
seems like its not possible:
an extension method never takes priority over an instance method with a suitable signature, and never participates in polymorphism (GetHashCode is a virtual method).
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.
Hi Karthik,
Can you explain the same for radiobuttons and checkbox