Testing with Cucumber

OpenText Functional Testing for Developers supports Behavior Driven Development (BDD) using the Cucumber testing framework. This framework 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 consult the Cucumber documentation.

Overview

You can create a test project based on Cucumber.

OpenText Functional Testing for Developers 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.

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.

All the OpenText Functional Testing for Developers tools are at your disposal when working with the OpenText Functional Testing for Developers Cucumber framework, just like any other testing project.

Note:  

  • To use OpenText Functional Testing for Developers Cucumber-based projects that were created using LeanFT templates from versions earlier than 14.52, follow the instructions in My old Cucumber project does not work.

  • Before running Cucumber tests, make sure your installed Java version is compatible with the Cucumber version used to create the tests.

Back to top

Use the Cucumber framework in Eclipse

Note: This section is also relevant for Spring Tool Suite, JBoss Developer Studio, and IntelliJ IDEA.

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

  • LeanFtFeature.feature file. Includes the test scenario in the Gherkin language.
  • LeanFtStepDefinition.java class. Includes the implementation of the test scenario described in the feature file.
  • pom.xml. Includes OpenText Functional Testing for Developers and Cucumber dependencies for Maven.

To create a Cucumber test 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 UFT Developer Cucumber Project.
    3. Select the Cucumber version you want to use.
    4. 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:

    1. Update the OpenText Functional Testing for Developers dependencies.
    2. Add a testSourceDirectory child element to the <build> element:

      <build><testSourceDirectory>src/main/java</testSourceDirectory></build>

      This is required for running tests using Maven.

      Note: If your runner class is not located in the default location, make sure to set the testSourceDirectory in the pom.xml file to the exact location of the runner class.

  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 the script as follows: 

    • Include the Scenario Outline keyword instead of the Scenario keyword.

    • Add an Examples data table with the parameters and values.

    • Incorporate the parameters into the scripts instead of hard-coded values.

    The test runs 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 UFTDeveloperTest, and then click Run UFTDeveloperTest.

    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 OpenText Functional Testing for Developers 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 UFTDeveloperTest before implementing the methods, you get a pending exception.

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

    • Import the required OpenText Functional Testing for Developers classes and use the OpenText Functional Testing for Developers SDK to create the test. For details, see Add steps to your tests.
    • Use OpenText Functional Testing for Developers tools, such as the Object Identification Center or the Application models.
    • Change the regular expressions suggested by Cucumber as required.

    Note: You do not need to edit the UFTDeveloperTest.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 UFTDeveloperTest, and then click Run UFTDeveloperTest.

      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, click UFT Developer menu > View 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

In Visual Studio, you can create a Cucumber project using SpecFlow. The instructions below describe how to perform the following steps: 

Enable Cucumber to work in a test

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

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

    1. Create a new OpenText Functional Testing for Developers NUnit project. You can use the UFT Developer NUnit or MSTest project template as a basis so that you 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 OpenText Functional Testing for Developers NUnit project.  If you want to start with an empty Visual Studio project, follow the instructions described in: Use your own frameworks 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 testing project, select Manage NuGet Packages, then search for and install SpecFlow. This installs the DLL and adds a reference to it.

    3. Optional.  Delete the UFTDeveloperTest.cs that was added when you created the test.

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
    
    

Implement the scenario

  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 OpenText Functional Testing for Developers. 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 UFT Developer NUnit or MSUnit template to create your testing project, UnitTestClassBase is already provided.

      UnitTestClassBase is a base class of the test classes that are provided when you create a new OpenText Functional Testing for Developers test project.

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

      If you did not use a UFT Developer template, make sure to follow the instructions in Use your own frameworks to initialize the OpenText Functional Testing for Developers SDK and Report.

  4. Implement the test methods using the OpenText Functional Testing for Developers SDK.  

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

Run the test and view the results

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

Back to top

See also: