无法截取失败的测试用例的屏幕截图
Unable to take screenshot of failed test cases
我正在尝试截取失败的测试用例的屏幕截图,但收到与自动化扩展相关的错误:
public static String captureScreenshot(WebDriver driver, String screenshotName)
{
try
{
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String userDirector = System.getProperty("user.dir");
String resultFile = userDirector
+ "\src\main\java\utils\screenshots\"+screenshotName+".png";
File destination = new File(resultFile);
FileUtils.copyFile(source, destination);
System.out.println("Screenshot Taken");
return resultFile;
}
catch(Exception e)
{
System.out.println("Exception while taking screenshot" + e.getMessage());
return e.getMessage();
}
这是我的 class 调用此方法的地方
@AfterMethod (alwaysRun = true)
public void tearDown(ITestResult result) throws InterruptedException, IOException{
if (result.getStatus()==ITestResult.FAILURE){
String screenshot_path=Screenshot.captureScreenshot(driver, result.getName());
String image = logger.addScreenCapture(screenshot_path);
logger.log(LogStatus.FAIL, "Test Script Failed and TestScript name is ", image);
Thread.sleep(3000);
}
report.endTest(logger);
report.flush();
//else if ((result.getStatus()== ITestResult.SUCCESS)){
// logger.log(LogStatus.PASS, "test case Pass1" + result.getName());
//}
//TestBase.updateResult(1, "result.getName()", "Pass", "I dont Know");
driver.close();
}
在 运行 之后我收到以下错误:
TestCases failed and TestScript is KeepCookiesCheckInvalidScenario
Exception while taking screenshotunknown error: cannot get automation
extension
from unknown error: page could not be found: chrome-
extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html
(Session info: chrome=60.0.3112.113)
(Driver info: chromedriver=2.27.440174
(e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393
x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.20 seconds
我也试过不关闭这里的驱动,但是还是不行,求助
如果您使用的是 chrome,请尝试使用以下代码获取 chrome 属性:
System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--enable-automation");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
我希望你能使用上面的代码解决你的问题。享受!
我正在尝试截取失败的测试用例的屏幕截图,但收到与自动化扩展相关的错误:
public static String captureScreenshot(WebDriver driver, String screenshotName)
{
try
{
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String userDirector = System.getProperty("user.dir");
String resultFile = userDirector
+ "\src\main\java\utils\screenshots\"+screenshotName+".png";
File destination = new File(resultFile);
FileUtils.copyFile(source, destination);
System.out.println("Screenshot Taken");
return resultFile;
}
catch(Exception e)
{
System.out.println("Exception while taking screenshot" + e.getMessage());
return e.getMessage();
}
这是我的 class 调用此方法的地方
@AfterMethod (alwaysRun = true)
public void tearDown(ITestResult result) throws InterruptedException, IOException{
if (result.getStatus()==ITestResult.FAILURE){
String screenshot_path=Screenshot.captureScreenshot(driver, result.getName());
String image = logger.addScreenCapture(screenshot_path);
logger.log(LogStatus.FAIL, "Test Script Failed and TestScript name is ", image);
Thread.sleep(3000);
}
report.endTest(logger);
report.flush();
//else if ((result.getStatus()== ITestResult.SUCCESS)){
// logger.log(LogStatus.PASS, "test case Pass1" + result.getName());
//}
//TestBase.updateResult(1, "result.getName()", "Pass", "I dont Know");
driver.close();
}
在 运行 之后我收到以下错误:
TestCases failed and TestScript is KeepCookiesCheckInvalidScenario
Exception while taking screenshotunknown error: cannot get automation
extension
from unknown error: page could not be found: chrome-
extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html
(Session info: chrome=60.0.3112.113)
(Driver info: chromedriver=2.27.440174
(e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393
x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.20 seconds
我也试过不关闭这里的驱动,但是还是不行,求助
如果您使用的是 chrome,请尝试使用以下代码获取 chrome 属性:
System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--enable-automation");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
我希望你能使用上面的代码解决你的问题。享受!