2017-10-30 3 views
2
public enum ElementType 
{ 
    Id, 
    ClassName, 
    Name, 
    XPath, 
    CssSelector, 
    LinkText 
} 

public static class WebDriverExtensions 
{ 

    public static void AssertElementDisplayed(this IWebDriver driver, ElementType elementType, string element) 
    { 
     if (elementType == ElementType.Id) 
      Assert.IsTrue(driver.FindElement(By.Id(element)).Displayed); 
     if (elementType == ElementType.ClassName) 
      Assert.IsTrue(driver.FindElement(By.ClassName(element)).Displayed); 
     if (elementType == ElementType.Name) 
      Assert.IsTrue(driver.FindElement(By.Name(element)).Displayed); 
     if (elementType == ElementType.XPath) 
      Assert.IsTrue(driver.FindElement(By.XPath(element)).Displayed); 
     if (elementType == ElementType.CssSelector) 
      Assert.IsTrue(driver.FindElement(By.CssSelector(element)).Displayed); 
     if (elementType == ElementType.LinkText) 
      Assert.IsTrue(driver.FindElement(By.LinkText(element)).Displayed); 
    } 

    public static void WaitForElementPresent(this IWebDriver driver, ElementType elementType, string element) 
    { 
     WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 

     if (elementType == ElementType.Id) 
      wait.Until(ExpectedConditions.ElementIsVisible(By.Id(element))); 
     if (elementType == ElementType.ClassName) 
      wait.Until(ExpectedConditions.ElementIsVisible(By.ClassName(element))); 
     if (elementType == ElementType.Name) 
      wait.Until(ExpectedConditions.ElementIsVisible(By.Name(element))); 
     if (elementType == ElementType.XPath) 
      wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(element))); 
     if (elementType == ElementType.CssSelector) 
      wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector(element))); 
     if (elementType == ElementType.LinkText) 
      wait.Until(ExpectedConditions.ElementIsVisible(By.LinkText(element))); 
    } 

これは、if文を使用するのではなく、これを実現するための最良の方法がわからないスイッチケース文に変換する最良の方法を探しています。 By.Id、Byクラス、By.Nameなどを切り替える簡単な方法が必要です。C#Selenium WebDriverスイッチ/ケース

+2

は単なるswitch文を書くのか?何が問題なのですか? – j4nw

答えて

2

私はSeleniumに慣れていませんが、この作品のようなものでしょうか?

private By GetElement(ElementType type, string element) 
{ 
    switch(ElementType) 
    { 
     case ElementType.Id: 
      return By.Id(element); 
     // add your other cases here! 
    } 
} 

次のように次に、あなたの方法でそれを使用することができます。

public static void WaitForElementPresent(this IWebDriver driver, ElementType elementType, string element) 
{ 
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 

    wait.Until(ExpectedConditions.ElementIsVisible(GetElement(elementType, element))); 
} 
+0

なぜdownvote?私は学ぶことが大好きですが、このように助けにはなりません... – oerkelens

+0

OPがアサーションをしていて、あなたがちょうど "by"を返すので、私はそれを賭けました。 * shrugs *しかし、downvoteはdownvotedされた理由が説明されていない場合、downvotingは役に立ちませんので、downvoteはそれに必要なコメントが必要です。 – IamBatman

+1

@IamBatman私は、ちょうどByを返す新しいメソッドを提案しました。これは、使用されるところで必要とされるものとまったく同じようです...もしSeleniumが 'By'のサブクラスではなく、 「By.Id'は私には驚くかもしれませんが、スイッチを元の方法に戻すことができます。 – oerkelens

3

をすべてこの余分な情報を渡す必要はありません。ちょうどByロケータの周りを渡すと、残りの部分を処理し、コードをはるかに簡単にします。

public static void AssertElementDisplayed(this IWebDriver driver, By locator) 
{ 
    Assert.IsTrue(driver.FindElement(locator).Displayed); 
} 

とoerkelensコメントを1として

public static void WaitForElementPresent(this IWebDriver driver, By locator, int timespan) 
{ 
    new WebDriverWait(driver, TimeSpan.FromSeconds(timespan)).Until(ExpectedConditions.ElementIsVisible(locator)); 
} 
+0

これをすべて掲載したので、これは正しい方法ではないと思います(それぞれに別の方法があります)。このコードはあなた自身のスクリプトに入るはずです... 'Assert.IsTrue(driver.FindElement(...)。Displayed));など。 1ライナーをラップするメソッドを書く理由はあまりありません。 – JeffC

+0

私は演算子としてそれを置いた唯一の理由は、複数の人が同じプロジェクトで作業するため、他の人より経験が少なく、単純にその方法を使用できる人がいるからです。 – Danny

+0

それは本当に良い理由ではありません。それは、物事を行う正しい方法と同じことをする機能を指し示すことを彼らに示すためにもはや長くかかっていません。それらのチートシートを作成するか、ベストプラクティス、共通の問題などの文書を書いてください。最後に参照してより良いコードを書くことができます。いくつかの複雑さのためにヘルパーメソッドを保存します。 – JeffC

0

、これを解決し、最終的なコードは以下のとおりである:

public enum ElementType 
{ 
    Id, 
    ClassName, 
    Name, 
    XPath, 
    CssSelector, 
    LinkText 
} 

public static class WebDriverExtensions 
{ 
    public static By GetElement(ElementType type, string element) 
    { 
     switch (type) 
     { 
      case ElementType.ClassName: 
       return By.ClassName(element); 
      case ElementType.Name: 
       return By.Name(element); 
      case ElementType.XPath: 
       return By.XPath(element); 
      case ElementType.CssSelector: 
       return By.CssSelector(element); 
      case ElementType.LinkText: 
       return By.LinkText(element); 
      case ElementType.Id: 
      default: 
       return By.Id(element); 
     } 
    } 

    public static void AssertElementDisplayed(this IWebDriver driver, ElementType elementType, string element) 
    { 
      Assert.IsTrue(driver.FindElement(GetElement(elementType, element)).Displayed); 
    } 


    public static void WaitForElementPresent(this IWebDriver driver, ElementType elementType, string element) 
    { 
     WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
     wait.Until(ExpectedConditions.ElementIsVisible(GetElement(elementType, element))); 
    }