Warning: fopen(/tmp/SghXTngBZPli-qfpy8x.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-qfpy8x.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
Custom methods for Controls in Selenium with C# (Part 1)
In this post we will discuss writing custom methods for the HTML controls of web pages using Selenium with C# like
Textbox control
Button control
Drop Down list
But in this post we will deal with Setoperation in the control like entering text, clicking button, selecting drop down etc. In next post, we will deal with Getoperations of controls.
Purpose
The real purpose of writing custom methods is to
Simplify the coding (clean and neat)
Reuse the code (reduce redundancy of same code)
Easy to maintain and modify
Here is the complete video for the above discussion.
Here is the complete code for each and every controls
Text box/Text Area Control
public static void EnterText(IWebDriver driver, string element, string value, string elementtype)
{
if (elementtype == "Id")
driver.FindElement(By.Id(element)).SendKeys(value);
if (elementtype == "Name")
driver.FindElement(By.Name(element)).SendKeys(value);
}
Button/Checkbox/Option Click
//Click into a button, Checkbox, option etc
public static void Click(IWebDriver driver, string element,string elementtype)
{
if (elementtype == "Id")
driver.FindElement(By.Id(element)).Click();
if (elementtype == "Name")
driver.FindElement(By.Name(element)).Click();
}
Drop Down list box
//Selecting a drop down control
public static void SelectDropDown(IWebDriver driver, string element, string value, string elementtype)
{
if (elementtype == "Id")
new SelectElement(driver.FindElement(By.Id(element))).SelectByText(value);
if (elementtype == "Name")
new SelectElement(driver.FindElement(By.Name(element))).SelectByText(value);
}
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
20 Replies to “Custom methods for Controls in Selenium with C# (Part 1)”
i work with webpages which sometimes do not have the attributes name / id for the element – is there an option to find the element based on the class name ?
Your test code with selenium should look like this, because this is the maintainable, neat and oop. Your approach leads to a function hell.
public class LoginPage
{
private IWebDriver webDriver;
[FindsBy(How = How.Id, Using = “PasswordID”)]
public IWebElement Password { get; set; }
[FindsBy(How = How.Id, Using =”cmdSubmitID”)]
public IWebElement SubmitButton { get; set; }
[FindsBy(How = How.Id, Using = “txtUserNameID”)]
public IWebElement UserName { get; set; }
public LoginPage() { }
public LoginPage(IWebDriver webDriver)
{
this.webDriver = webDriver;
if(!webDriver.Url.Contains(“Login.aspx”))
{
throw new StaleElementReferenceException(“This is not the login page”);
}
PageFactory.InitElements(webDriver, this);
}
public HomePage signIn(string username, string password)
{
UserName.SendKeys(username);
Password.SendKeys(password);
SubmitButton.Submit();
Also boolshit.. FindBy annotations – the most stupid improvement within Selenium WebDriver “DSL”.. Really – we can define findElement By.* type in one locator-string without destroying OOP concepts..
I really thanks for this new idea about reusable code.
When I implement it in my project, I have question, suppose if there is a change in element locater, thn we need to change all method name every where..
It is not good idea..
please guide me where even if the element locater, I need to update it in page object. rather then touching the script…
Thats not possible, since you are using C# properties with its name, if you change the property, then you must change everywhere, rather you can use refactor –> rename to rename all the referenced location, which should be easy !!!
love your training video. Thank you so much for all the tutorials. Do you have video/or can you do one video for Selenium in C# with jstree and drag/drop feature?
Hi There,
Thanks for the great videos. I am coming across an issue when I try to run this piece of code.
public void ExecuteTest()
{
//Login
SeleniumSetMethods.EnterText(“txtWebID”, “xxxxxxxxx”, “name”);
//Password
SeleniumSetMethods.EnterText( “txtPassword”, “xxxxxxxx”, “Name”);
//Click
SeleniumSetMethods.Click( “loginButton”, “Name”);
Console.WriteLine(“The value from Username is : ” + SeleniumGetMethods.GetText(“txtWebID”, “Name”));
Console.WriteLine(“The value from Password is : ” + SeleniumGetMethods.GetText(“txtPassword”, “Name”));
I keep getting an error message saying Unable to find element with name == txtPassword,
not sure why. Please help, I am new to selenium
Hi, Please help me with this issue I have. I’m running the same classes in my local and I’m getting this error message that the “OpenQA.Selenium.WebdriverException : no such session”. Have anyone got this issue? when using this awesome’s guy examples? Please help
i work with webpages which sometimes do not have the attributes name / id for the element – is there an option to find the element based on the class name ?
Yap, you have that option, there is a method in By class, which help you to identify element using Class name
Thanks,
Karthik KK
??? back to the 80th? Kill OOP?
Your test code with selenium should look like this, because this is the maintainable, neat and oop. Your approach leads to a function hell.
public class LoginPage
{
private IWebDriver webDriver;
[FindsBy(How = How.Id, Using = “PasswordID”)]
public IWebElement Password { get; set; }
[FindsBy(How = How.Id, Using =”cmdSubmitID”)]
public IWebElement SubmitButton { get; set; }
[FindsBy(How = How.Id, Using = “txtUserNameID”)]
public IWebElement UserName { get; set; }
public LoginPage() { }
public LoginPage(IWebDriver webDriver)
{
this.webDriver = webDriver;
if(!webDriver.Url.Contains(“Login.aspx”))
{
throw new StaleElementReferenceException(“This is not the login page”);
}
PageFactory.InitElements(webDriver, this);
}
public HomePage signIn(string username, string password)
{
UserName.SendKeys(username);
Password.SendKeys(password);
SubmitButton.Submit();
HomePage homePage = new HomePage(webDriver);
PageFactory.InitElements(webDriver, homePage);
return homePage;
}
}
And use method extensions if u want to add reusable comfy functions to an object.
http://stackoverflow.com/questions/8149808/whats-the-best-way-to-use-selenium-pageobject-design-pattern
Yes Zoltan, I have separate video of C# Extension methods for custom control. We will work on that as well.
This post is a part of the series.
Thanks,
Karthik KK
We have separate post for this covered in upcoming post of POM.
Thanks for bringing it up !!!
Thanks,
Karthik KK
Also boolshit.. FindBy annotations – the most stupid improvement within Selenium WebDriver “DSL”.. Really – we can define findElement By.* type in one locator-string without destroying OOP concepts..
I really thanks for this new idea about reusable code.
When I implement it in my project, I have question, suppose if there is a change in element locater, thn we need to change all method name every where..
It is not good idea..
please guide me where even if the element locater, I need to update it in page object. rather then touching the script…
Thats not possible, since you are using C# properties with its name, if you change the property, then you must change everywhere, rather you can use refactor –> rename to rename all the referenced location, which should be easy !!!
Hope it helps !!!
Thanks,
Karthik KK
I’m trying to select my drop down value by SelectByIndex (Custom Method). But Its not taking . please give me any Idea about this.
Thanks in Advance
Regards,
Thirupathi.
I think if you pass the index number, it should work !!!
Unable to find anything in test explorer.
love your training video. Thank you so much for all the tutorials. Do you have video/or can you do one video for Selenium in C# with jstree and drag/drop feature?
Thanks again
-Michael
You are welcome Michael, BTW I dont have any video for jstree, should make one if time permits me 🙂
Thanks,
Karthik KK
Hi There,
Thanks for the great videos. I am coming across an issue when I try to run this piece of code.
public void ExecuteTest()
{
//Login
SeleniumSetMethods.EnterText(“txtWebID”, “xxxxxxxxx”, “name”);
//Password
SeleniumSetMethods.EnterText( “txtPassword”, “xxxxxxxx”, “Name”);
//Click
SeleniumSetMethods.Click( “loginButton”, “Name”);
Console.WriteLine(“The value from Username is : ” + SeleniumGetMethods.GetText(“txtWebID”, “Name”));
Console.WriteLine(“The value from Password is : ” + SeleniumGetMethods.GetText(“txtPassword”, “Name”));
I keep getting an error message saying Unable to find element with name == txtPassword,
not sure why. Please help, I am new to selenium
Hi Kathik;
Some time we need to read a element inside a iframes , so in that case what we have to do?
You should switch to the iframe and perform operation, again it cannot sit on the controls method though.
Thanks,
Karthik KK
Hi, Please help me with this issue I have. I’m running the same classes in my local and I’m getting this error message that the “OpenQA.Selenium.WebdriverException : no such session”. Have anyone got this issue? when using this awesome’s guy examples? Please help