How to mark a step as failed and continue execution afterwards

4,836 views
Skip to first unread message

Udaya Kumar Anem

unread,
Sep 11, 2016, 3:13:07 AM9/11/16
to Cukes
Hi all,

I have a scenario where i need to validate object existence in a form.

If some object is missing, then in Cucumber reports, it should report the step as failed and attach the screenshot and then go for the next object validation(Here the execution should not aboard because this object does not exist.)

if(blnFail)
 embedScreenshotToCucumberReport
();


 public void embedScreenshotToCucumberReport()
 
{
 
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
 scenario
.embed(screenshot, "image/png");
 
}


Here in this case the screenshot is attached to the step in Cucumber report, but the step is not marked as failed. I want to mark the step as failed which may cascade to the Scenario. How can i do that?

Thanks,
Uday

Paolo Ambrosio

unread,
Sep 12, 2016, 1:11:52 AM9/12/16
to cu...@googlegroups.com
Cucumber will always stop the execution of a scenario at the first
failed scenario. You might want to pass all form fields as a data
table to the same step:

Then the customer is asked to fill:
| favourite vegetable |
| pet's name |

If the stakeholders have a name for those fields you can "push the how
down the stack" and use the business name for it without going into
details in the scenario. Let's say that asking for "favourite
vegetable" and "pet's name" is called by the business "April fool's
fields", you can rewrite it as:

Then the customer is asked to fill the April fool's fields

...and in the step definitions:

@Then("^the customer is asked to fill the April fool's fields$")
public void checkAprilFoolsFields() {
checkFormField("favourite vegetable");
checkFormField("pet's name");
}

private void checkFormField(String fieldName) { ... }


As a general advice, in this list you'll get better answers if you
explain your problem instead of just asking how to implement what you
think it is a solution to it. In this case I had to make assumptions
on what you were trying to do.

Also the first hit on Google for "cucumber carry on after failed step"
linked me to https://github.com/cucumber/cucumber-ruby/issues/155#issuecomment-2408305

(OK, to be fare it was a broken link, and had to find issue 155 in
cucumber-ruby instead of cucumber)


Hope it helps,
Paolo

Udaya Kumar Anem

unread,
Nov 18, 2016, 6:42:16 AM11/18/16
to Cukes
Hi,

Can someone help in the above query?

Say i have a scenario, where i need to validate 10 objects exists in the first page.

If any of the field say(2nd Object) does not exists in the application, then in cucumber reports the step should be marked as Failed and eventually entire scenario should be reported as failed but the script execution continue to validate the remaining objects(from 3rd object to 10th object). The script should not abort at 2nd object not found.

Can someone help how to achieve this?

Please let me know if anything above is not clear.

Thanks,
Uday

Andrew Premdas

unread,
Nov 18, 2016, 8:45:06 AM11/18/16
to cu...@googlegroups.com
On 18 November 2016 at 11:42, Udaya Kumar Anem <uday...@gmail.com> wrote:
Hi,

Can someone help in the above query?

Say i have a scenario, where i need to validate 10 objects exists in the first page.

If any of the field say(2nd Object) does not exists in the application, then in cucumber reports the step should be marked as Failed and eventually entire scenario should be reported as failed but the script execution continue to validate the remaining objects(from 3rd object to 10th object). The script should not abort at 2nd object not found.

Can someone help how to achieve this?

Please let me know if anything above is not clear.

Thanks,
Uday

The way to do this is to push the how down. Instead of trying to validate the existence of 10 objects in your scenario, group the 10 objects together and give them a name. Then validate this new single thing. In the process of validating your new object collect the errors and then report them back as a single failure.
Lets say your objects are bar1-10 and your new thing is foo. Your cucumber step would say

Then foo should be valid

Cukes knows nothing about bar

and this would be implemented something like

Then 'foo should be valid' do
  check_foo
end

which in turn is implemented something like

def check_foo
  # this is where we start to know about bar objects
  for bar1 to bar10 do
    check_bar
    ...

  return errors
end

I'll leave you to work out the details of how you implement this which is dependent on your platform.

TLDR: not a job for cukes. A job for you lower level test code.

All best

Andrew


On Sunday, 11 September 2016 12:43:07 UTC+5:30, Udaya Kumar Anem wrote:
Hi all,

I have a scenario where i need to validate object existence in a form.

If some object is missing, then in Cucumber reports, it should report the step as failed and attach the screenshot and then go for the next object validation(Here the execution should not aboard because this object does not exist.)

if(blnFail)
 embedScreenshotToCucumberReport
();


 public void embedScreenshotToCucumberReport()
 
{
 
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
 scenario
.embed(screenshot, "image/png");
 
}


Here in this case the screenshot is attached to the step in Cucumber report, but the step is not marked as failed. I want to mark the step as failed which may cascade to the Scenario. How can i do that?

Thanks,
Uday

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
------------------------
Andrew Premdas

Udaya Kumar Anem

unread,
Nov 20, 2016, 10:57:15 PM11/20/16
to Cukes
Thanks for your inputs Andrew.

As you said, we already implemented the step in the same manner.
(Scenario step below:)
    Then the Mailing Information of Registration Form is displayed with
     
| Address        |
     
| City           |
     
| State/Province |
     
| Postal Code    |
     
| Country        |


Step definition:
@Then("^the Mailing Information of Registration Form is displayed with$")
 
public void verifyFieldsMailingSection(List<String> strFieldsInSection) throws Throwable
 
{
 
for(String strField:strFieldsInSection)
 
{
 
switch(strField)
 
{
 
case "Address":
 
if(objRegister.verifyAddress1())
 reportStatus
(StepStatus.PASS, "'Address1' field exists in 'Mailing Information' as expected");
 
else
 reportStatus
(StepStatus.FAIL, "'Address1' field does not exists in 'Contact Information'");
 
 
if(objRegister.verifyAddress2())
 reportStatus
(StepStatus.PASS, "'Address2' field exists in 'Mailing Information' as expected");
 
else
 reportStatus
(StepStatus.FAIL, "'Address2' field does not exists in 'Contact Information'");
 
 
case "City":
 
if(objRegister.verifyCity())
 reportStatus
(StepStatus.PASS, "'City' field exists in 'Mailing Information' as expected");
 
else
 reportStatus
(StepStatus.FAIL, "'City' field does not exists in 'Contact Information'");
 
break;
 
 
case "State/Province":
 
if(objRegister.verifyState())
 reportStatus
(StepStatus.PASS, "'State / Province' field exists in 'Mailing Information' as expected");
 
else
 reportStatus
(StepStatus.FAIL, "'State / Provinc' field does not exists in 'Contact Information'");
 
break;
 
 
case "Postal Code":
 
if(objRegister.verifyPostalcode())
 reportStatus
(StepStatus.PASS, "'Postal Code' field exists in 'Mailing Information' as expected");
 
else
 reportStatus
(StepStatus.FAIL, "'Postal Code' field does not exists in 'Contact Information'");
 
break;
 
 
case "Country":
 
if(objRegister.verifyCountry())
 reportStatus
(StepStatus.PASS, "'Country' field exists in 'Mailing Information' as expected");
 
else
 reportStatus
(StepStatus.FAIL, "'Country' field does not exists in 'Contact Information'");
 
break;
 
}
 
}
 
}


Here for each object verification we have separate Page method, which basically verifies  objElement.isDisplayed(). If the object does not exists, below reporting section will be called.

Below is portion of result reporting
                case "FAIL":
                    reportExecutionLog
("fail", strStepDescription);
                   
String strFailureStepFilePath = returnScreenshotFilePath();
                    logStep
.log(LogStatus.FAIL, String.valueOf(strStepDescription) + logStep.addScreenCapture(strFailureStepFilePath));
                   
embedScreenshotToCucumberReport();
                   
break;


In the above, it is embedding the screenshot to result report, but the status of the step is shown as passed in cucumber-jvm reports. But i want to mark the step as Fail, in fact should fail the Scenario.

From the above ex. if City does not exists, mark Step as fail(which inturn should fail scenario) and the script continues to validate remaining objects.

So can you please help here?

Thanks,
Uday
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Call The Bluff

unread,
Nov 22, 2016, 5:19:22 AM11/22/16
to Cukes
Hi Uday,
  Yes there are many ways to can do to accomplish just what you  wanted to do.
   Let's walk through your case step by step and see what we can do to solve them:
  Q: Say i have a scenario, where i need to validate 10 objects exists in the first page.
  A: in your page class where you define all of objects/elements on the page you can map every object/elements to a table construct using map<String "object name", WebElement(facade) "path to the object">.  Then overtime when you are on the page you need to walk through the "Map/dictionary" to see if the object/element 's value is null or not.  If it is null, then you know the object/element does not exist or fails to load.  In the case the returned value is null, you need to write an error message to your report or your log.
  Q: If any of the field say(2nd Object) does not exists in the application, then in cucumber reports the step should be marked as Failed and eventually entire scenario should be reported as failed but the script execution continue to validate the remaining objects(from 3rd object to 10th object). The script should not abort at 2nd object not found.
  A:  it all depends on your code.  If you want to assert that the target object is null , then cucumber will shows an error and stop the execution.  If you use the conditional statement like "If", then you have a control to tell cucumber not to stop. and you can dictate whether a particular condition is a pass or a failure..
  Now cucumber might have some library functions that can you help you with this situation.  But if you you cucumber with Serenity that you have the option to bypass the error conditions.  Basically, Serenity allows you to continue to execute the rest of your code even when it encounters an error.  At the end of the execution it will report the error(s) but will not stop at the point where the error occurs.
  I won't go into code details here, but i'm 100% sure you can accomplish just what you want to do based on the outline described above.
  Give it a shot! and good luck!
  Call The Bluff

Andrew Premdas

unread,
Nov 25, 2016, 6:37:59 AM11/25/16
to cu...@googlegroups.com
I'm sorry I can't really help you more here. What you are doing here is a crazy amount of work for very little benefit. I'd suggest going back to first principles, reading at minimum the Cucumber book and thinking carefully about what value you get from your scenarios. Writing a new method for every form you validate in an application is not sustainable..

To unsubscribe from this group and stop receiving emails from it, send an email to cukes+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Udaya Kumar Anem

unread,
Dec 28, 2016, 6:05:24 AM12/28/16
to Cukes
apremdas,

If you heard of Page Object Model / Page Factory, we will have methods for each object(to gain more re-usability). So for each form we will have a method and for each field in the form we have a method to check its existence or some operation. The field level method calls some common routines like object exists validations etc... That is how we follow automation frameworks.

Sometimes we may also need to automate medium to low priority cases. Any tool should support these minimum requirements also. If these minimum requirements are not met, then people cannot jump to another tool for these cases, right?

Thanks,
Uday
Reply all
Reply to author
Forward
0 new messages