简体   繁体   中英

Is this approach better? could I improve it any more?

Of late I started dabbling in to C# for Selenium 2.0 (aka WebDriver). For those who are not aware of Selenium, it lets you control UI objects of web application using html id, name, class etc.

So you can click on an element as -

webDriver.FindElement(By.Id(elementLocator)).Click();

but then you can also use html name or class or XPath to click on element. So it could be -

webDriver.FindElement(By.Name(elementLocator)).Click();
webDriver.FindElement(By.Xpath(elementLocator)).Click();

Soon I realized that I should abstract it in a method and use the method instead of using such long statements through out my tests. So I created a method as -

 public static void click(IWebDriver webDriver, int elementLocatorType, String elementLocator)
    {
        switch (elementLocatorType)
        {
            case 0:
                webDriver.FindElement(By.Id(elementLocator)).Click();
                break;
            case 1:
                webDriver.FindElement(By.Name(elementLocator)).Click();
                break;
            case 2:
                webDriver.FindElement(By.XPath(elementLocator)).Click();
                break;
        }

    }

and use it as -

Commons.click(webDriver, 0, elementLocator)

I am wondering if I could improve it any more, say if I could avoid switch statement and use something better....

I would pass the By.* call as delegate to the click method. I don't know the actual return type of the By.* calls, but in case the methods return IElement you could do it like this:

// Replace IElement by the actual return type of By.Id, By.Name, By.XPath!
public static void click(IWebDriver webDriver, Func<string, IElement> by, String elementLocator)
{
    webDriver.FindElement(by(elementLocator)).Click();
}

Usage:

Commons.click(webDriver, By.Id, elementLocator);
Commons.click(webDriver, By.Name, elementLocator);
Commons.click(webDriver, By.XPath, elementLocator);

This will get you rid of those error-prone "magic" numbers.

Alternative, using extension methods - to satisfy comments :)

// Replace IElement by the actual return type of By.Id, By.Name, By.XPath!
public static void click(this IWebDriver webDriver, Func<string, IElement> by, String elementLocator)
{
    webDriver.FindElement(by(elementLocator)).Click();
}

// Convenience methods
public static void clickById(this IWebDriver webDriver, String elementLocator)
{
    webDriver.click(By.Id, elementLocator);
}

public static void clickByName(this IWebDriver webDriver, String elementLocator)
{
    webDriver.click(By.Name, elementLocator);
}

public static void clickByXPath(this IWebDriver webDriver, String elementLocator)
{
    webDriver.click(By.XPath, elementLocator);
}

The least you should do is to introduce an enumeration for that elementLocatorType .

But I would do it like this:

public static void ClickElement(this IWebDriver webDriver, Element element)
{
    webDriver.FindElement(element).Click();
}

public static void ClickElementById(this IWebDriver webDriver, string elementLocator)
{
    webDriver.ClickElement(By.Id(elementLocator));
}

public static void ClickElementByName(this IWebDriver webDriver, string elementLocator)
{
    webDriver.ClickElement(By.Name(elementLocator));
}

public static void ClickElementByXPath(this IWebDriver webDriver, string elementLocator)
{
    webDriver.ClickElement(By.XPath(elementLocator));
}

You can use it like this:

webDriver.ClickElementById(elementLocator1);
webDriver.ClickElementByName(elementLocator2);
webDriver.ClickElementByXPath(elementLocator3);

If there are even more possibilities to locate an element, consider passing the location method as a delegate as Florian's answer suggests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM