0

最近、私はSelenium 2.0(別名WebDriver)のC#に手を出し始めました。Seleniumを知らない人のために、html id、name、classなどを使用してWebアプリケーションのUIオブジェクトを制御できます。

したがって、要素をクリックすると-

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

ただし、html名、クラス、またはXPathを使用して要素をクリックすることもできます。だからそれは-

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

すぐに、テスト全体でそのような長いステートメントを使用する代わりに、メソッドで抽象化し、メソッドを使用する必要があることに気付きました。だから私は次のようにメソッドを作成しました-

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

    }

そしてそれを-として使用します

Commons.click(webDriver, 0, elementLocator)

これ以上改善できるかどうか、たとえば、switchステートメントを避けてより良いものを使用できるかどうか疑問に思っています。

4

2 に答える 2

2

ClickメソッドへのデリゲートとしてBy。*呼び出しを渡します。By。*呼び出しの実際の戻りタイプはわかりませんが、メソッドが戻る場合は、次のIElementように行うことができます。

// 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();
}

使用法:

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

これにより、エラーが発生しやすい「マジック」ナンバーを取り除くことができます。

別の方法として、拡張メソッドを使用します-コメントを満たすために:)

// 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);
}
于 2011-03-31T08:37:59.190 に答える
1

少なくとも、そのための列挙型を導入する必要がありますelementLocatorType

しかし、私はこのようにします:

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));
}

次のように使用できます。

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

要素を見つける可能性がさらにある場合は、Florianの回答が示唆するように、ロケーションメソッドをデリゲートとして渡すことを検討してください。

于 2011-03-31T08:36:31.823 に答える