Search code examples
bdd

I want to write script for login functionality. There are 5 types of user for that I have written following feature file


Query: I want to login with different user for that I am parametrizing the Usertype and will verify the respective element w.r.t there access.

Now In Step file suppose eg: I have written

@Then ("^User logged in with \"([^\"]*)\"$)
Public void User_logged_in_with_Usertype() {
    If (Usertype= Admin){
    ...

So in above code in how I will get value of admin or any other user(Can we get same value from feature file or I need to write a code separately for each user)

Feature: As a user I would like to login in FMJ-Redesign application with different users

Story : User is logging in FMJ application

Scenario Outline: User is logged in with Admin user credentials

Given Navigating BU to "<Browser>"
When User clicks on Location
And ForevermarkJewellerWebsite element should be present on login page 
Then User logged in with "<Usertype>"
And User will check visibility of "<Element>"
Then User Logout Successfully

Examples:

|Browser  | UserType    | Element |
|Chrome   |  Admin      |         |
|Chrome   |  Market     |         |
|Chrome   |  Jeweller   |         |
|Chrome   |  Store      |         |

Solution

  • The way to get the value for Usertype is to have it as a parameter in the method.

    Your code looks like this:

    @Then ("^User logged in with \"([^\"]*)\"$)
    Public void User_logged_in_with_Usertype() {
        If (Usertype= Admin){
            ...
    

    I rewrote your Scenario outline a bit and ended up with this version

      Scenario Outline: User is logged in with Admin user credentials
    
        Given Navigating BU to <Browser>
        When User clicks on Location
        And ForevermarkJewellerWebsite element should be present on login page
        Then User logged in with <UserType>
        And User will check visibility of <Element>
        Then User Logout Successfully
    
        Examples:
    
          | Browser | UserType | Element |
          | Chrome  | Admin    |         |
          | Chrome  | Market   |         |
          | Chrome  | Jeweller |         |
          | Chrome  | Store    |         |
    

    This allowed me to write the step like this:

    @Then("^User logged in with (.*)$")
    public void user_logged_in_with(String userType) throws Throwable {
        if (userType.equals("Admin")) {
            // implement our behaviour here
        }
    }
    

    There are a few differences to notice here:

    • I removed the quotes around the parameters in the example
    • I added the value of UsertType as a parameter to the method user_logged_in_with

    These changes simplified the regular expression needed and allows you to implement different behaviour for different UserTypes.

    I would probably implement four different steps for the user types you have. This would give me simpler steps, I could avoid the condition. The price would be four methods.

    If I did that, the resulting implementation would look like this instead:

    @Then("^User logged in with Admin$")
    public void user_logged_in_with_Admin() throws Throwable {
        // implement our behaviour here
    }
    

    This simplified the regular expression even more and removed the need for a capture group.