我需要将结果值传递给我拥有的check
方法。
@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 :(得分:0)
在实际文本案例结束时,“@ test”会对预期结果进行断言。如果断言通过,则测试用例将在方法之后传入状态,反之亦然。
//Assertion Example in Testng
Assert.assertEquals(checkin, timestamp);
希望这会有所帮助。感谢。