ExecuteAutomation

Refactoring Custom methods of control libraries

In last two post we discussed creating custom methods for Setting values to the UI control and Getting values out from the UI control. The code we wrote was fine, but there were lot of redundant and not so necessary parameters passed in each and every methods, in this post we will try to refactor our code a little bit to to have Here is the complete video of the above discussion Here is the complete modified code discussed in the video First try to create a class called PropertiesCollection and create an auto-implemented property for WebDriver as shown below
    public static IWebDriver driver {get;set}
And assign the ChromeDriver to the PropertiesCollection.driver property, then instead of IWebDriver local instance variable on each method (Since those are also passed as parameters) , try to use the PropertyCollection.driver property. Set Methods for controls like Textbox, Button, DropDownList box.
    //Enter Text
        public static void EnterText(string element, string value, string elementtype)
        {
            if (elementtype == PropertyType.Id)
                PropertiesCollection.driver.FindElement(By.Id(element)).SendKeys(value);
            if (elementtype == PropertyType.Name)
                PropertiesCollection.driver.FindElement(By.Name(element)).SendKeys(value);
        }

        //Click into a button, Checkbox, option etc
        public static void Click(string element,string elementtype)
        {
           if (elementtype == PropertyType.Id)
                PropertiesCollection.driver.FindElement(By.Id(element)).Click();
             if (elementtype == PropertyType.Name)
                PropertiesCollection.driver.FindElement(By.Name(element)).Click();
        }

       //Selecting a drop down control
        public static void SelectDropDown(string element, string value, string elementtype)
        {
           if (elementtype == PropertyType.Id)
                new SelectElement(PropertiesCollection.driver.FindElement(By.Id(element))).SelectByText(value);
             if (elementtype == PropertyType.Name)
                new SelectElement(PropertiesCollection.driver.FindElement(By.Name(element))).SelectByText(value);
        }
Get Methods for controls like Textbox and DropDownList box
       public static string GetText(string element,PropertyType elementtype)
        {
            if (elementtype == PropertyType.Id)
                return PropertiesCollection.driver.FindElement(By.Id(element)).GetAttribute("value");
            if (elementtype == PropertyType.Name)
                return PropertiesCollection.driver.FindElement(By.Name(element)).GetAttribute("value");
            else return String.Empty;

        }

        public static string GetTextFromDDL(string element, PropertyType elementtype)
        {
            if (elementtype == PropertyType.Id)
                return new SelectElement(PropertiesCollection.driver.FindElement(By.Id(element))).AllSelectedOptions.SingleOrDefault().Text;
            if (elementtype == PropertyType.Name)
                return new SelectElement(PropertiesCollection.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