繁体   English   中英

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

[英]Handle an exception in IF statement c# Selenium

我已经用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");
 }
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);

如果是测试方法,则应引发异常或使用“ Assert”类,如下所示:

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

bool ignoreCase = ...;
Assert.AreEqual(title, expectedTitle, ignoreCase);
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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM