Selenium WebDriver C#——抛出异常时继续
Selenium WebDriver C# -- continue when exception thrown
我想检查页面上是否存在许多元素,如果不存在,我想查看自定义错误消息列表,告诉我哪些元素不存在。
目前我有:
测试用例:
[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;
}
这会起作用,但是,如果其中任何一个失败,测试将失败。我需要一种方法来继续 运行 测试,然后在测试结束时吐出任何错误,例如:"Cannot find the first element"、"Cannot find the third element" 等
谢谢。
您可以将方法修改为 return 错误字符串:
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);
}
}
这只是一种方式。你可以变得更复杂一点,return 一个 class 作为一个 boolean
和一个 ErrorMessage
属性 的结果,如果你想要花哨的话。
创建一个新的 class 来保存满足您需要的方法
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"每次 "attribute",像这样:
public void ElementHasAttribute(IWebElement element, string elementName)
{
ElementHasAttribute(element, elementName, "id");
}
所以每次你想要你的属性是"id",你只需调用重载方法。您几乎可以按照上面的示例做任何事情,并根据您的需要更改 code/methods。
您还可以修改结果 class 以执行以下操作
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();
我想检查页面上是否存在许多元素,如果不存在,我想查看自定义错误消息列表,告诉我哪些元素不存在。
目前我有:
测试用例:
[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;
}
这会起作用,但是,如果其中任何一个失败,测试将失败。我需要一种方法来继续 运行 测试,然后在测试结束时吐出任何错误,例如:"Cannot find the first element"、"Cannot find the third element" 等
谢谢。
您可以将方法修改为 return 错误字符串:
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);
}
}
这只是一种方式。你可以变得更复杂一点,return 一个 class 作为一个 boolean
和一个 ErrorMessage
属性 的结果,如果你想要花哨的话。
创建一个新的 class 来保存满足您需要的方法
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"每次 "attribute",像这样:
public void ElementHasAttribute(IWebElement element, string elementName)
{
ElementHasAttribute(element, elementName, "id");
}
所以每次你想要你的属性是"id",你只需调用重载方法。您几乎可以按照上面的示例做任何事情,并根据您的需要更改 code/methods。
您还可以修改结果 class 以执行以下操作
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();