Site Loader
Auckland, New Zealand
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)”

  1. 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”);

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

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

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

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

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

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

Leave a Reply to ZerO81 Cancel reply

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