首页 > 解决方案 > 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);
            }

如果第一个断言失败,有没有办法继续测试到最后?如果任何其他断言失败,或者发生任何其他异常,都在同一个堆栈跟踪消息下吗?

我现在所拥有的,如果一个断言失败,则测试存在,我会收到带有堆栈跟踪的消息。

标签: c#seleniumassert

解决方案


您可以像这样将每个断言放入它自己的 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);
        }

推荐阅读