我正在尝试使用扩展区报告构建selenium,但我无法使用save screenshot功能,因为我无法在ITestListener类中引用WebDriver对象。以下是我的示例代码:
Test Runner.java:
@Listeners({com.peterwkc.Listener.TestListener.class})
public class ChromeTestManager {
private WebDriverManager webDriverManager = new WebDriverManager();
private WebDriver driver;
private LoginPages loginPages;
private AdminPages adminPages;
@BeforeClass
//@Parameters({"browser"})
public void setupTest(/*String browser*/) throws MalformedURLException {
System.out.println("BeforeMethod is started. " + Thread.currentThread().getId());
// Set & Get ThreadLocal Driver with Browser
webDriverManager.createDriver("chrome");
driver = webDriverManager.getDriver();
// Pages Object Initialization
loginPages = PageFactory.initElements(driver, LoginPages.class);
logoutPages = PageFactory.initElements(driver, LogoutPages.class);
adminPages = PageFactory.initElements(driver, AdminPages.class);
}
@DataProvider(name = "loginCredentials")
public static Object[][] getLoginCredentials() {
return new Object [][] {{ "Admin123", "admin123" }, {"testUser", "test"}, {"test", "test"}};
}
@Test(groups= {"Login"}, description="Invalid Login", priority = 0, dataProvider = "loginCredentials", invocationCount = 3)
public void login_invalid(String username, String password) {
loginPages.login_invalid(driver, username, password);
}
}
TestListener.java 公共类TestListener实现ITestListener {
//Extent Report Declarations
private static ExtentReports extent = ExtentManager.createInstance();
private static ThreadLocal<ExtentTest> test = new ThreadLocal<>();
public TestListener() {
}
@Override
public synchronized void onTestFailure(ITestResult result) {
System.out.println((result.getMethod().getMethodName() + " failed!"));
test.get().fail("Exception Error : \n" + result.getThrowable());
/*String feature = getClass().getName();
String screenShot;
try {
screenShot = CaptureScreenshot.captureScreen(driver, CaptureScreenshot.generateFileName(feature));
test.get().addScreenCaptureFromPath(screenShot);
test.get().log(Status.FAIL, screenShot);
} catch (IOException ex) {
LogManager.logger.log(Level.INFO, "Exception: " + ex.getMessage());
}*/
}
}
问题:
如何将WebDriver对象从TestRunner.java传递给TestListener 类
如何在范围报告3中保存屏幕截图?
我的代码有什么问题吗?
请提前帮助,谢谢!
答案 0 :(得分:3)
首先不要在@BeforeClass
中实例化您的webDriver,因为只有在课前注释时才会调用它,尝试使用interface ITestListener
并使用beforeInvocation
实现进行初始化WebDriver。
其次,你不能一次调用所有PageObjects的PageFactory,你怎么认为所有3个页面一次初始化,这应该在每个页面对象的构造函数中实现,当你初始化你的页面对象时(新的Login)元素也被初始化,所以这不行:
// Pages Object Initialization
loginPages = PageFactory.initElements(driver, LoginPages.class);
logoutPages = PageFactory.initElements(driver, LogoutPages.class);
adminPages = PageFactory.initElements(driver, AdminPages.class);
第三,我没有看到ExtentReport测试的初始化。它应该看起来像这样:
ExtentTest extentTest = ExtentTestManager.startTest(method.getName(), "");
以下是我调用屏幕截图实现的代码示例部分,我正在调用afterInvocation
,因为我使用的是并发驱动程序初始化,所以它必须来自这里,也可以通过onTestFailure
实施来实现:
public synchronized void afterInvocation(IInvokedMethod method, ITestResult testResult){
if (method.isTestMethod() && testResult.getStatus()==2) {
File scrFile = (dataMethod.getAndroidDriver()).getScreenshotAs(OutputType.FILE);
String dest = System.getProperty("user.dir") + "/resources/screenshots/" + dataMethod.getDriver().getSessionId() + ".png";
File destination = new File(dest);
try {
FileUtils.copyFile(scrFile, destination);
dataMethod.setScreenshotPath(destination.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
System.err.println("Path:" + dataMethod.getScreenshotPath());
}
你必须更多地考虑结构,
希望这会有所帮助......
答案 1 :(得分:3)
以下是执行此操作的步骤:
1:将WebDriver对象传递给侦听器类
首先在ChromeTestManager类中或您可以在其处调用的其他任何位置创建以下方法,这里假定它存在于ChromeTestManager类中:
public static ITestContext setContext(ITestContext iTestContext, WebDriver driver) {
iTestContext.setAttribute("driver", driver);
return iTestContext;
}
它将驱动程序对象设置为TestContext。
现在将您的@BeforeClass setUp方法更改为接受参数ITestContext,下面是代码:
public class ChromeTestManager {
private WebDriverManager webDriverManager = new WebDriverManager();
private WebDriver driver;
private LoginPages loginPages;
private AdminPages adminPages;
private static ITestContext context; // creating a ITestContext variable
@BeforeClass
//@Parameters({"browser"})
public void setupTest(ITestContext iTestContext) throws MalformedURLException {
System.out.println("BeforeMethod is started. " + Thread.currentThread().getId());
// Set & Get ThreadLocal Driver with Browser
webDriverManager.createDriver("chrome");
driver = webDriverManager.getDriver();
this.context = setContext(iTestContext, driver); // setting the driver into context
// Pages Object Initialization
loginPages = PageFactory.initElements(driver, LoginPages.class);
logoutPages = PageFactory.initElements(driver, LogoutPages.class);
adminPages = PageFactory.initElements(driver, AdminPages.class);
}
运行此命令时,它将平稳运行并且不会产生错误(如果您认为我将如何传递ITestcontext上下文,则在内部进行处理)
现在,驱动程序已作为对象添加到ITestcontext中;
现在在侦听器中访问驱动程序:
@Override
public synchronized void onTestFailure(ITestResult result) {
WebDriver driver = (WebDriver) result.getTestContext().getAttribute("driver"); // here we are accessing the driver object that we added in Test class
}
2。在范围报告3中保存ScreenShot(我在Maven中使用依赖项3.1.5)
@Override
public synchronized void onTestFailure(ITestResult result) {
System.out.println("!!!!!!!!!!!!!!!!!!!! Test Failed !!!!!!!!!!!!!!!!!!!!");
WebDriver driver = (WebDriver) result.getTestContext().getAttribute("driver"); // accessing driver here
String feature = getClass().getName();
String screenShot;
try {
screenShot = CaptureScreenshot.captureScreen(driver, CaptureScreenshot.generateFileName(feature));
test.addScreenCaptureFromPath(screenShotPath); // I am assuming that the "screenShot" is fully qualified path with extension e.g "C:\Users\12345\Desktop\sfgfdh.PNG"
} catch (IOException ex) {
LogManager.logger.log(Level.INFO, "Exception: " + ex.getMessage());
}
}
3。您的代码有什么问题吗?
否
您只需要在Listener类中使用驱动程序,并在扩展报告中添加屏幕截图时, 确保屏幕快照的路径正确并且是带有扩展名的完全限定路径。
如果您遇到此问题,请告诉我。