1

Is it not possible to have both SUCCCESS and FAILURE Outcome in the story?

Lifecycle:
After:
Outcome: FAILURE 
Given I capture page screenshot
Given I close browser
Outcome: SUCCESS
Given I close browser

Scenario: Sample one
Given I open browser
When I do something

Scenario: Sample two
Given I open browser
When I do another thing

For example, for failures I want to take a screenshot before closing the browser. If successful I just want to close the browser.

I know I can just close the browser at the end of all my scenarios and only have the failure outcome remain. I would like to know if there is a way to do this in the Lifecycle After. Thanks.

tic
  • 83
  • 1
  • 9

1 Answers1

0

Quoting OP:

For example, for failures I want to take a screenshot before closing the browser. If successful I just want to close the browser.

Now, the interesting question will be which framework are you using for your assertions? I'll assume you use Junit which comes bundled with JBehave as JBehave relies on knowing there is an error by JUnit's thrown exception.

The idea is to: a) throw an exception when an error occurs (so need to check on every step) b) take a screenshot c) continue with testing (i.e. closing the browser)

So in order to throw an exception, you really do no need to do much as this is done automatically when using JUnit's Assert statement.

so for example

Assert(username.equals("expected_user").isTrue();

If the above fails an exception will be thrown. You can capture it as such:

public class RunnerExtension implements AfterTestExecutionCallback {

    @Override
    public void afterTestExecution(ExtensionContext context) throws Exception {
        Boolean testResult = context.getExecutionException().isPresent();
        System.out.println(testResult); //false - SUCCESS, true - FAILED
    }
}

@ExtendWith(RunnerExtension.class)
public abstract class Tests {

}

Taken from this answer: JUnit5 - How to get test result in AfterTestExecutionCallback

So basically you override the standard behaviour -after- each assertion has been executed. In the case above you can add (when an exception is thrown --> take screenshot).

Here is the take a screenshot code for Selenium-Java:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

Hope the above helps!

Xwris Stoixeia
  • 1,831
  • 21
  • 22