2017-07-31 47 views
1

我需要将结果值传递给我拥有的check方法。将测试结果传递给使用Java的Selenium中的ITestResult

@Test 
    public void test1() 
    { 
     test = extent.createTest("Test Case 4 : Checking displayed date against current date", 
       "Checking if the date displayed on the check in and check out box is equal to the current date"); 

     String checkin = driver.findElement(By.id("from")).getAttribute("value"); 
     test.info(checkin); 

     String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); 

     if (checkin.equals(timeStamp)) 
     { 
      test.info("The checkin in date " + checkin + " is equal to the current date " + timeStamp); 
     } else 
     { 
      test.info("The checkin in date " + checkin + " does not equal the current date " + timeStamp); 
     } 

    } 

    @AfterMethod 
    public void check(ITestResult result) throws IOException 
    { 
     if (result.getStatus() == ITestResult.FAILURE) 
     { 
      String screenshotPath = GetScreenshot.capture(driver); 
      test.fail("Test Case Failed"); 
      test.info(result.getThrowable()); 
      test.info("Screenshot Below : " + test.addScreenCaptureFromPath(screenshotPath)); 

     } else if (result.getStatus() == ITestResult.SKIP) 
     { 
      test.skip("Test Case Has Been Skipped"); 
     } else 
     { 
      test.pass("Test Case Passed"); 
     } 
    } 

所以,我想做到的是,如果checkin等于timeStamp那么测试应该通过,如果checkin不等于timeStamp测试应该失败。对于我当前的代码,测试通过两种情况。

有无论如何,我可以通过test.fail()什么的check方法。我试过check(ITestResult.FAILURE);哪个没有用。

我也试过check((ITestResult) test.fail("Test has Failed"));,它投掷了ClassCastException

回答

0

在您的实际案例结尾“@test”对预期结果有断言。如果断言通过,测试用例将在方法之后传递一个状态,反之亦然。

//Assertion Example in Testng 
Assert.assertEquals(checkin, timestamp); 

希望这会有所帮助。谢谢。

相关问题