Sunday, 29 April 2018

Gherkin: * keyword

You can replace the keywords But, Given, When, Then, And with a ‘*’. It is another way of writing, but to the cucumber both the styles are same.

Example1
Scenario: Successful Login 
 User should be logged in successfully by providing correct username and password.
  
 Given I am registered user to the application 
 When I log in with user name "krishna" and password "password123" 
 Then I should be logged in successfully 
 And I should see a personalized greeting message

Above scenario can be written like below.

Scenario: Successful Login 
 User should be logged in successfully by providing correct username and password.
  
 * I am registered user to the application 
 * I log in with user name "krishna" and password "password123" 
 * I should be logged in successfully 
 * I should see a personalized greeting message

Example 2
Scenario: Session Expired 
 User should be redirected to login page on session expiration
 
 Given I am the logged in user to the application 
 But My session is expired 
 When I try to interact with the application 
 Then I should be notified about session expiration 
 And I should be redirected to the login page 

Above scenario can be written like below.

Scenario: Session Expired 
 User should be redirected to login page on session expiration
 
 * I am the logged in user to the application 
 * My session is expired 
 * I try to interact with the application 
 * I should be notified about session expiration 
 * I should be redirected to the login page


I personally I preferred the approach of using the keyword When, Then, But, And keywords. You can choose either of the approaches.

Login.feature

Feature: Login 

 Login should be quick and friendly. When user logged in with correct credentials, then he would be greeted with welcome message.
 If user session is expired, he should be reverted back to login page.
 
Scenario: Successful Login 
 User should be logged in successfully by providing correct username and password.
  
 * I am registered user to the application 
 * I log in with user name "krishna" and password "password123" 
 * I should be logged in successfully 
 * I should see a personalized greeting message
 
Scenario: Session Expired 
 User should be redirected to login page on session expiration
 
 * I am the logged in user to the application 
 * My session is expired 
 * I try to interact with the application 
 * I should be notified about session expiration 
 * I should be redirected to the login page

Cucumber generates below snippet.

@Given("^I am registered user to the application$")
public void iAmRegisteredUserToTheApplication() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I log in with user name \"([^\"]*)\" and password \"([^\"]*)\"$")
public void iLogInWithUserNameAndPassword(String arg1, String arg2) throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I should be logged in successfully$")
public void iShouldBeLoggedInSuccessfully() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I should see a personalized greeting message$")
public void iShouldSeeAPersonalizedGreetingMessage() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I am the logged in user to the application$")
public void iAmTheLoggedInUserToTheApplication() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^My session is expired$")
public void mySessionIsExpired() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I try to interact with the application$")
public void iTryToInteractWithTheApplication() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I should be notified about session expiration$")
public void iShouldBeNotifiedAboutSessionExpiration() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^I should be redirected to the login page$")
public void iShouldBeRedirectedToTheLoginPage() throws Exception {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment