【发布时间】:2020-02-22 13:27:58
【问题描述】:
我尝试了各种方法,但仍然获得了 NPE。请看下面的代码sn-ps。
TestBase.class
public WebDriver initializeDriver() { // nothing special }
public void takeScreenshot(String testResult) {
try {
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File(
"//Users//roman//Desktop//MainProject//SeleniumJava//ScreenshotsFailure//"
+ testResult + "_" + "screenshot.png"));
//
// System.getProperty("user.dir")
log.info("Screenshot of " + testResult + "'s failure has been created successfully");
} catch (IOException e) {
e.printStackTrace();
log.error("Screenshot of " + testResult + "'s failure has not been created");
}
}
Listeners.class
public class Listeners implements ITestListener {
TestBase testBase = new TestBase();
Date currentDate;
@Override
public void onTestFailure(ITestResult iTestResult) {
currentDate = new Date();
testBase.takeScreenshot(iTestResult.getName() + "_" + currentDate.getTime());
}
HomePageTest.class
@Test
void my() {
Assert.assertEquals(driver.getTitle(), "adb");
// intentionally cause False to take a screenshot of this failure
}
所以发生 NPE 是这两行:
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
和
testBase.takeScreenshot(iTestResult.getName() + "_" + currentDate.getTime());
如何解决这个问题? 非常感谢!
【问题讨论】:
标签: java selenium-webdriver automated-tests testng ui-automation