ExecuteAutomation

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