How to skip the rest steps dynamically while running the story

4,319 views
Skip to first unread message

Bill.pm

unread,
Sep 13, 2017, 5:00:00 AM9/13/17
to Serenity BDD Users Group
Dear All,

I have situation that I have a story A to run as precondition for every story. Story A is to register a new account. 

And I want to check in Story A that if the account is existed, then just skip the rest steps safely.

Here is story might look like:

Lifecycle: 
Before:
Given Check if username exist with primary phone 0123456789

Scenario: CS Register with valid data

Given contract data C123 and phone 0123456789

When I want to send register data for step 1
Then I get status code 204

Given Username abc and password xyz

When I want to send register data for step 2

Then I get status code 201

I intent to put the check at the first Given

My question is: Do we have a flag to ignore the scenario below if username existed?

Or is there any solution so fulfill this requirement?

Any comments or suggestion are highly appreciated.


Thanks,
Bill

SeleniumWhat?

unread,
Sep 13, 2017, 9:39:49 AM9/13/17
to Serenity BDD Users Group
Hi Bill,
  Yes you can but you have to do it differently and not at the Gherkin level.  I'll get back to you tonite when I get home.  Stay tuned.
  J.N

SeleniumWhat?

unread,
Sep 13, 2017, 8:47:47 PM9/13/17
to Serenity BDD Users Group
Hi Bill,
  When I first learnt Cucumber - like you - I misunderstood the Cucumber language.  
  Cucumber is not a programming language but rather a requirements/specification language.  There's no control flows or logic you can apply at this layer.  Cucumber is solely intended for nontechnical folks to understand the user stories that describes the expected behaviors of intended features/functionality.
  You need to include all conditional conditions ( If A, then do B. else do C ) of you test cases in one single feature file as follows:
  1 -  Scenario #1: Create a new user with a unique phone number.
  2 -  Scenario X: Exercise test case(s) that has dependence on the condition that the user must exist.
  3 -  Scenario Y: Delete the newly-created user.
                           You need to delete the newly-created user because you need the next test cases to run on the condition that the user you just created does not exist in your system.
  4  - Scenario Z: Exercise test case(s) that has no dependencies on the conditions that the user must exist.
 
  The Cucumber For Java book does talk about how to avoid - what it calls -  creating brittle test cases.  Please read the book starting from page 93.  It helps you to avoid the pitfall that you are attempting to implement.
  J.N
   

Bill.pm

unread,
Sep 14, 2017, 12:43:23 AM9/14/17
to Serenity BDD Users Group
Hi SeleniumWhat,

Thank you so much for the hint and great book also.

I understand your point clearly, Cucumber is not programming. but I really don't want to put every scenario into just ONE BIG STORY that require user existing.
So, I have to put Givenstories "Story A: create new account" at every story that dependent to user existing. The issue is the Story A is kind of slow, then it make the whole test run in more than 30 min, instead of just 15min.

But because you said so.. there is no way to add control flow to story, I think I have to balance to group some story has dependencies.

Thanks again for the hint man, you are so kind.

Bill


Vào 07:47:47 UTC+7 Thứ Năm, ngày 14 tháng 9 năm 2017, SeleniumWhat? đã viết:

SeleniumWhat?

unread,
Sep 14, 2017, 5:27:49 PM9/14/17
to Serenity BDD Users Group
Hi Bill,
  You're welcome.  Any time of I can help.
  You said: "but I really don't want to put every scenario into just ONE BIG STORY that require user existing.", but you don't have to.  I got a solution for you but I'm not available right now.  I'll get back to you when I get home.  Stay tuned
  J.N

SeleniumWhat?

unread,
Sep 14, 2017, 8:50:44 PM9/14/17
to Serenity BDD Users Group
Hi Bill,
  I'm going to walk you through the steps.  It's a long explanation, so be patient...!
  Ready?  Let's go :-).
  Let's first lay out the scenarios(test cases):
  1 - You have a "base" feature where you create a new user with a unique phone number.  Let's call it Create_New_User.feature.  
        In this feature file, create an annotation @Create-New-User.  Place this annotation at the first line (above the "Feature:" key word).
   2 - And let say you have 5 scenarios that are dependent on the condition that the new user account must exist.  Place them in the same feature file called Dependency_Test_Cases.feature:  For each scenario, create an annotation called @Test-Case-1, @Test-Case-2...Test-Case-5.  Place the corresponding annotation just above the "Scenario:" key word.
   3 - Create a feature file, and call it Delete_New_User.feature
        In this feature file, create an annotation @Delete-New-User.  Place this annotation at the first line (above the "Feature:" key word).  In this feature file you need to cover two conditions: (1) if the user exists? Remove the user.  (2) if not, do nothing.  Just exist the test case.
    4 - And let say you have 5 scenarios that are NOT dependent on the condition that the new user account must exist.  Place them in the same feature file called Non_Dependency_Test_Cases.feature:  For each scenario, create an annotation called @Test-Case-6, @Test-Case-7...Test-Case-10.  Place the corresponding annotation just above the "Scenario:" key word.

  What I just did was to breakdown the test cases into individual ones so that you can call them as you like.  They're just like independent functions which you can combine/use them in any way that you like to.
  Now let say you want to create a TestSuite that have dependency on the condition that the user must exist.  You want to test the case where the execution should follow this order: 
  1) A new user is created
  2) Test-Case-1.
  Here is how you can do that.  Create a Test Runner class "Create_New_User_And_Run_Test_Case_1.java".  The contend of the class will look like this:

import cucumber.api.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;

@RunWith(CucumberWithSerenity.class)
@CucumberOptions( 
features={"src/test/resources/features"},
tags={"@Create-New-User, @Test-Case-1"}
)
public class FutureLoginRunnerTestSuite {
}

  Do the same of any test case that you want to.  Another example, if you can to run the following test cases:
  1 - Create a new user.
  2 - Run Test-Case-1.
  5 - Run Test-Case-5.
 Then your Test Suite will look like this:

import cucumber.api.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;

@RunWith(CucumberWithSerenity.class)
@CucumberOptions( 
features={"src/test/resources/features"},
tags={"@Create-New-User, @Test-Case-1@Test-Case-5"}
)
public class FutureLoginRunnerTestSuite {
}

  Question: So... how the execution knows what you want to run? And in what order
  Answer: Your Test runner tells the execution to follow exactly order that you stated in the "tags" option.
  Now... a note of caution:  This is very important note that you need to pay attention to.  The way the cucumber works is a bit weird.  The cucumber execution actually does NOT follow what you state in your "tags" option.  It follows its now rules of execution.  The execution order is based on Alpha-Numeric order of the annotation name.  This order of execution has thrown me off when I first learnt of Cucumber.  It took me quite a while to figure it out.
  Let's  take look at our example number 1 again.  As expected, the execution will first execute @Create-New-User and then @Test-Case-1.  Now let say you have the @Test-Case-1 changed to @ATest-Case-1, then what would happen?  What would happen is that the execution order will be: @ATest-Case-1 then @Create-New-User.  Now that will break your test suite.
  In short, when you name your annotations, make sure follow the Alpha-Numeric orders that you want to execute them.
  Now, you can create any test suites that you want and you don't have to put all test cases under the same dependency any more.
  Still confused?  Just ask :-).
  Good luck!
  J.N

     

Bill.pm

unread,
Sep 21, 2017, 9:28:01 PM9/21/17
to Serenity BDD Users Group
WOW, Thanks SeleniumWhat! I'm impressed of how details you show me and very deep insight in code. It helps me to get the idea that how cucumber work in code, I am still working to find similar approach for Jbehave.

Thanks again and have a good day or night :)

Bill

Vào 07:50:44 UTC+7 Thứ Sáu, ngày 15 tháng 9 năm 2017, SeleniumWhat? đã viết:
Reply all
Reply to author
Forward
0 new messages