问题
Currently I'm triggering my automation tests in the cloud using a Ubuntu instance.
The Ubuntu instance has a Jenkins instance running and also Selenium Hub and Node. It seems the screenshot images dont get saved within the builds\3\cucumber-html-reports\embeddings on a ubuntu system however on a windows system this issue does not exist.
I have currently added the below logic to capture and attach images to the Jenkins Cucumber reports however currently the images are not getting attached to the reports:
@After
public void after(Scenario scenario) {
if (scenario.isFailed()) {
try {
WebDriver augmentedDriver = new Augmenter().augment(getDriver());
byte[] s = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.BYTES);
scenario.embed(s, "image/png");
Example Report (Generated in Jenkins), please note the images are getting saved and attached to the reports when running the tests locally.
回答1:
If this does not work then you may give a thought to below way of attaching screen shot to report on jenkins.
Hooks looks like below -
@After
public void afterScenario(Scenario scenario){
try{
if(scenario.isFailed()){
ExtentTestManager.addScreenShotsOnFailure();
// More code goes here....
Method to capture image - please customize path as per your need.
public static void addScreenShotsOnFailure() {
i = i + 1;
File scrFile = ((TakesScreenshot) DriverManager.getDriver()).getScreenshotAs(OutputType.FILE);
Date d = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("E dd MMM HH:mm:ss z yyyy");
String strDate = formatter.format(d);
screenshotName = strDate.replace(":", "_").replace(" ", "_") + "_"+i+".jpg";
try {
FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "/target/extent-report/" + screenshotName));
} catch (IOException e) {
e.printStackTrace();
}
}
回答2:
I do not have direct answer for ubuntu problem. But, try adding path of captured images to your cucumber report as html links.
@After
public void after(Scenario scenario){
if (scenario.isFailed()) {
try {
WebDriver augmentedDriver = new Augmenter().augment(getDriver());
File path = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(path,new File(localRepoPath));
String html = "<html><body><a href=\">" + localRepoPath + "\"> screenshot </a></body></html>";
scenario.embed(html.getBytes(), "text/html");
}catch(Exception e){
// Do Something
}
}
}
来源:https://stackoverflow.com/questions/56360891/how-to-capture-images-and-attach-them-to-cucumber-reports-jenkins-using-seleni