UFT Developer with Cucumber

UFT Developer 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 visit the Cucumber website.

Overview

You can create a UFT Developer project based on Cucumber.

UFT Developer 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, JBoss Developer Studio, 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 UFT Developer tools are at your disposal when working with the UFT Developer Cucumber framework, just like any other UFT Developer project.

Note: To use UFT Developer 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.

Back to top

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

When you create a UFT Developer 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 UFT Developer and Cucumber dependencies for Maven.

To create a UFT Developer 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 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 UFT Developer 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 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 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 UFT Developer 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.

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

Perform the following steps:

Enable Cucumber to work in a UFT Developer test

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

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

    1. Create a new UFT Developer NUnit project. You can use the UFT Developer 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 UFT Developer NUnit project.  If you want to start with an empty Visual Studio project, follow the instructions described in: Use your own frameworks with UFT Developer 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 UFT Developer 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 UFTDeveloperTest.cs that was added when you created the UFT Developer 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 using UFT Developer

  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 UFT Developer. 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 UFT Developer test classes that are provided when you create a new UFT Developer test project.

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

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

  4. Implement the test methods using the UFT Developer 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 UFT Developer report would look something like this:

Back to top

See also: