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

 

6 comments

  1. ZerO81 says:

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

    • karthik says:

      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. Sri says:

    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. Valentin says:

    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?

    • Karthik kk says:

      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. Lucas says:

    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!

Leave a Reply

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