在范围报告中测试用例失败时获取java.lang.ClassCastException。 我有一个TestListener类,它在一个程序包中实现onpass,onfailure,onskipp方法声明,在一个程序包中具有Test类,并且在第三个程序包的基类下声明Webdriver,这样做是为了提高可读性。
现在,每当一个测试用例失败时,控制就会转到失败,我们已经实现了屏幕截图功能,这就是我的代码失败的地方。
onfailure方法-
public void onTestFailure(ITestResult iTestResult) {
System.out.println("I am in onTestFailure method " + getTestMethodName(iTestResult) + " failed");
//Get driver from BaseTest and assign to local webdriver variable.
Object testClass = iTestResult.getInstance();
WebDriver webDriver = ((BaseClass) testClass).getDriver();
//Take base64Screenshot screenshot.
String base64Screenshot = "data:image/png;base64,"+((TakesScreenshot)webDriver).
getScreenshotAs(OutputType.BASE64);
//Extentreports log and screenshot operations for failed tests.
ExtentTestManager.getTest().log(LogStatus.FAIL,"Test case failed",
ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot));
//ExtentTestManager.getTest().log(LogStatus.FAIL, "Test case failed as:"+iTestResult.getThrowable());
}
基类:
public WebDriver getDriver()
{
if (driver == null)
{
System.setProperty("webdriver path here");
driver= new ChromeDriver();
return driver;
}
else
{
return driver;
}
}
错误日志-
java.lang.ClassCastException: VWAutomationTest.TestVWLoginPage cannot be cast to Utility.BaseClass
at Listeners.TestListener.onTestFailure(TestListener.java:77)
at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:67)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1388)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1041)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
答案 0 :(得分:0)
我想出了答案,
我正在做的错误是我没有在我的Test类和Testlistener以及VWAutomationTest.TestVWLoginpage中扩展Baseclass,我在调用baseclass的对象,因此出现错误。 为了使其工作,我只是扩展了基类,即
public class TestVWLoginPage extends Utility.BaseClass
其他所有内容都保持不变。