从Testng运行测试组时,在Testlistener中获取java.lang.ClasscastException

时间:2018-08-08 08:42:45

标签: selenium testng

我正在尝试使用Testlistner类使用Testng运行存储在TestVWLoginPage中的测试用例,将驱动程序从基类存储到testlistener中的局部变量时,我遇到了类转换异常。

我已经附加了Testlistenere类,错误代码和受影响的代码行。

在调试时,我意识到,如果我不在testng文件中运行组,也可以正常工作。

<groups>
     <run>
        <include name = "Guitest" />
     </run>
  </groups>
   <classes>
     <class name = "VWAutomationTest.TestVWLoginPage" />
  </classes>

添加上面的代码后,我收到了classcast异常 随附的代码-

Testlistner类-

package Listeners;

import com.relevantcodes.extentreports.LogStatus;

import java.util.Set;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import Utility.BaseClass;
import ExtentReports.ExtentManager;
import ExtentReports.ExtentTestManager;


public class TestListener implements ITestListener {

private static String getTestMethodName(ITestResult iTestResult) {
    return iTestResult.getMethod().getConstructorOrMethod().getName();
}


//Before starting all tests, below method runs.
@Override
public void onStart(ITestContext iTestContext) {
    System.out.println("I am in onStart method " + iTestContext.getName());
    //iTestContext.setAttribute("WebDriver", this.driver);
}

//After ending all tests, below method runs.
@Override
public void onFinish(ITestContext iTestContext) {
    System.out.println("I am in onFinish method " + iTestContext.getName());





    //Do tier down operations for extentreports reporting!
    ExtentTestManager.endTest();
    ExtentManager.getReporter().flush();

}

@Override
public void onTestStart(ITestResult iTestResult) {
    System.out.println("I am in onTestStart method " +  getTestMethodName(iTestResult) + " start");
    //Start operation for extentreports.
    ExtentTestManager.startTest(iTestResult.getMethod().getMethodName(),"");
}

@Override
public void onTestSuccess(ITestResult iTestResult) {
    System.out.println("I am in onTestSuccess method " +  getTestMethodName(iTestResult) + " succeed");
    //Extentreports log operation for passed tests.
    ExtentTestManager.getTest().log(LogStatus.PASS, "Test passed");
}

@Override
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 Failed",
           ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot));
}

@Override
public void onTestSkipped(ITestResult iTestResult) {
    System.out.println("I am in onTestSkipped method "+  getTestMethodName(iTestResult) + " skipped");
    //Extentreports log operation for skipped tests.
    ExtentTestManager.getTest().log(LogStatus.SKIP, "Test Skipped");
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
    System.out.println("Test failed but it is in defined success ratio " + getTestMethodName(iTestResult));
}

}

我失败的代码-

@Override
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 Failed",
            ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot));
}

还有例外-

java.lang.ClassCastException: VWAutomationTest.TestVWLoginPage cannot be cast to Utility.BaseClass
at Listeners.TestListener.onTestFailure(TestListener.java:68)
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 个答案:

没有答案