Skip to content

Commit ccd7ee9

Browse files
authored
Merge pull request #2493 from codehackerr/develop
2499: Improve auto-screenshot on error in report
2 parents fcc1ad3 + 481cead commit ccd7ee9

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public String getEmbedFileName(ResourceType resourceType) {
160160
return scenario.getUniqueId() + "_" + System.currentTimeMillis() + (extension == null ? "" : "." + extension);
161161
}
162162

163-
public Embed saveToFileAndCreateEmbed(byte[] bytes, ResourceType resourceType) {
163+
private Embed saveToFileAndCreateEmbed(byte[] bytes, ResourceType resourceType) {
164164
File file = new File(featureRuntime.suite.reportDir + File.separator + getEmbedFileName(resourceType));
165165
FileUtils.writeToFile(file, bytes);
166166
return new Embed(file, resourceType);
@@ -465,6 +465,12 @@ public StepResult execute(Step step) {
465465
error = stepResult.getError();
466466
logError(error.getMessage());
467467
}
468+
if (engine.driver != null) {
469+
engine.driver.onFailure(currentStepResult);
470+
}
471+
if (engine.robot != null) {
472+
engine.robot.onFailure(currentStepResult);
473+
}
468474
} else {
469475
boolean hidden = reportDisabled || (step.isPrefixStar() && !step.isPrint() && !engine.getConfig().isShowAllSteps());
470476
currentStepResult.setHidden(hidden);
@@ -484,14 +490,6 @@ public StepResult execute(Step step) {
484490
stopped = true;
485491
}
486492
}
487-
if (stepResult.isFailed()) {
488-
if (engine.driver != null) {
489-
engine.driver.onFailure(currentStepResult);
490-
}
491-
if (engine.robot != null) {
492-
engine.robot.onFailure(currentStepResult);
493-
}
494-
}
495493
if (executed && !dryRun) {
496494
featureRuntime.suite.hooks.forEach(h -> h.afterStep(currentStepResult, this));
497495
}

karate-junit5/src/test/java/karate/SampleTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import com.intuit.karate.junit5.Karate;
44

55
class SampleTest {
6+
@Karate.Test
7+
// Uncomment @ignore on embed.feature for development verification
8+
Karate testScreenshotIsEmbeddedOnTheCorrectStepOnFailure() {
9+
return Karate.run("embed").relativeTo(getClass());
10+
}
611

712
@Karate.Test
813
Karate testSample() {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Feature: browser automation demo
2+
3+
Background:
4+
* configure driver = { type: 'chrome' }
5+
@ignore
6+
Scenario: try to login to github and then do a google search
7+
Simulates step failure for testing embedded screenshots. Remove @ignore for developement
8+
9+
Given driver 'https://github.com/login'
10+
And input('#login_field', 'YYY')
11+
And input('#password', 'world')
12+
When submit().click("input[name=commit]")
13+
Then match html('.flash-error') contains 'Bad username or password.'
14+
15+
Given driver 'https://google.com'
16+
# And click('{}Accept all')
17+
And input("[name=q][name=q]", 'karate dsl')
18+
When submit().click("input[name=btnI]")
19+
Then waitForUrl('https://github.com/karatelabs/karate')
20+
21+
Scenario: Dummy not to fail build
22+
* print 'dummy'

0 commit comments

Comments
 (0)