Site Loader
Auckland, New Zealand
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
  • Reduced number of parameters
  • Strongly-typed parameters
  • More reusable
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

Post Author: Karthik kk

10 Replies to “Refactoring Custom methods of control libraries”

  1. Error 1: Inconsistent accessibility: parameter type ‘UnitTestProject3.PropertyType’ is less accessible than method ‘UnitTestProject3.seleniumSetMethods.EnterText(string, string, UnitTestProject3.PropertyType)’
    this error comes when i run my task..!! Please reply for solution..

    1. I think your enum is within the SeleniumSetMethods class, make sure you copy paste the enum out from the class and make it as public so that it can be accessible.

      Thanks,
      Karthik KK

    1. Its not string, rather its an WebElement type, since FindElement method returns WebElement interface.

  2. Nice work Karthi and thanks for the post.

    From the dropdown when I selected the value, the program selected the correct value for the app but Console Write its display the First 2 characters only.

    return new SelectElement(PropertiesCollection.driver.FindElement(By.Name(element))).AllSelectedOptions.SingleOrDefault().Text;

    1. Thanks Ram!

      I guess the code is very specific to get a single value.

      Using SingleOrDefault() method will always get only ONE value, its used mainly to not through any null reference exception.

      To get all value you can modify your code like this

      SelectElement(PropertiesCollection.driver.FindElement(By.Name(element))).AllSelectedOptions

      Thanks,
      Karthik KK

  3. Hi Karthik,

    I my XPath i have two values in the same name, i run the test below code but my test is failed please suggest me in proper way.

    IWebElement Firs = driver.FindElement(By.XPath(“(//input[@name=’phone-number’])[1]”)); Firs.SendKeys(“123456789”);

    Below are the Xpath’s in chrome browser Console:
    $x(“.//input[@name=’phone-number’]”)
    (2) [input.wpcf7-form-control.wpcf7-text.wpcf7-tel.wpcf7-validates-as-required.wpcf7-validates-as-tel.z3c…, input.wpcf7-form-control.wpcf7-text.wpcf7-tel.wpcf7-validates-as-required.wpcf7-validates-as-tel.wpc…]

    Thanks,
    Siva.

  4. HI Karthik i appreciate if you paste all codes on the page as its missing program.cs.

    please each lessons you have if you can write the codes page wise

    like program.cs. and other 2 class files

Leave a Reply to Karthik kk Cancel reply

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