Forum Discussion

TarekO's avatar
TarekO
New Contributor
3 years ago

How to fetch if the test passed or failed in the After hook

Hi,

I would like to know how can I implement an After hook that does an action if the test failed or passed.

For example, please check the follow snippet of a sample where I can handle the step state with the reason of failing.

if( Step.failed() ){
   System.out.println("The test failed due to this reason " + Step.failReason());
}else if( Step.passed(){
   System.out.println("The test passed with no issues");
}

 

I am using Cucumber Open Source with Appium/Selenium Libraries.

1 Reply

  • TarekO's avatar
    TarekO
    New Contributor

    I have found a partial solution for my issue by using the After Hook:

    @After
    public void tearDown(Scenario scenario) {
    		if(scenario.isFailed()){
    			//Code here
    		}else if(scenario.getStatus().toString().equalsIgnoreCase("passed")){
    			//Code here
    		}
    		driver.quit();
    }

     

    However, the second part of my issue is to fetch the Reason why the Scenario failed. I have tried using the Arguments :

    @After
    	public void tearDown(Scenario scenario, Step step) {

    And cucumber does not support that. Is there a way I can access the failed step reason?