Writing first cucumber test

Just follow below steps to write first cucumber test in IntelliJ IDEA.

  1. Create a maven project.
  2. Add feature file containing scenarios in Test Resources directory.
  3. Add the maven dependency for Cucumber.
  4. Write step definitions and execute the tests from feature file.

Here is the feature file that we created and put in the test resources folder.

@selenium
Feature: My feature

  Scenario: Verify title
    Given I am on the www.softpost.org home page
    Then I verify that title contains tutorials

Note that we have not added the step definitions for above steps in feature file. When you execute the feature file, you will see below errors.

You can implement missing steps with the snippets below:

@Given(“^I am on the www\\.softpost\\.org home page$”)
public void i_am_on_the_www_softpost_org_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}

@Then(“^I verify that title contains tutorials$”)
public void i_verify_that_title_contains_tutorials() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}

Undefined step: Given I am on the www.softpost.org home page

Undefined step: Then I verify that title contains tutorials

Writing the step definitions 

You can copy above method snippets and write your own code inside it. Alternatively, if you have cucumber for Java and Gherkin plugin installed in IntelliJ IDEA, then you can create the step definitions by pressing alt + enter on the steps in Feature file as shown in below image.

Creating cucumber step definitions in IntelliJ IDEA

Creating cucumber step definitions in IntelliJ IDEA

Here is the class with step definitions.

package org.softpost;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import junit.framework.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * Created by Sagar on 12-07-2016.
 */
@SuppressWarnings("ALL")
public class seleniumsteps {
    WebDriver driver;

    @Before
    public void launchBrowser(){
        driver = new FirefoxDriver();
    }

    @Given("^I am on the www\\.softpost\\.org home page$")
    public void i_am_on_the_www_softpost_org_home_page() throws Throwable {
        driver.get("https://www.softpost.org");
    }

    @Then("^I verify that title contains tutorials$")
    public void i_verify_that_title_contains_tutorials() throws Throwable {
        Assert.assertTrue(driver.getTitle().toLowerCase().contains("tutorials"));
    }

    @After
    public void killBrowser(){
        driver.close();
        driver.quit();
    }
}

After writing the step definitions, you can create the run configuration for feature file as shown in below image. Note that Glue property is very important. You should specify the package name that contains step definitions in the Glue.

glue in Cucumber run configuration

glue in Cucumber run configuration

Then you can right click on feature file and run the test as shown in below image.

Running cucumber feature file in IntelliJ IDEARunning cucumber feature file in IntelliJ IDEA

If everything is ok, tests will run successfully.

Cucumber Reports in IntelliJ IDEA

You can view report of execution as shown in below images.

Viewing cucumber execution report in IntelliJ IDEA

Viewing cucumber execution report in IntelliJ IDEA

 

HTML report of cucumber tests

HTML report of cucumber tests

 

 

 

 

You may also like...