Tutorial on Selenium, Cucumber and Gherkin

tutorial-cucumber-selenium

Selenium is a tool for web application testing. You can write a script in java but have you wondered whether that script will be understood by a business analyst or not. You can say a business analyst and technical persons are not on the same page. For bridging the gap between them, we need a behavior driven framework. If we follow the process which lacks the BDD framework there are maximum chances that the product will not be built the way the client needs. Cucumber is a behavior driven framework which helps in building the product the way the client wants. It tests the application based on the behavior of the application. The tests are written in so simple language which is similar to English. That language is known as Gherkin language.

Cucumber Application in Automated Testing

Prerequisites for using Cucumber

For downloading and installing cucumber, you have to get some pre-requisites in your machine. If you will not have that you won’t be able to run tests through cucumber.

  • Selenium Jar Files

  • Cucumber Jar Files: You can download from here

    • cucumber-core

    • cucumber-java

    • cucumber-junit

    • cucumber-jvm-deps

    • cucumber-reporting

    • gherkin

    • junit

    • mockito-all

    You have to go to the maven repository and search for these jars there. For every jar, you would get two options. For one artefact id would be info.cubes and for other, it would be io.cucumber. You have to download the jar file which will be having artefact id as io.cucumber. In this way, you have to get all the dependencies from there and add in the pom.xml file.

  • Is to download cucumber eclipse plug-in. You have to go eclipse marketplace and then search for cucumber plug-in. You can then install the plug-in from there.

How to Build the First Project with Cucumber and Selenium?

Now, you have all the pre-requisites for building a project with cucumber and selenium. Now, you have to start building a project. Let’s have a look at those.

  • Create a project in Eclipse. Let’s look at how to create the project

  • While creating a project choose New -> Project -> Other. You have to then select Maven in other projects.

  • Now, whatever jar dependencies you have got from maven repository site. You can add them to pom.xml.

  • Now, you can save the project and all the dependencies will be downloaded and saved. It will download all the jar files according to the version number you have specified in the pom.xml.

  • Now, you can start writing your first cucumber feature file.

  • You can now create a feature file in your project by right-clicking on your project. Create a folder name Features. Now you have to follow the steps: Create -> New -> File. You can save the file by name FirstTest.feature.

  • Your first feature file is created and you have to start writing scenarios in it using gherin syntax. You have to then follow “Given, When Then and And” syntax.

Let’s Look At the Feature File

Feature:

  • Login scenario for the test application

  • This login scenario will test the login page functionality of the test application.

  • Scenario: Login to the Demo application.

  • Given I am on the login page of the demo application

  • When User enters username and password

  • Then User should log in to the home page successfully.

The above Scenario explains the login functionality. The first step is a given statement which tells that the user should be at the home page. When condition signifies an operation user performs. The user enters a username and password. Third line statement will tell the user about the action which is performed after the second-line statement operation has been completed.

Now, you have to create a test runner file with which you will be running this feature file. You have to first create a TestRunner Package and then you have to create a testRunner.java class in it.

package RunnerTest ; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) @CucumberOptions(features="Features",glue={"StepDefinition"}) public class testRunner { }

So, you have two annotations in this test runner class. First is @RunWith annotation which will execute the test cases. Another annotation present is @CucumberOptions which is used to set the properties for a cucumber test. It defined the format of the report you want.

Now, you are already with the feature and runner file. The main thing is the execution of tests but for that, you have to define the tests in the Java language corresponding to the statements you have written in your feature file. You will write the definition of these steps in the step definition file.

Create a package named “StepDefinition” in the project folder structure. There, you have to make a file with name “TestCucumber.java”. Now, here you have to actually define the test steps mentioned in the feature file.

package StepDefinition; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class TestCucumber { WebDriver driver; @Given("^I am on the login page of the demo application$") public void open_the_Browser_and_launch_the_application() throws Throwable { System.setProperty(“webdriver.chrome.driver”,”Path of Chrome.exe file”); driver = new ChromeDriver(); driver.manage().window().maximize(); Driver.get(“URL”); System.out.println("Login page of the application is opened."); } @When("^User enter username and password $") public void enter_the_Username_and_Password() throws Throwable { driver.findElement(By.name("uid")).sendKeys("username"); driver.findElement(By.name("password")).sendKeys("password"); System.out.println("Username and password entered in the application”); } @Then("^User should login to the home page successfully$") public void successful_login() throws Throwable { driver.getTitle().contains(“Title”); System.out.println("Matching the title of the page after successful login."); } @After Public void Close_the_browser() throws Throwwable { driver.close(); System.out.println("Closes the browser opened through selenium"); } }

5 Main Keywords of Cucumber:

  • Given: It is used when you want to define a pre-condition.

  • When: It is used when you want to define step after a pre-condition.

  • Then: It is used as a response for when statement.  

  • And: If you want to conjunct two steps, you can use And keyword.

  • But: If you want to add a negative assertion, you can use But keyword

Whatever tags you will be using in feature file, you have replicated the same in step definition file so that cucumber can map the steps in the feature file to the steps in the step definition file.

You can run the above test by going to the TestRunner file. You can right click on it. You can then select “Run With” JUnit Test. When you will be running these tests, your tests would be executed. You will be able to see all the things on a console.

In the above scenario, you have done hard coding of data, but if you want parameterization or data-driven testing, then you have to pass data from the feature file.

Scenario:

  • Successful Login to Test Application using Scenario Outline

  • Given User is at the login page

  • When User enters Username as and Password as

    |username|password|

    |user1|pwd1|

    |user2|pwd2|

    |user3|pwd3|

    And User clicks on login button

  • Then User is able to successfully login to the Test Application.

@When("^User enters Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$") public void I_enter_Username_as_and_Password_as(String arg1, String arg2) { driver.findElement(By.id("email")).sendKeys(user1); driver.findElement(By.id("password")).sendKeys(pwd1); driver.findElement(By.id("login")).click(); }

You can then change the above step in the step definition file and you are doing with data-driven testing.

Conclusion:

Now, you have learned the way we will be using cucumber with selenium. It will bridge the gap as a business analyst can easily read the feature files and can easily point the specifications missed by you. Use cucumber and bridge the gap between different stakeholders while Automation Testing Services. All the best!!

Some of our clients

team