【问题标题】:In selenium how to continue running a script even if it is failed在 selenium 中,即使脚本失败,如何继续运行脚本
【发布时间】:2016-05-12 22:04:45
【问题描述】:

我有一个表单填写脚本 在任何行(步骤)执行期间,如果测试用例失败,则直接关闭浏览器。 例如:在填写表单时,如果找不到元素(文本框或复选框),它会抛出异常 nosuchelements 并直接关闭我在脚本中使用 assert (testNG) 的浏览器 我想要的是,如果测试用例在中间的任何地方都失败了,我希望测试用例继续执行,但在报告中它应该显示测试用例失败

在我的代码中

String Jobvalue = driver.findElement(By.xpath("//label[@for='ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_5']")).getText();

Assert.assertEquals(Jobvalue, "Job Value ($)");

Reporter.log(Jobvalue+" Text Verification ---- Passed",true);

如果实际值不等于期望值,断言将失败测试用例 我想要一个替代断言或任何继续执行测试用例直到结束的代码,如果实际值不等于预期值,它应该在报告中显示它失败

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    Santhosh,我们需要更多关于您到底在尝试什么的信息/代码。假设,这是我对您问题的回答。

    在测试步骤中使用 validate() 方法而不是 assert()。

    【讨论】:

      【解决方案2】:

      使用 try-catch 可以轻松处理任何异常。

      try{
      String Jobvalue = driver.findElement(By.xpath("//label[@for='ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_5']")).getText();
      
      Assert.assertEquals(Jobvalue, "Job Value ($)");
      }
      catch()
      {
      //Write your code here about failing the test
      }
      

      【讨论】:

        【解决方案3】:

        如果 Assert 失败,它会抛出 AssertionError,这就是你的程序停止的原因。您可以使用 try/catch 来处理异常。

        try{
         Assert......
        }catch(AssertionError  e){
         //handle the exception
        }
        

        请参阅 here 获取断言文档

        【讨论】:

          猜你喜欢
          • 2016-05-15
          • 1970-01-01
          • 2020-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-08
          相关资源
          最近更新 更多