LeanFT with Cucumber

LeanFT supports Behavior Driven Development (BDD) using Cucumber. Cucumber is a testing framework driven by BDD. It defines application behavior with simple English text using the Gherkin language.

Note: This topic is aimed at people familiar with Cucumber. If you are new to Cucumber and would like to learn more, we recommend that you visit the Cucumber website.

Overview

You can create a LeanFT project based on Cucumber.

LeanFT includes a Cucumber project template based on JUnit and Maven. The Cucumber template can be used on IntelliJ IDEA and Eclipse. For details, see Use the Cucumber framework in Eclipse, Spring Tool Suite, or IntelliJ IDEA.

While there is no built-in template for Visual Studio, you can create a Cucumber project using SpecFlow. For details, see Use the Cucumber framework in Visual Studio using SpecFlow.

All the LeanFT tools are at your disposal when working with the LeanFT Cucumber framework, just like any other LeanFT project.

Back to top

Use the Cucumber framework in Eclipse, Spring Tool Suite, or IntelliJ IDEA

When you create a LeanFT test project that is based on Cucumber, it includes all the resources required for using Cucumber:

  • LeanFtFeature.feature file. Will include the test scenario in the Gherkin language.
  • LeanFtStepDefinition.java class. Will include the implementation of the test scenario described in the feature file.
  • pom.xml. Includes LeanFT and Cucumber dependencies for Maven.

To create a LeanFT Cucumber project

  1. Follow the instructions in Prerequisites for Maven-based projects.

  2. Create a new project.

    1. In your IDE, click FileNew > Project.
    2. Select LeanFT Cucumber Project.
    3. Enter the required information and then click Next. You must enter the Artifact ID and the Group ID.

      The project is created with all the resources required for using Cucumber.

  3. Open the pom.xml file for editing and make the following changes:

    • Update the LeanFT dependencies.
    • Add a testSourceDirectory child element to the <build> element:

      <build><testSourceDirectory>src</testSourceDirectory></build>

      This is required for running tests using Maven.

  4. Build the project to make sure all the dependencies were imported from the Maven repository.

    Run the Maven build from the command line by running the following command from the project's main directory (where the pom file is located):

    mvn clean install

  5. Open the LeanFtFeature.feature file, and write the test scenario using the Gherkin language.

    Example:

                            Feature: Shop
    
    	Scenario: Check speakers price
    	Given I navigate to www.advantageonlineshopping.com
    	When  I select the speakers category
    	And   I add the first product to the cart
    	And   I navigate to the cart
    	Then  the total price is $269.99

    To use parameters in your scenarios, adjust script to include the Scenario Outline keyword instead of the Scenario keyword, add an Examples data table with the parameters and values, and incorporate the parameters into the scripts instead of hard-coded values. The test will run consecutively for each of the values (for each row in the Examples table).

    Example:

                            Feature: Shop
    
    	Scenario Outline: Check prices
    		Given I navigate to www.advantageonlineshopping.com
    	     	When I select the <category> category
    	     	And I add the first product to the cart
    	     	And I navigate to the cart
    		Then the total price is <price>
    
    		Examples:
                   | category    | price    |
                   | speakers   | $269.99|
                   | mice       | $9.99  |
  6. In the Project pane in your IDE, right-click LeanFtTest, and then click Run LeanFtTest.

    The output console at the bottom of your IDE displays a list of empty implementations for each step of the scenario that you wrote in the LeanFT feature file.

    Example:

    @Given("^I navigate to www\\.advantageonlineshopping\\.com$")
    public void i_navigate_to_www_advantageonlineshopping_com() throws Throwable {
    	// Write code here that turns the phrase above into concrete actions
           throw new PendingException();
    }
    
    @When("^I select the speakers category$")
    public void i_select_the_speakers_category() throws Throwable {
    	// Write code here that turns the phrase above into concrete actions
          throw new PendingException();
    }
    
    @When("^I add the first product to the cart$")
    public void i_add_the_first_product_to_the_cart() throws Throwable {
    	// Write code here that turns the phrase above into concrete actions
           throw new PendingException();
    }
    
    @When("^I navigate to the cart$")
    public void i_navigate_to_the_cart() throws Throwable {
           // Write code here that turns the phrase above into concrete actions
           throw new PendingException();
    }
    
    @Then("^the total price is \\$(\\d+)\\.(\\d+)$")
    public void the_total_price_is_$(int arg1, int arg2) throws Throwable {
    	// Write code here that turns the phrase above into concrete actions
           throw new PendingException();
    }				
  7. Copy the list of empty implementations from the output console into the LeanFtStepDefinitions class.

    If you run LeanFtTest before implementing the methods, you get a pending exception.

    cucumber.api.PendingException: TODO: implement me
    ...
  8. Implement the methods in the LeanFtStepDefinitions class.

    Note: You do not need to edit the LeanFtTest.java file.

    Example:

                            package com.company;
    
    
    import java.io.IOException;
    import com.hp.lft.report.*;
    import com.hp.lft.sdk.*;
    import com.hp.lft.sdk.web.*;
    import com.hp.lft.verifications.Verify;
    import cucumber.api.*;
    import cucumber.api.java.*;
    import cucumber.api.java.en.*;
    
    public class LeanFtStepDefinitions {
    	Browser browser;
    
    	public LeanFtStepDefinitions() {}
    
    	@Before
    	public void setUp() throws GeneralLeanFtException {
    		browser = BrowserFactory.launch(BrowserType.CHROME);
    		browser.deleteCookies();
    	}
    
    	@After
    	public void cleanUp() throws GeneralLeanFtException {
    		browser.close();
    	}
    
    	//Implementation of the feature’s steps
    	@Given("^I navigate to www.advantageonlineshopping.com$")
    	public void i_navigate_to() throws Throwable {
    		browser.navigate("http://www.advantageonlineshopping.com");
    	}
    
    	@When("^I select the ([^\"]*) category")
    	public void i_select_category(String categoryName) throws GeneralLeanFtException {
    		String innerText = categoryName.toUpperCase() + " Shop Now ";
    			Link category = browser.describe(Link.class, new LinkDescription.Builder()
    				.tagName("DIV")
    				.innerText(innerText).build());
    			category.click();
    	}
    
    	@And("^I add the first product to the cart$")
    	public void i_add_the_first_product_to_the_cart() throws GeneralLeanFtException{
    		WebElement firstItem = browser.describe(Image.class, new ImageDescription.Builder()
    			.className("imgProduct")
    			.index(0).build());
    		firstItem.click();
    
    		Button button = browser.describe(Button.class, new ButtonDescription.Builder()
    			.buttonType("submit")
    			.tagName("BUTTON")
    			.name("ADD TO CART").build());
    		button.click();
    	}
    
    	@And("^I navigate to the cart$")
    	public void i_navigate_to_the_cart() throws GeneralLeanFtException{
    		WebElement cart = browser.describe(WebElement.class, new CSSDescription("svg#menuCart > path"));
    		cart.click();
    	}
    
    	@Then("^the total price is (\\$[0-9,.]+)$")
    	public void the_total_price_is(String price) throws GeneralLeanFtException{
    		WebElement totalPrice = browser.describe(WebElement.class, new WebElementDescription.Builder()
    			.className("roboto-medium ng-binding")
    			.innerText(new RegExpProperty("\\$.*")).build());
    
    		Verify.areEqual(price, totalPrice.getInnerText());
    }
  9. Run the test.

    • From your IDE. In the Project pane in your IDE, right-click LeanFtTest, and then click Run LeanFtTest.

      Or

    • From the command line. From the root folder of your project, run the following:

      mvn test

  10. View the test report.

    In your IDE menu, click LeanFTView last run results.

    The report includes the scenario keywords.

    For more details on analyzing run results, see Analyze run results

Back to top

Use the Cucumber framework in Visual Studio using SpecFlow

  1. Enable Cucumber to work in a LeanFT test:

    1. Install the SpecFlow extension on Visual Studio and make sure it is enabled.

    2. Create a LeanFT testing project that references TechTalk.SpecFlow.dll.

      1. Create a new LeanFT NUnit project. You can use the LeanFT NUnit or MSTest project template as a basis so that you will have all the required referenced files and other configurations, or you can create one from scratch.

        Note: The remainder of these instructions assume you are starting with the LeanFT NUnit project.  If you want to start with an empty Visual Studio project, follow the instructions described in: Use your own frameworks with LeanFT and then adjust the instructions below as needed.

      2. Add a reference to TechTalk.SpecFlow.dll.

        If you are using SpecFlow for the first time, right-click the LeanFT testing project, select Manage NuGet Packages, then search for and install SpecFlow. This will install the DLL and add a reference to it.

      3. Optional.  Delete the LeanFtTest.cs that was added when you created the LeanFT test.
  2. Add a feature to your project:

    1. Right-click your project and select Add > Component.
    2. Select SpecFlow Feature File, provide a file name and click Add.
    3. Replace the default content of the feature file with your feature content. For example:

      Feature: Calculator
      
      @Add
      Scenario: Add two numbers
             Given I have entered 50 into the calculator
             And I press add
             And I have entered 70 into the calculator
             When I press equals
             Then the result should be 120 on the screen
      
      @Multiply
      Scenario: Multiply two numbers
             Given I have entered 5 into the calculator
             And I press multiply
             And I have entered 7 into the calculator
             When I press equals
             Then the result should be 35 on the screen
      
      
  3. Implement the scenario using LeanFT:

    1. Generate a class file for your step definitions.  

      Right-click inside the feature file and select Generate Step Definitions. Then Click Generate, accept the default name, for example CalculatorSteps.cs, and click Save.

    2. Add the required using statements for LeanFT. For example:

                                      using System;
      using System.Diagnostics;
      using TechTalk.SpecFlow;
      using HP.LFT.SDK;
      using HP.LFT.SDK.StdWin;
      using NUnit.Framework;
    3. Add a class to the file.  For example: 

          public partial class CalculatorFeature : UnitTestClassBase
          {
          }
      • The name of your class must be the same as the name that was automatically generated by SpecFlow in the XXX.feature.cs file.

      • If you used a LeanFT NUnit or MSUnit template to create your testing project, UnitTestClassBase is already provided.

        UnitTestClassBase is a base class of the LeanFT test classes that are provided when you create a new LeanFT test project.

        This class provides all required functionality related to running LeanFT tests and creating the LeanFT HTML report.

        If you did not use a LeanFT template, make sure to follow the instructions in Use your own frameworks with LeanFT to initialize the LeanFT SDK and Report.

    4. Implement the test methods using the LeanFT SDK.  

      View the full implementation for the Calculator feature example shown above here: Implement SpecFlow Steps for a Calculator Feature File.

  4. Run the test and view the results.

    When you run the test in the example provided above, the LeanFT report would look something like this:

Back to top

See also: