詹金斯将显示“构建成功”,即使测试失败,因为该脚本错误处理(try和catch块)

问题描述 投票:0回答:2

注:我无法粘贴确切的框架和代码为我工作的服务器是不能从外部访问。因此,我将试图解释在简单的话和例子我的问题。

概述 - 我创建了一个硒自动化框架,其中包括TestNG的,Maven的(pom.xml的),测试数据文件,脚本和可重复使用的一些功能。

问题我面临的 - 我用詹金斯执行我的脚本。詹金斯称pom.xml文件,其依次调用的testng.xml文件(文件的testng.xml,我提到要执行的脚本)

比方说,我一定要进行登录操作

主脚本

@Test
Public void maintest ()
{

//I use Extent reports for reporting purpose and hence I have created extent 
 //reporting reusable function which is called in the below fashion.

//If Login method returns pass, ExtentReportingFunc passes Pass to its 
 //function and displays Pass for that particular Login step in the report.

ExtentReportingFunc (Login(UserName, Password));
}

可重复使用的功能

Public String Login (String UN, String Pass)
{
//Sendkeys and set UN
driver.findelement (By.id("username")).sendkeys(UN);

//Sendkeys and set Password
driver.findelement (By.id("password")).sendkeys(pass);

//Click Login
driver.findelement (By.id("login")).click ();

//Verifying the message "Welcome User" is displayed after login
   try 
   {
      if (driver.findlement (By.id("welcomemessage")).isdisplayed ();
      {
        return pass;
      }
   } catch (Exception e)
   {
     //If welcome message is not found then return fail to the Reporting function
     return "fail";

     //Below code will be unreachable which is correct as per Java but that is the challenge.
      // I tried several ways to find a work around to return fail as above as 
      // well throw exception but no luck till now.
      // Upon throwing exception in the below way, Jenkins displays build as 
      //failed. If it is not done, though the script has failed, 
      //Jenkins displays "BUILD PASSED"

       throw e;
   }
}

//Extent Reporting function
ExtentReportingFunc (String status)
{
log.report (status);
}

这里的挑战是 - 在catch块,如果我不提“扔E”,詹金斯会不明白发生了故障,并显示“BUILD通过”在它的控制台输出。我希望它显示在控制台詹金斯“构建失败”。我之所以希望它显示“BUILD失败” - 我有集成的JIRA与詹金斯。只有当詹金斯显示构建失败,它会自动记录的bug JIRA。如果是“BUILD通过”虽然最终状态是不稳定的,没有失败将显示在詹金斯的测试结果部分也不在JIRA记录任何错误。

不过,那个时候我将无法通过返回“失败”的主报告功能,以便它可以显示登录步骤,如报告故障。

我明白了,按照Java中,我们可以抛出或catch块中返回,但不能同时使用。有没有我们可以使这项工作的任何其他方式?

我已经创造了端到端的框架,但是当我开始与詹金斯集成(否则一切都很好,直到那时)后来才意识到这个问题。

java maven selenium jenkins testng
2个回答
1
投票

你为什么不加入断言失败里面你catch语句,你迫使TestNG的测试失败这种方式,当它的catch语句内

org.testng.Assert.fail(“我现在没有在这里因...你可以在这里添加您的电子信息”);

只需添加一行代码之前

return "fail";

并保持功能的其余部分是


0
投票

您可以通过使用断言解决这个问题,所以每当你的条件不满足断言将失败,所以将你的测试案例和詹金斯将显示为打造“不稳定”,而不是“PASS”的状态。

例如,在上面的例子中,而不是使用尝试捕捉,如果在try条件下,它可以被断言的单行这也会给你你想要的生成状态以及解决。

您可以替换与上面的代码: Assert.assertTrue(driver.findElement(By.id("welcomemessage")).isDisplayed(), "Element is not present on the page");

所以在这一点,如果在网页上没有显示的元素,断言将失败,因为它期待真正的价值,但会得到错误的和您的詹金斯建立状态将显示为不稳定。

© www.soinside.com 2019 - 2024. All rights reserved.