2017-11-07 7 views
0

ページ上に多数の要素が存在するかどうかをチェックしたい場合は、存在しないものがあるかどうかを確認するカスタムエラーメッセージの一覧を表示します。Selenium WebDriver C# - 例外がスローされたときに続きます。

テストケース:

[Test] 
public void CheckAllElementsArePresent() 
{ 
    Assert.IsTrue(Results.CheckFirstElement()); 
    Assert.IsTrue(Results.CheckSecondElement()); 
    Assert.IsTrue(Results.CheckThirdElement()); 
} 

フレームワーク:私が持っている瞬間

これらのいずれかが失敗した場合

public static bool CheckFirstElement() 
{ 
    var results = Driver.Instance.FindElement(By.CssSelector("firstElementSelector']")); 

    if (results == null) 
    { 
     throw new Exception("Cannot find first element"); 
    } 

    var attr = results.GetAttribute("id"); 
    if (attr == null) 
    { 
     throw new Exception("Cannot find first element"); 
    } 

    if (attr.Contains("someTextIWantToFind")) 
    { 
     return true; 
    } 
    return false; 
} 

これは動作しますが、しかし、テストは失敗します。テストを続行し、テストの最後にエラーを吐き出す方法が必要です。たとえば、「最初の要素が見つからない」、「3番目の要素が見つかりません」など。

ありがとう。

public static string CheckFirstElement() 
{ 
    var results = Driver.Instance.FindElement(By.CssSelector("firstElementSelector']")); 

    if (results == null) 
    { 
     return "Cannot find first element"; 
    } 

    var attr = results.GetAttribute("id"); 
    if (attr == null) 
    { 
     return "Cannot find id attribute of first element"; 
    } 

    if (!attr.Contains("someTextIWantToFind")) 
    { 
     return "Cannot find the text in first element"; 
    } 
    return null; 
} 

が続いてメインのテストで見つかったすべてのエラーを収集:

[Test] 
public void CheckAllElementsArePresent() 
{ 
    var errors = new List<string>(); 
    var error = Results.CheckFirstElement(); 
    if(error != null) 
     errors.Add(error); 
    error = Results.CheckSecondElement(); 
    if(error != null) 
     errors.Add(error); 
    error = Results.CheckThirdElement(); 
    if(error != null) 
     errors.Add(error); 

    if(errors.Any()) 
    { 
     //Output to console 
     var errorString = string.Join(";", errors); 
     Console.Writeline(errorString); 
     //Fail the test 
     Assert.Fail(errorString); 
    } 

} 

これはただ一つの方法ですあなたは何ができるか

答えて

0

ニーズのための方法に

public static class MakeSure 
{ 
    private static List<string> errorsFound = new List<string>(); 
    public static List<string> getErrors() { return errorsFound; } 

    public void ElementIsNotNull(IWebElement element, string elementName) 
    { 
     if(element == null) 
     { 
      errorsFound.Add("Cannot find element " + elementName +"."); 
     } 
    } 

    public void ElementHasAttribute(IWebElement element, string elementName, string attribute) 
    { 
     if(element.GetAttribute(attribute) == null) 
     { 
      errorsFound.Add("Cannot find " + attribute + " attribute of the element."); 
     } 
    } 

    public void ElementContaisText(IWebElement element, string elementName, string attribute, string text) 
    { 
     if(!element.GetAttribute(attribute).Text.Contains(text)) 
     { 
      errorsFound.Add("Cannot find the text " + text +" in " + elementName +" element"); 
     } 
    } 
} 

を保持し、あなたがあなたの要素は、特定の属性を持っている「MakeSure」するために、あなたのテストケースからそれらを呼び出すことができる新しいクラスを作成し、等のnullではありませんより多くのオーバーロードは、あなたがそうのように、「ID」と「属性」毎回を配置する必要はありません。

public void ElementHasAttribute(IWebElement element, string elementName) 
{ 
    ElementHasAttribute(element, elementName, "id"); 
} 

だから毎回、あなたの属性が「ID」になりたい、あなただけのオーバーロードメソッドを呼び出します。上記の例に従うだけで、コード/メソッドを必要に応じて変更できます。

また、次の

public void CheckFirstElement() 
{ 
    IWebElement firstElement; 
    MakeSure.ElementIsNotNull(firstElement, "first element"); 
    MakeSure.ElementHasAttribute(firstElement, "first element", "id"); 
    MakeSure.ElementContaisText(firstElement, "first element", "id", "Text to validate."); 
} 

を行うには、あなたの結果クラスを変更することができますので、あなたのテストで行う必要がありますすべてがある:

Results.CheckFirstElement(); 
+1

これはすばらしく、私はそれを試して実装します - ありがとう! –

+0

問題ありません!どういたしまして。実装に関する質問がある場合は、質問を削除してください –

0

は、エラー文字列を返すようにする方法を変更しています。あなたはもっと洗練されて得ることができる場合は、結果としてbooleanErrorMessageプロパティでクラスを返します。あなたが作成することができます

+0

私はこの行く与える、ありがとう! –

関連する問題