Automation Testing With Selenium, Cucumber & TestNG

Bharadwaj Pendyala

Posted On: February 18, 2019

view count115167 Views

Read time13 Min Read

This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Cucumber Tutorial.

Cucumber is a test automation framework which leverages Behavior Driven Development for collaboration in between Business and IT teams. It empowers a user to define an application’s behavior in plain English language which makes it easier for non-programmers to understand the acceptance criteria.
The core of cucumber has been developed in Ruby programming language however it supports all the majorly used programming languages for testing such as Java, C#, Python. In this article, I will be demonstrating automation testing with Selenium, Cucumber and TestNG.

If you’re new to Selenium and wondering what it is then we recommend checking out our guide – What is Selenium?

What Is Behavior Driven Development?

Behavior Driven Development is derived from Test Driven Development as a software development methodology. As a part of the Test Driven Development approach, the developer creates tests as a part of the acceptance criteria first. The developer will make sure the test is passed and will commit changes in the code. if required. Test Driven Development is a way to ensure the system meets its requirements by ensuring a 100% test coverage. It provides an edge over other methodologies in terms of finding defects early in the cycle which reduces the cost of finding bugs and refactoring improves the code.

Cons of Test Driven Development are the maintenance of tests created, it is a test-centric methodology, non-programmers might find it hard to understand for performing selenium automation testing. The test or acceptance criteria being targeted makes it hard for team collaboration. To overcome the above trouble heads, Behavior Driven Development was introduced to reduce the time to test, less code with more collaboration.

Advantages Of Using BDD For Automation Testing With Selenium & Cucumber

Collaborative Approach: Bridges the communication gap between stakeholder or business teams who can’t easily understand or read the code. This problem is addressed by defining the behavior of application under test in a ubiquitous language i.e. easily understandable.

Focuses more in terms of end-user experience or inclined more towards behavior specifications while compared to that of traditional Test Driven methodology. Tests are written more from end user’s point of view, making them easier to understand for performing Selenium automation testing.

With TestNG certification, you can challenge your skills in performing automated testing with TestNG and take your career to the next level.

Here’s a short glimpse of the TestNG certification from LambdaTest:

How Cucumber Leverages BDD For Selenium Automation Testing?

As discussed earlier, Cucumber is a test automation tool that supports Behavior Driven Development. It makes use of user-defined specifications to validate the application under test. Before we step into performing automation testing with Selenium & Cucumber, let me tell you about the three major parts of the cucumber framework i.e. Feature file, Step Definitions, and Test Runner file.

Feature File would enable the user to define the behavior of the application in plain English text with help of Gherkin Language. Gherkin is used to define executable specifications with the help of a predefined set of contextual keywords. Keywords involved are demonstrated with the help of the below example:

Feature: High-level description of a software feature or behavior of application under test.

Scenario: Feature file can contain more than one scenario which acts as a test case derived from test scenarios/behaviors. It also symbolizes and acts as one or more user perspectives involved.

Annotations: Keywords which hold a specific meaning and would help and defined the meaning of a scenario. Sample keywords involved are GIVEN, WHEN, THEN & AND.

  1. GIVEN: Define pre-requisites of the application.
  2. WHEN: Trigger point for a scenario execution can be related more towards steps to be executed.
  3. THEN: Defined an outcome more like the expected result.
  4. AND: Acts as a logical bridge AND in between two statements.

Scenario Outline: It’s a template provided to carry out scenario execution, test data table provided in the Examples section would replace variables/arguments created making each individual row in a table as a unique scenario.

Tags: It paves the way to organize your tests based on your tag definition.

Quoted an example related to the feature file below which involves all the above-mentioned keywords.

Example:

Username

Password

ProductId

Username1

Password1

ProductId1

Username2

Password2

ProductId2

Step Definition file created would be consisting of a code which defines the steps for annotations created in feature file. An annotation followed by step defined in patterns with the help of regular expression would help in linking the step definition file to steps in feature file. Sample step definition code sample for above code is defined as above.

Test Runner file defined the location of step definition and primarily provides all the metadata information required for test execution. It makes use of ‘@RunWith()’ annotation from JUnit framework for execution. Secondarily you would be making use of ‘@CucumberOptions’ annotation to define the location of feature files, step definitions location through glue, what reporting integrations to use etc.

Below is the sample of feature file created.

We can integrate parallel test execution for automation testing with Selenium & Cucumber framework for saving a considerable amount of time during test execution. To elaborate the above scenario, serve the regression test needs of an application under test you have to execute your entire regression test automation repository which could serve you with both time and resource constraints. The end-user would not be using the same browser with its latest version, there could be plenty of browsers, operating systems and browser version combinations which should be considered while performing out testing.

However, the cons stated above could be resolve with cloud solutions such as LambdaTest which is a cross browser testing cloud. LambdaTest offers an online Selenium grid consisting 3000+ real browsers and operating systems help you achieve maximum amount of browser test coverage. Build time could be drastically reduced by utilizing LambdaTest automation for your test execution. Parallel test execution with the help of various browsers and multiple versions of each browser instance would help you achieve your cross browser parallel testing needs. Automated screenshots and video recording would help you keep track of your test failures and find the root cause quickly providing you a better analytical perspective while troubleshooting.

As we have embarked on our journey of implementing automation testing with Selenium & Cucumber framework using Java, till now we have learned the overview of the cucumber framework and how it leverages BDD. Let’s get into hands-on the mode by creating a sample framework using Cucumber for Selenium automation.

automation-trial-now

Creating Your First Project Using Cucumber For Selenium Automation Testing

We make use of Eclipse IDE for coding purposes, get the latest version from their official website https://www.eclipse.org/downloads/.

Archetype would be generated with the help of above statement. If in case above archetype has been missing please add template for the Archetype by entering below info.

After entering above details try navigating further and finishing required steps. Hence maven project would be successfully created.

Replace the dependencies by obtaining them from below pom file as required with below set of dependencies.


Project Using Cucumber

Creating your Feature file with user defined specifications in Gherkin Language:
Step 1: Create a folder for organizing all your feature files as shown in the image below.
Step 2: Select File from the navigation menu. File → New → File.
Step 3: Name the file using “Login.feature”.
Step 4: As there is no plugin which supports “.feature” extension file created would look as in the image below.
feature button
Step 5: Once you create the feature file below alert would be prompted asking you to select the editor to be installed from the marketplace.
marketplace editor
Step 6: Select “Show IDE extensions for this file type and let me install them” radio button and select the first option which is Cucumber Eclipse Plugin as shown in the image below.
Click on install. Accept the terms of the license agreement. Click on Finish. You would be prompted with a security alert, click on Install Anyway option, once installation is finished click on Restart Now to view the changes made.
eclipse marketplace
Step 7: To view if changes are reflected observe the Feature file as in below image how different is it from the previous image of the feature file created.
feature menu

Watch this video to learn how TestNG has become one of the most robust test automation frameworks and all you need to know to get started with TestNG in Selenium.

Running First Script Of Automated Testing With Selenium & Cucumber Framework

Let’s consider a real-world case study of querying a search term in google search by creating a feature file which consists of feature, tags and scenarios and scenario outline as applicable.

For automation testing with Selenium & Cucumber framework for Java, we need to create a GoogleSearch.feature file under Features package with below content under “src/test/java” path.

Example:

Query

Lambdatest

Selenium

Cucumber

Once you have completed creating a feature file, created steps would be highlighted with warnings indicating that you haven’t defined the step contained in step definition file yet.
In order to resolve the errors, lets navigate to the step definition file to complete coding the gist which defines our steps.
Below code can be replaced in step definition file which is Stepdefs.
TIP:

While creating your step definitions if you want a template for step definitions file creation just run the feature file along with warnings as discussed previously. It would provide you with the template upon which you can start coding as shown in the image below.
 Automated Testing With Selenium &  Cucumber Framework

Make sure your step definitions are specified in package “com.googlesearch.testing.steps” under the path of “src/main/java”.
Automated Testing With Selenium &  Cucumber Framework

Copy the code and define the path to your chrome driver. Below is the code used for automation testing with Selenium & Cucumber in Java.

Make sure your Test Runner java file which is named as RunCucumberTest is under “src/test/java” path with code specified below.

Now you have all the 3 main aspects ready for test execution. To proceed further for running automation testing with Selenium and Cucumber you would need to right-click on RunCucumberTest.java file which is created. Click on Run as JUnit Test. Tests get triggered for both the scenarios specified. If you observe the scenario outline it gets executed thrice with all the sets of data specified in a data driven format.

Interested about JUnit? Don’t forget to read our article on Automated Testing With JUnit And Selenium For Browser Compatibility.

Using LamdaTest For Automation Testing With Selenium & Cucumber

Above test would be running in sequence and nexttest would have to wait till the thread is empty by completion of previous test triggered. This would consume more amount of time and cross browser testing with above methodology would prove to be a troublesome and expensive due to the permutations and combinations of the browsers and operating systems and versions available. In order to overcome the issue we would have to run our automation tests with Selenium and Cucumber in parallel, even if we do so still we would have to configure a lot of machines as the number of tests you can trigger per system would depend on the computing capabilities of each system.

LambdaTest would help us overcome the above problem statements. As a cloud solution it offers scalability to reduces build time, improve browser test coverage, provision to validate responsive test from pixel to pixel accuracy, enhanced reporting and analytical capabilities. Let’s start with configuring Cucumber to enable parallel test execution of Selenium with Java using LambdaTest for automated cross browser testing

TestNG CTA

Watch this video to learn how to set up and use TestNG with Selenium to automate your testing process. We will also introduce the TestNG Priority method, which allows you to write easy-to-read and maintainable tests.

Configuring Your Test Suite With LambdaTest For Parallel Test Execution Of Selenium With Cucumber & TestNG

Replace your step definition file with below code which includes Remote WebDriver configuration to connect to cloud virtual machines. Currently we have JUnit as our unit testing framework, for provisioning parallel test execution let’s integrate current framework with TestNG. TestNG provides an easier configuration and additional capabilities which makes it more powerful than that of JUnit.

We start with creating a TestNG xml file to run the test suite. Add below xml code to your project root folder. If you are not familiar with TestNG then you can have a look at our guide for running first TestNG automation script.

Replace your step definition file with below code. @Parameters will help us to parameterize the desired capabilities such as browsername, version, platform etc. Have a look at LambdaTest Capabilities Generator for fetching the coded values according to your desired capabilities. If you take a glance at above xml file we have two parameters which are browser and version. Thread count in the xml code represents the number of concurrent session which are possible. We have set it to 2 for demo purposes. Kindly replace the stepDefinition code with below TestNG model.

As we have trigger the tests from xml file of TestNG we can replace the TestRunner file with below code, as our entry point would be TestNGRunner xml.

After executing the above code for performing automation testing with Selenium, Cucumber, & TestNG. It is important to observe the details of your Selenium automation tests. LambdaTest provides every inch of information, from analytics to video logs for you to observe the output of your automation testing with Selenium.

lambdatest_community

Automation Dashboard:

Navigate to automation dashboard of lambdatest engine to view number of concurrent sessions being active and browser info as highlighted in the above screenshot.
Concurrent Sessions Usage


It provides the logs, metadata information, screenshots and video recording for the tests executed. Click on test to view the details specified above as in below snapshot.
Automation Dashboard


Analytics View:

Analytical view of tests execution information could be viewed by click on analytics tab. This would provide a comprehensive view of builds passed, failed, day of execution, time taken, bugs logged from the testing etc. You can take a glance at it in the below image.Analytics


Timeline View:

Timeline would provide the information of test suite executed as a whole. Time and date of execution browser information, failures and error information if any. You can view tests executed in the form of build or test view.
pasted image 0

To summarize we have explained what is cucumber, how does it help overcome challenges of traditional test methodologies. We have ran our first test script automation testing with Selenium and Cucumber framework after understanding what cross browser testing is all about. Also, how cross browser testing could be simplified using parallel cloud testing solution like LambdaTest. We have created a cucumber framework and configured it with LambdaTest integrating TestNG for parallel testing. Happy Testing! 🙂

Author Profile Author Profile Author Profile

Author’s Profile

Bharadwaj Pendyala

Bharadwaj has more than 5+ year of experience in handling Automation Testing who is also an expert in handling Manual Testing, API Automation, Performance Testing and Certified Agile Practitioner who is currently handling projects in TDD and BDD methodologies with both custom built and hybrid automation frameworks

Blogs: 4



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free