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
Site Loader
Auckland, New Zealand
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 Set operation in the control like entering text, clicking button, selecting drop down etc. In next post, we will deal with Get operations 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)”

  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 ?

    1. Yap, you have that option, there is a method in By class, which help you to identify element using Class name

      Thanks,
      Karthik KK

  2. 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;
    }
    }

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

    1. We have separate post for this covered in upcoming post of POM.

      Thanks for bringing it up !!!

      Thanks,
      Karthik KK

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

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

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

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

  5. 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

    1. You are welcome Michael, BTW I dont have any video for jstree, should make one if time permits me 🙂

      Thanks,
      Karthik KK

  6. 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

    1. You should switch to the iframe and perform operation, again it cannot sit on the controls method though.

      Thanks,
      Karthik KK

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

Leave a Reply

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