'Does if else concept available in feature file (Gherkin language)?

Is there anyway where we can use if/else concept in feature file? For example:

  Scenario: User should be able to check login page
  Given I am on login page
  When I click on SignIn button 
  Then I should be in home page 
  If yes
  Then I will create a new profile
  Else
  Then I will logout from page 


Solution 1:[1]

Not that I am aware of. Gherkin (and cucumber) are best used when they specify discreet business cases though, and should be repeatable, else they get hard to follow and test. It looks like you have two stories here at least:

Scenario: A new user should be asked to sign in
  Given I am a new user
  And I navigate to the login page
  When I click on SignIn button
  I should not be able to get to the home page

Scenario: An existing user should be able to log in
  Given I am an existing user
  And I navigate to the login page
  And I submit valid credentials
  When I click on SignIn button
  I should be taken to the home page

Solution 2:[2]

No you can't and you shouldn't. Feature files are for business behaviour, not programming.

From your scenario I think you are trying to deal with different behaviour, depending on whether you are registered or not. To do this you would write two scenarios

Given I am registered
When I 
Then I should ....

Given I am a new user
When I ...
Then I should be asked to register

Notice how these scenarios don't describe 'how' anything is done. Anything like `I click on foo' in feature is a smell and should be avoided.

Solution 3:[3]

What about if we are using Gherkin in a smoke test type situation and we need to ensure something exists in the database using only the UI?

Scenario: I need to create one (and only one) Box before I run the rest of my smoke tests
Given I login as Admin
When I am on the Box list Page
Then the test passes if the Box named "QA SmokeTest" exists
When I click the Add New Box Button
And enter the details for a New Box
And press Save New Box
Then the test passes if the Box named "QA SmokeTest" exists

The re-use of the same Then step twice is essentially an if-else that will make sure my Box exists so that I can run my other tests in the smoke test suite that require a Box.

But that is dependent on being able to stop the Scenario execution in the test runner or doing something extraneous like:
ScenarioContext.Current["TestPassed"] = true;
and then in each of the steps
if(ScenarioContext.Current.Get<bool>("TestPassed")) return;

Solution 4:[4]

You would like to twist your scenario as below

Scenario: User should be able to check login page
  Given I am on login page
  When I click on SignIn button 
  Then I should be in home page 
  And check if home page exist and store result as existence
  If existence is true
  Then I will create a new profile
  And Else
  Then I will logout from page 

In the step definition for And check if home page exist and store result as existence is Check home page exist or not by checking the driver.findElement returns null or not and based on it store it as true or false in thread context instance with key as existence

In the step definition for If existence is true retrievie the thread contenxt instance data for key existence as asserts it if its true or false and store it with key conditionresult in thread context instance.

In the step definition for Then I will create a new profile before creating the profile retrieve thread context instance data for key conditionresult and check if value is true then create the new profile and set again thread context instance for conditionresult as null with in the condition loop.

In the step definition for And else retrieve the thread context instance for conditionresult and check if its false and if yes set again thread context instance for conditionresult as true

In the step definition for Then I will logout from page retrieve the thread context instance for conditionresult and check if its true then perform logout and set again thread context instance for conditionresult as null with in the condition loop.

Solution 5:[5]

You can use parameter in feature file and implement the If else in the Code based on the parameter passed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jon Bates
Solution 2 diabolist
Solution 3
Solution 4 Sonali Das
Solution 5 Nathan Tuggy