This article is contributed. See the original author and article here.

 


By Anusha Ammaluru


 


This time we bring you a blog article about Cucumber, Selenium and Integration with Azure DevOps, let’s get started and welcome to the journey to learn Cucumber.


The blog post will cover the following topics:



  • Cucumber Introduction

  • Setup Cucumber with Selenium in Eclipse

  • Cucumber Basics

  • Eclipse Integration with Azure DevOps


 


 


 


Cucumber Introduction


Cucumber is a tool that supports Behaviour-Driven Development(BDD). It lets us define application behavior in plain meaningful English text using a simple grammar defined by a language called Gherkin. Cucumber itself is written in Ruby, but it can be used to “test” code written in Ruby or other languages.


 


Cucumber is one of the most powerful tools. It offers us the real communication layer on top of a robust testing framework. The tool can help run automation tests on wide-ranging testing needs from the backend to the frontend. Moreover, Cucumber creates deep connections among members of the testing team, which we hardly found in other testing frameworks.


 


What is Gherkin? It is a simple, lightweight, and structured language, which uses regular spoken language to describe user requirements and scenarios. Examples of regular spoken language are English, French, and around 30 more languages. Gherkin contains a set of syntax/keywords


Feature: Defines the feature (generally a user story)


Given: Specifies the pre-condition of the test


And: Defines additional conditions of the test


Then: States the post-condition/expected result of the test


 


Key points to note:



  • The test is written in plain English, which is common to all the domains of your project team.

  • This test is structured that makes it capable of being read in an automated way. Thereby creating automation tests at the same time while describing the scenario.


 


 


 


Setup Cucumber with Selenium in Eclipse



  1. Setup java

    • Download the latest version of Java from https://www.oracle.com/technetwork/java/javase/downloads/index.html 

      • 1.png


      • Download .exe file



      • 2.png



    • Install Java by double-clicking on the .exe file and proceed with default options

    • Set up Java Environment Path

      • Type Env in the windows search and click on the ‘Environment Variables’ option

        • 3.png



      • Click on Path in the System variables and click the ‘Edit’ button

        • 4.png



      • Add folder path where the JDK is installed and click the ‘OK’ button

        • 5.png



      • Go to Command Prompt and type java -version If you see a screen like below, Java is installed

        • 6.png







  2. Setup Eclipse

    • Download Eclipse Photon from https://www.eclipse.org/downloads/packages/release/photon/rc3, if you are going to maintain the repo in Azure DevOps, I had faced issues earlier with other bundles.

      • 7.png



    • Select Eclipse IDE for Java Developers and Install

      • 8.png



    • Launch the eclipse and select a workspace folder to save your repo





  1. Install Cucumber Plugin for Eclipse

    • Launch the Eclipse IDE and from the Help menu, click “Install New Software”.

    • You will see a dialog window, click the “Add” button.

    • Enter the name “Cucumber” and location as “http://cucumber.github.com/cucumber-eclipse/update-site”. Click the ‘OK’ button.

      • 9.png



    • Check the ‘Cucumber Eclipse Plugin’ and click next to install

    • You may or may not encounter a Security warning, but if you do just click the ‘OK’ button.

    • Restart Eclipse for the changes to take effect





  1.   Download Cucumber JARS from Maven Repo

    • Go to https://search.maven.org.

    • Search for cucumber-core in the Central Maven Repository.

    • Download jar file

    • Similarly, search for all the below libs in the Maven repo and download JAR’s

      • 10.png







  1. Download Selenium

    • Download WebDriver Java client from Selenium website

      • 11.jpg



    • Extract the files and save it in your local folder





  1. Configure Eclipse with Cucumber and Selenium WebDriver libs

    • Go to Eclipse : File-> New -> Project -> Select Maven

      • 12.png



    • Click the ‘Next’ button

      • 13.png



    • Filter Cucumber and select cucumber-archetype

      • 14.png



    • Enter Group Id and Artifact Id and click the ‘Finish’ button

      • 15.png



    • Add Selenium Jars

      • Right-click on Project ‘CucumberTest > Select Properties > Java build path. Then navigate to the Libraries tab and click Add External JARs.

      • Browse to the local folder where the selenium jars are saved and select all the jars and add

        • 16.png



      • Go to the lib folder and add all the remaining Jars

        • 17.png



      • Click ok



    • Add Cucumber Jars

      • Right-click on Project ‘CucumberTest > Select Properties > Java build path. Then navigate to the Libraries tab and click Add External JARs.

      • Browse to the local folder where the cucumber jars are saved and select all the jars and add

        • 18.png



      • Click the ‘Ok’ button

      • Now we are all set :smiling_face_with_smiling_eyes:






 


 


 


Cucumber Basics



  • Cucumber Feature File

    • An entry point to the Cucumber tests. It contains a list of scenarios/test cases written in natural English language using Gherkin keywords

    • Create a package in eclipse project -> name it as ‘Features’

      • 19.jpg



    • Right-click on the ‘Features’ package and create a file -> name it as ‘DemoTest.feature’

      • 20.png



    • Add a Feature: Name and description and a scenario underneath like the below example

      • 25.png



    • Step Definitions

      • Add selenium java code in the step definition methods corresponding to feature files.

      • Sample code:






@Given(“user is on home page“)


 public void user_is_on_home_page() {       


 driver.get(“https://pul-ai-anu.azurewebsites.net/“);


 driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);


 }


 


 @When(“I click on login link“)


 public void i_click_on_login_link() {


 login = new LoginPage(driver);


 login.lnk_Login.click();


 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


 }


 @When(“I enter username {string}“)


 public void i_enter_username(String email) {


 login = new LoginPage(driver);


 login.txtbx_UserName.sendKeys(email);


 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


 }


 @@When(“I enter password {string}


 public void i_enter_password(String password) {


 login = new LoginPage(driver);


 login.txtbx_Password.sendKeys(password);


 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);


 }


 @When(“I click on login button“)


 public void i_click_on_login_button() {


 login = new LoginPage(driver);


 login.btn_Login.click();


 driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);


 }


 @Then(“I verify the login is successful“)


 public void i_verify_the_login_is_successful() {


 login = new LoginPage(driver);


 assert(login.lnk_profile.isEnabled());


 }


Junit Test Runner Class


package runner;


 


import io.cucumber.junit.Cucumber;


import io.cucumber.junit.CucumberOptions;


import org.junit.runner.RunWith;


 


@RunWith(Cucumber.class)


@CucumberOptions(plugin = {“pretty“},


features= “srctestresourcesfeatures” ,


glue= “stepDefinitions“)


public class RunCucumberTest {


 


}


Right-click on TestRunner class and Click Run As  > JUnit Test Application


Test Run Report will be shown like this


21.png


 


 


Eclipse Integration with Azure DevOps



22.jpg


 


 

Conclusion


I have created a sample Java Maven project using the Selenium Page factory and Cucumber in GitHub https://github.com/anu-01/CucumberSelenliumPageFactory. This is a great place to get started with a Cucumber-based framework.

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.