From 481ceadda9b35505967d024dfd749d56e3094e8c Mon Sep 17 00:00:00 2001 From: Abdul Salam Date: Thu, 1 Feb 2024 21:21:50 -0800 Subject: [PATCH] 2499: Improve auto-screenshot on error in report --- .../intuit/karate/core/ScenarioRuntime.java | 16 ++++++-------- .../src/test/java/karate/SampleTest.java | 5 +++++ .../src/test/java/karate/embed.feature | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 karate-junit5/src/test/java/karate/embed.feature diff --git a/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java b/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java index 672dfdb8f..73a86e41a 100644 --- a/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java +++ b/karate-core/src/main/java/com/intuit/karate/core/ScenarioRuntime.java @@ -160,7 +160,7 @@ public String getEmbedFileName(ResourceType resourceType) { return scenario.getUniqueId() + "_" + System.currentTimeMillis() + (extension == null ? "" : "." + extension); } - public Embed saveToFileAndCreateEmbed(byte[] bytes, ResourceType resourceType) { + private Embed saveToFileAndCreateEmbed(byte[] bytes, ResourceType resourceType) { File file = new File(featureRuntime.suite.reportDir + File.separator + getEmbedFileName(resourceType)); FileUtils.writeToFile(file, bytes); return new Embed(file, resourceType); @@ -465,6 +465,12 @@ public StepResult execute(Step step) { error = stepResult.getError(); logError(error.getMessage()); } + if (engine.driver != null) { + engine.driver.onFailure(currentStepResult); + } + if (engine.robot != null) { + engine.robot.onFailure(currentStepResult); + } } else { boolean hidden = reportDisabled || (step.isPrefixStar() && !step.isPrint() && !engine.getConfig().isShowAllSteps()); currentStepResult.setHidden(hidden); @@ -484,14 +490,6 @@ public StepResult execute(Step step) { stopped = true; } } - if (stepResult.isFailed()) { - if (engine.driver != null) { - engine.driver.onFailure(currentStepResult); - } - if (engine.robot != null) { - engine.robot.onFailure(currentStepResult); - } - } if (executed && !dryRun) { featureRuntime.suite.hooks.forEach(h -> h.afterStep(currentStepResult, this)); } diff --git a/karate-junit5/src/test/java/karate/SampleTest.java b/karate-junit5/src/test/java/karate/SampleTest.java index e1218aaa7..c895e17d0 100644 --- a/karate-junit5/src/test/java/karate/SampleTest.java +++ b/karate-junit5/src/test/java/karate/SampleTest.java @@ -3,6 +3,11 @@ import com.intuit.karate.junit5.Karate; class SampleTest { + @Karate.Test + // Uncomment @ignore on embed.feature for development verification + Karate testScreenshotIsEmbeddedOnTheCorrectStepOnFailure() { + return Karate.run("embed").relativeTo(getClass()); + } @Karate.Test Karate testSample() { diff --git a/karate-junit5/src/test/java/karate/embed.feature b/karate-junit5/src/test/java/karate/embed.feature new file mode 100644 index 000000000..da8719aa4 --- /dev/null +++ b/karate-junit5/src/test/java/karate/embed.feature @@ -0,0 +1,22 @@ +Feature: browser automation demo + + Background: + * configure driver = { type: 'chrome' } + @ignore + Scenario: try to login to github and then do a google search + Simulates step failure for testing embedded screenshots. Remove @ignore for developement + + Given driver 'https://github.com/login' + And input('#login_field', 'YYY') + And input('#password', 'world') + When submit().click("input[name=commit]") + Then match html('.flash-error') contains 'Bad username or password.' + + Given driver 'https://google.com' + # And click('{}Accept all') + And input("[name=q][name=q]", 'karate dsl') + When submit().click("input[name=btnI]") + Then waitForUrl('https://github.com/karatelabs/karate') + + Scenario: Dummy not to fail build + * print 'dummy' \ No newline at end of file