Warning: fopen(/tmp/SghXTngBZPli-tG3WxL.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-tG3WxL.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 control in Selenium C# (Part 2)
In the last post of this series we discussed how to write custom methods for control which perform Set operations in UI controls, in this post we will write code which perform Get operations out from a control like getting text value from UI controls.
We will write code to Get values out from controls like
Text box
Drop Down listbox
Here is the video of the above discussion
Here is the complete code from above video
Getting value from Textbox
//Getting value out from Textbox
public static string GetText(IWebDriver driver, string element, PropertyType elementtype)
{
if (elementtype =="Id")
return driver.FindElement(By.Id(element)).GetAttribute("value");
if (elementtype == "Name")
return driver.FindElement(By.Name(element)).GetAttribute("value");
else return String.Empty;
}
Getting Value from DropDownList box
//Getting value from Dropdownlist
public static string GetTextFromDDL(IWebDriver driver, string element, PropertyType elementtype)
{
if (elementtype == "Id")
return new SelectElement(driver.FindElement(By.Id(element))).AllSelectedOptions.SingleOrDefault().Text;
if (elementtype == "Name")
return new SelectElement(driver.FindElement(By.Name(element))).AllSelectedOptions.SingleOrDefault().Text;
else return String.Empty;
}
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
11 Replies to “Custom methods for control in Selenium C# (Part 2)”
You forgot to update the program.cs file in your video to reference the updated GetTextFromDDL, the code should be:-
[Test]
public void ExecuteTest()
{
// Select title
SeleniumSetMethods.SelectDropDown(driver, “TitleId”, “Mr.”,”Id”);
// Type Initial
SeleniumSetMethods.EnterText(driver, “Initial”, “executeautomation”, “Name”);
Console.WriteLine(“The value from my Title is:” + SeleniumGetMethods.GetTextFromDDL(driver,”TitleId”,”Id”));
Console.WriteLine(“The value from my Initial is:” + SeleniumGetMethods.GetText(driver,”Initial”,”Name”));
// Click Save
SeleniumSetMethods.Click(driver, “Save”, “Name”);
Even after updating the code as
Console.WriteLine(“The value from my Title is:” + SeleniumGetMethods.GetTextFromDDL(driver,”TitleId”,”Id”));
this….,
am getting output as ‘Select’. not the one which we are passing ‘Mr.’
i donno y….
Hi Ya,
Saw this video it is great…just one question
In the Video –
//Getting value out from Textbox
public static string GetText(IWebDriver driver, string element, String elementtype)
In this page code says this
//Getting value out from Textbox
public static string GetText(IWebDriver driver, string element, PropertyType elementtype)
should the element type be string or PropertyType? if it is property type what is the namespace to be added?
Hello Karthik.
Watching you video’s now, find it very useful.
One question. In this video you are using this method:
AllSelectedOptions.SingleOrDefault().Text
for getting the selected value. Why aren’t you using:
SelectedOption.Text
for this?
The reason, its giving a list of options from the control, hence, I am using LINQ method SingleOrDefault() to select the first value and its text. Its just yet another option to prove you can use either in this way !!
Your courses are awesome! But in this particular one I have a problem. The test executes fine, but the text output always comes empty in the following way:
The value from my Title is:
The value from my Initial is:
I want to click a hyperlink from radgrid in telerik control. That control is also used pagination control. So, I check every pages and one of the page is contained which is my searched link means, How can I click that.
Note: My searched hyperlink in current page in the sense I can click. But it is in another page means ?
You forgot to update the program.cs file in your video to reference the updated GetTextFromDDL, the code should be:-
[Test]
public void ExecuteTest()
{
// Select title
SeleniumSetMethods.SelectDropDown(driver, “TitleId”, “Mr.”,”Id”);
// Type Initial
SeleniumSetMethods.EnterText(driver, “Initial”, “executeautomation”, “Name”);
Console.WriteLine(“The value from my Title is:” + SeleniumGetMethods.GetTextFromDDL(driver,”TitleId”,”Id”));
Console.WriteLine(“The value from my Initial is:” + SeleniumGetMethods.GetText(driver,”Initial”,”Name”));
// Click Save
SeleniumSetMethods.Click(driver, “Save”, “Name”);
Even after updating the code as
Console.WriteLine(“The value from my Title is:” + SeleniumGetMethods.GetTextFromDDL(driver,”TitleId”,”Id”));
this….,
am getting output as ‘Select’. not the one which we are passing ‘Mr.’
i donno y….
Hi Ya,
Saw this video it is great…just one question
In the Video –
//Getting value out from Textbox
public static string GetText(IWebDriver driver, string element, String elementtype)
In this page code says this
//Getting value out from Textbox
public static string GetText(IWebDriver driver, string element, PropertyType elementtype)
should the element type be string or PropertyType? if it is property type what is the namespace to be added?
hi i have the same doubt did your query get clarifies if yes please let me know
Hello Karthik.
Watching you video’s now, find it very useful.
One question. In this video you are using this method:
AllSelectedOptions.SingleOrDefault().Text
for getting the selected value. Why aren’t you using:
SelectedOption.Text
for this?
The reason, its giving a list of options from the control, hence, I am using LINQ method SingleOrDefault() to select the first value and its text. Its just yet another option to prove you can use either in this way !!
Thanks,
Hi!
Your courses are awesome! But in this particular one I have a problem. The test executes fine, but the text output always comes empty in the following way:
The value from my Title is:
The value from my Initial is:
Do you know what this might be?
Thanks!
Lucas, did you get this resolved? (I have the same problem.)
Hi Karthik
How to get text for links and header?
Thanks You
Hi Karthik,
I want to click a hyperlink from radgrid in telerik control. That control is also used pagination control. So, I check every pages and one of the page is contained which is my searched link means, How can I click that.
Note: My searched hyperlink in current page in the sense I can click. But it is in another page means ?