Monday, August 13, 2018

Cucumber Setup with Maven

Enable Cucumber in Java project

1) Enable using maven

Create Maven project and Add below dependency in pom.xml
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
<version>1.0.11</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.0.11</version>
<type>pom</type>
</dependency>

2) Download and install cucumber plugin for eclipse

http://marketplace.eclipse.org/content/cucumber-jvm-eclipse-plugin

3) Create feature file in your project

To create a feature file

    1. In the Project tool window, right-click a directory, where feature files should be created.
    2. On the context menu of the target directory, choose New | File, and in the New File dialog box, type <name>.feature.

4) Write Steps in feature file

Feature: Login 

  @smoke
  Scenario: Login Functionality
    Given User details
    When UI submits the User data
    Then return Message user is authenticated

5) We have cucumber plugin installed in eclipse IDE so right click on feature file and run it.

It will show you below output on console
WARNING: Cucumber-JVM's --format option is deprecated. Please use --plugin instead.
1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.000s
You can implement missing steps with the snippets below:
@Given("^User details$")
public void user_details() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^UI submits the User data$")
public void ui_submits_the_User_data() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^return Message user is authenticated$")
public void return_Message_user_is_authenticated() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Undefined step: Given User details
Undefined step: When UI submits the User data
Undefined step: Then return Message user is authenticated
1 scenario (0 passed)
3 steps (0 passed)
Process finished with exit code 0
We have not implemented steps mentioned in feature file so thats why it is giving this error.

Step 6) Copy methods present below "You can implement missing steps with the snippets below:" string in console.

Create new class in same  package and paste them in that.
Will create TestSuit.java dn paste it.
package features;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class TestSuit {

    @Given("^User details$")
    public void user_details() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @When("^UI submits the User data$")
    public void ui_submits_the_User_data() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Then("^return Message user is authenticated$")
    public void return_Message_user_is_authenticated() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }



}

Step 7) Again right click in feature file and run it .

Now it will give below error because we have not implemented above methods and they are still having
 throw new PendingException();
in theire methods
WARNING: Cucumber-JVM's --format option is deprecated. Please use --plugin instead.
Skipped step
Skipped step
Skipped step
1 scenario (0 passed)
3 steps (0 passed)
1 Scenarios (1 pending)
3 Steps (2 skipped, 1 pending)
0m0.742s
cucumber.api.PendingException: TODO: implement me
at features.TestSuit.user_details(TestSuit.java:13)

Step 8) Either implement your logic in these methods or for just testing purpose remove these exception from methods.

now code will look like
package features;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class TestSuit {

    @Given("^User details$")
    public void user_details() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        
    }

    @When("^UI submits the User data$")
    public void ui_submits_the_User_data() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
       
    }

    @Then("^return Message user is authenticated$")
    public void return_Message_user_is_authenticated() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
       
    }



}

Step 9) Run feature file again

you will get below output
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.559s
1 scenario (1 passed)
3 steps (3 passed)
Process finished with exit code 0
Now in this steps all the steps are passed.
We are done with cucumber integration.

No comments:

Post a Comment

How to check whether operating system is 64 bit or 32bit?

What is 32 and 64 bit operating system? The terms 32-bit and 64-bit refer to the way a computer's processor that is CPU, handles info...