LeanFT with Cucumber

You can use LeanFT with the Cucumber test framework within your IDE to create behavior-driven development (BDD) tests.

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

Use the Cucumber framework in Eclipse/IntelliJ IDEA

  1. Enable Cucumber to work with Eclipse/IntelliJ IDEA:

    1. Eclipse only: Install a Cucumber editor plugin for Eclipse.  For example, the xtext plugin.

      Tip: IntelliJ IDEA has built-in Cucumber support. For more information, see Enabling Cucumber Support in Project in the IntelliJ IDEA Help.

    2. Download the following cucumber .jar files (the version numbers below are just an example).  These can be found on sites such as Maven:

      • cucumber-core-1.2.2

      • cucumber-html-0.2.3

      • cucumber-java-1.2.2

      • cucumber-junit-1.2.2

      • cucumber-jvm-deps-1.0.3

      • gherkin-2.12.2

    3. Create a new test project.

      You can use the LeanFT JUnit or TestNG project templates 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 JUnit project.

      If you want to start with a TestNG project, adjust the instructions below as needed.

      If you want to start with a, an empty Java project, follow the instructions described in: Use your own frameworks with LeanFT and adjust the instructions below as needed.

    4. Add the cucumber jar files mentioned above to your test build path.
  2. Enable LeanFT to work with Cucumber in your test:

    1. In the LeanFTTest class, add the following annotation on the class:

      @RunWith(Cucumber.class)

    2. Delete the test method that was generated by the LeanFT JUnit template.

    3. Add a new .feature file inside the same package.
      For the example shown below, it could be called Calc.feature.

    4. Add scenarios to your feature file.  For example:

      Feature: Calculator
      test basic calculator math.
      
      Scenario: Add 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
      	
      Scenario: Add more numbers
      
      	Given I have entered 5 into the calculator
      	And I press add
      	And I have entered 70 into the calculator
      	When I press equals	
      	Then the result should be 75 on the screen 
      
    5. Create a new class to implement the steps above.  For example, CalcStepDefinitions.

      Inside this class you must implement:

      • @Before section in which the LeanFT Reporter object is configured to start a new node in the report.
      • @After section in which the LeanFT Reporter object is configured to end the node in the report.

      For example: 

          @Before    
      
          public void beforeCalcScenario(Scenario scenario) throws IOException, GeneralLeanFtException, ReportException {
              //the following will start a new test node in the report        
              Reporter.startTest(scenario.getName());
                   
              calcProcess = new ProcessBuilder("C:\\windows\\System32\\calc.exe").start();
              WindowDescription calculatorDescription = new WindowDescription.Builder().nativeClass("CalcFrame").build();

              calculator = Desktop.describe(Window.class, calculatorDescription);

          }

          @After
          public void afterCalcScenario() throws InterruptedException, ReportException {        
              calcProcess.destroy();
              
              calculator = null;

      //the following ends the test node in the report
              Reporter.endTest();
          }

      When you run your test, the LeanFT report would look something like this:

Back to top

See also: