簡體   English   中英

即使由於腳本中的錯誤處理(嘗試和捕獲塊)而導致測試失敗,Jenkins也會顯示“ Build Success”

[英]Jenkins displays “Build Success” even when tests are failed due to error handling in scripts (try and catch block)

注意 :我無法粘貼確切的框架和代碼,因為無法從外部訪問正在使用的服務器。 因此,我將嘗試用簡單的單詞和例子來解釋我的問題。

概述 -我創建了一個Selenium自動化框架,其中包括TestNG,Maven(POM.XML),測試數據文件,腳本和一些可重用的功能。

我面臨的問題 -我使用Jenkins執行腳本。 Jenkins調用了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塊中,如果我不提及“ throw e”,Jenkins將不了解該故障已發生,並在控制台輸出中顯示“ BUILD PASSED”。 我希望它在Jenkins控制台中顯示“ BUILD FAILURE”。 我希望它顯示“ BUILD FAILED”的原因是-我已將JIRA與Jenkins集成在一起。 僅當詹金斯顯示“ BUILD FAILED”時,它才會自動將錯誤記錄到JIRA。 如果它為“ BUILD PASSED”,並且完成狀態為UNSTABLE,則Jenkins的測試結果部分將不會顯示任何故障,也不會在JIRA中記錄任何錯誤。

但是,到那時,我將無法將return“ fail”傳遞給主報告功能,這樣它就可以在報告中將登錄步驟顯示為失敗。

我了解,按照JAVA,我們可以在catch塊中拋出或返回,但不能兩者都拋出。 我們還有其他方法可以使這項工作嗎?

我已經創建了端到端框架,但是后來在我開始與Jenkins集成時意識到了這個問題(否則在那之前一切都很好)。

為什么不在catch語句中添加斷言失敗,以免在testng語句中強制testng測試失敗

org.testng.Assert.fail(“由於...,我在這里失敗,您可以在這里添加您的電子消息”);

只需在之前添加代碼行

return "fail";

並保持其余功能不變

您可以使用斷言來解決此問題,因此只要您的條件不滿足,斷言就會失敗,因此測試用例和jenkins會將構建狀態顯示為“ UNSTABLE”而不是“ PASS”。

例如,在上面的示例中,可以使用單行斷言來解決該問題,而不用使用try catch和if條件,也可以為您提供所需的構建狀態。

您可以將以上代碼替換為:
Assert.assertTrue(driver.findElement(By.id("welcomemessage")).isDisplayed(), "Element is not present on the page");

因此,在這種情況下,如果元素未顯示在頁面上,則assert會失敗,因為它期望的是true值,但是會變為false,並且您的jenkins構建狀態將顯示為不穩定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM