c# - catch 是否有可能获得多个异常?
问题描述
我有一个类似于下面的代码:
try {
SelectElement selectSize = new SelectElement(picProfileBtn);
IList<IWebElement> optionsProfile = selectSize.Options;
IWebElement firstProfile = optionsProfile[0];
Assert.AreEqual("S", firstProfile.Text);
IWebElement secondProfile = optionsProfile[1];
Assert.AreEqual("M", secondProfile.Text);
IWebElement thirdProfile = optionsProfile[2];
Assert.AreEqual("L", thirdProfile.Text);
IWebElement fourthProfile = optionsProfile[3];
Assert.AreEqual("Test", fourthProfile.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
如果第一个断言失败,有没有办法继续测试到最后?如果任何其他断言失败,或者发生任何其他异常,都在同一个堆栈跟踪消息下吗?
我现在所拥有的,如果一个断言失败,则测试存在,我会收到带有堆栈跟踪的消息。
解决方案
您可以像这样将每个断言放入它自己的 try/catch 块中。
try {
SelectElement selectSize = new SelectElement(picProfileBtn);
IList<IWebElement> optionsProfile = selectSize.Options;
IWebElement firstProfile = optionsProfile[0];
Assert.AreEqual("S", firstProfile.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try {
IWebElement secondProfile = optionsProfile[1];
Assert.AreEqual("M", secondProfile.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try {
IWebElement thirdProfile = optionsProfile[2];
Assert.AreEqual("L", thirdProfile.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try {
IWebElement fourthProfile = optionsProfile[3];
Assert.AreEqual("Test", fourthProfile.Text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
推荐阅读
- c - C语言。这是指针比较还是其他?
- machine-learning - 如何使用pytorch处理多标签分类中的类不平衡
- html - 如何加载此天气 API 的 API 调用以显示在我的 html 页面的 div 中
- swift - 如何在 UIBezierPath 中对称地平滑曲线
- r - A,C,G,T上的R数据框列顺序
- java - 什么是创建映射函数表以应用于 Java 中的每个查询参数的好方法
- excel - 在 Excel 中绘制条形图
- python - 如何抓取新闻内容并删除不相关的部分
- django - 如何在同一管理页面 Django 上添加多个模型对象
- scons - gem5 scons 构建失败,并显示“TypeError:参数应该是整数或类似字节的对象,而不是'str'”