2017-07-31 3 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"); 
     } 
    } 

それでは、私が達成したいことはcheckintimeStampに等しい場合checkinは、テストが失敗するtimeStampと等しくない場合、テストは合格とすべきです。私の現在のコードでは、テストは両方の状況で合格します。

私はtest.fail()などをcheckメソッドに渡すことができます。

私はcheck(ITestResult.FAILURE);を試しましたが動作しませんでした。

ClassCastExceptionを投じたcheck((ITestResult) test.fail("Test has Failed"));も試しました。

答えて

0

実際のテキストの大文字小文字の最後に、「@test」には期待される結果のアサーションがあります。アサーションが渡された場合、テストケースはafterメソッドで渡されたステータスを持ち、その逆もあります。

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

これが役に立ちます。ありがとう。

関連する問題