在IF语句C#Selenium中处理异常

时间:2018-07-12 08:53:40

标签: c# selenium selenium-webdriver

我用IF语句编写了代码。但是,我目前所做的所有语句都是输出,无论我期望的文本是否正确。

如何更改实现方式,以使标题与我期望的测试不匹配?

 String title = webDriver.Title;

 String expectedTitle = "Utilities from Go Compare";
 if (title.Contains(expectedTitle))
 {
     Console.WriteLine("Tile is matching with expected value");
 }
 else
 {
     Console.WriteLine("Tile is not matching");
 }

3 个答案:

答案 0 :(得分:2)

String title = webDriver.Title;
String expectedTitle = "Utilities from Go Compare";

if (title.Contains(expectedTitle))
{
    Console.WriteLine("Title is matching with expected value");
}
else
{
    throw new Exception("Title does not match");
}

或在[TestMethod]中:

String title = webDriver.Title;
String expectedTitle = "Utilities from Go Compare";

bool contains = title.Contains(expectedTitle);
Assert.IsTrue(contains);

答案 1 :(得分:2)

如果它是一种测试方法,则应引发异常或按以下方式使用“ Assert”类:

String title = webDriver.Title;
String expectedTitle = "Utilities from Go Compare";

bool ignoreCase = ...;
Assert.AreEqual(title, expectedTitle, ignoreCase);

答案 2 :(得分:0)

You can use assert on you verification point, if assert conditions is not matching assert will throw exception :

String expectedTitle = "Utilities from Go Compare";

// 1st Way
bool isTrue= webDriver.Title.Contains(expectedTitle);
Assert.IsTrue(isTrue);

OR 

// 2nd Way
Assert.Contains(expectedTitle,webDriver.Title);