DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Accessibility Testing for Digital Products
  • Compatibility Testing: Checklists and Crucial Things You Need to Know About It
  • Top 11 Selenium Testing Misconceptions You Should Know
  • What BDD Is and Why You Need It: Java and Behavior Driven Development

Trending

  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • Teradata Performance and Skew Prevention Tips
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Cucumber Selenium Tutorial – Perform Website Testing

Cucumber Selenium Tutorial – Perform Website Testing

By 
Neha Vaidya user avatar
Neha Vaidya
·
Dec. 09, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
12.1K Views

Join the DZone community and get the full member experience.

Join For Free

cucumbers-in-barrel

There's no doubt that Selenium is a fantastic tool in the field of automation testing, but it might seem a little hard for those not familiar with software development to understand how it works due to the complexity of the code. To ease this task, Cucumber comes into play. It makes complex code simple and easy to understand. But, how exactly does it do that? I will be telling you all about it through this article on Cucumber Selenium Tutorial.

Below are the topics we'll cover:

  • Introduction to Cucumber.
    • Why Cucumber?
    • Behavior Driven Development.
  • Introduction to Selenium.
  • Why Use Cucumber With Selenium?
  • Steps to Create a Cucumber Application.
You may also like: Getting Started With Selenium.

Introduction to Cucumber

Cucumber logo - Cucumber Selenium Tutorial - Edureka

Cucumber logo

Cucumber is a testing approach/tool that supports Behavior Driven Development. It provides a way to write tests that anybody can understand, regardless of their extent of technical knowledge.

It explains the behavior of the application in a simple English using Gherkin language. I hope that you got a glimpse of what is Cucumber. Now, let’s look at why we'd want to use Cucumber in a project.

Why Cucumber?

Well, Cucumber is one of the most popular tools because of the reasons listed below:

  1. Cucumber is open source and therefore free to use.
  2. With Cucumber, you can write test scripts in multiple languages, such as Java, Ruby, .NET, Python, etc.
  3. It also integrates with Selenium, Ruby on Rails, Watir, and other web-based testing tools.
  4. Cucumber is one of the most widely used BDD tools.

These are some of the unique features that make Cucumber helpful for testing a website. Now that you know what is Cucumber and why do you need it, let’s understand one of the unique features of Cucumber that make it easy for a non-techie to understand.

Behavior Driven Development (BDD)

In very simple terms, BDD is a technique where your specifications or test cases are written in plain English. With this approach, non-technical team members find it easy to understand the flow and collaborate more in the process of software development.

Let’s understand this with the help of a simple example. Consider a scenario where you want to test a Twitter webpage. One of the test scenarios is to verify login credentials. With BDD, you can write this test in this format:

Feature: Test Twitter smoke scenario

Scenario: Test login with valid credentials
Given Open the firefox and start application
When I enter valid username and valid password
Then user should be able to login successfully


In the above example, I have just written a few statements in simple English for defining the scenarios. I will be providing more insights into this scenario and its implementation in a later part of this article. For now, let’s move further in Cucumber Selenium Tutorial article and learn the fundamentals of Selenium.

Introduction to Selenium

Selenium -Selenium Maven with Eclipse

Selenium

Selenium is the preferred tool when it comes to automating tests carried out on web browsers. It is useful only for testing web applications. Any desktop (software) application or mobile application cannot be tested using Selenium. It is very helpful in writing functional test cases.

Now that you know what Selenium ia, let’s move further in this article on Cucumber Selenium Tutorial and understand why use Cucumber with Selenium?

Why Use Cucumber With Selenium?

Many organizations use Selenium for functional and regression testing. Selenium and Cucumber are a great combination when it comes to web application automation, as Cucumber allows you to write your tests quickly in an English-like language, and Selenium allows you to run on various combinations of browsers.

Why use cucumber with selenium - Cucumber Selenium Tutorial - Edureka

Why use Cucumber with Selenium

Cucumber is based on Behavior Driven Development framework that acts as the bridge between the Software Engineer and Business Analyst and also between Manual Testers and Developers.

When using Selenium with Cucumber for test automation, tests are written in feature files that can be understood by various stakeholders in an agile environment. Cucumber also comes with an ability to support multiple scripts and programming languages; JUnit is used to execute these scripts and generate the output.

Let’s now see the various steps to create a Cucumber application and run various test cases.

Steps to Create a Cucumber Application

The various steps involved in creating a Cucumber application are as follows:

  1. Download the Jar files of Cucumber and Selenium and configure the build path.
  2. Add Cucumber Plugin from Eclipse Marketplace.
  3. Create a feature file and add scenarios.
  4. Implement the steps for the scenarios.
  5. Write the runner class and execute the code.

Now, let’s understand each of these steps in detail.

Download Cucumber and Selenium Jar files

Cucumber doesn’t actually interact with the browser and performs actions on the website under test. Interacting with the website is the action performed by Selenium WebDriver. Due to this, you will need to download Cucumber as well as Selenium Webdriver's jar files. If you wish to know how to install Selenium on your system. Below is the list of jar files for Cucumber.

  • Cucumber-core.
  • Cucumber-html.
  • cobertura code coverage.
  • Cucumber-java.
  • Cucumber-junit.
  • Cucumber-jvm-deps.
  • Cucumber-reporting.
  • Hamcrest-core.
  • Gherkin.
  • Junit.

Once you download these jar files from the Maven Repository, you can configure the build path of your project and add all the .jar files and libraries, as shown in the below snapshot.

Configure Build Path - Cucumber Selenium tutorial - Edureka

Configure Build Path

Install Cucumber in Eclipse

The next step is to install Cucumber in Eclipse. For that, you need to go to Help -> Eclipse Marketplace -> Search Cucumber and install Cucumber and Naturals in Eclipse. The following snapshot displays the process;

Eclipse Marketplace - Cucumber Selenium Tutorial - Edureka

Eclipse Marketplace

Create a Feature File

Once you configure Cucumber, the next step is to create a feature file. First, you have to create a new source folder and then add a feature file as shown below

Creating feature file - Cucumber Selenium Tutorial - Edureka

Creating a feature file

Next, you need to specify the name of the feature file, as shown below:

Feature file - Cucumber Selenium Tutorial - Edureka

Feature file

Once you create the feature file, you can write the scenarios, as shown in the following code.

Test Scenario 1

Feature: Reset functionality on login page of Application

Scenario: Verification of Reset button
Given Open the Firefox and launch the application
When Enter the Username and Password
Then Reset the credential

Step 4: After that, you need to implement the scenarios with the help of step definitions. In the below code, I have implemented the methods for the scenario that I have written in the feature file.

package stepDefinitions;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class Steps {
@Given("^Open the Firefox and launch the application$")
public void open_the_Firefox_and_launch_the_application() throws Throwable
{
System.out.println("This Step open the Firefox and launch the application.");
}

@When("^Enter the Username and Password$")
public void enter_the_Username_and_Password() throws Throwable
{
System.out.println("This step enter the Username and Password on the login page.");
}

@Then("^Reset the credential$")
public void Reset_the_credential() throws Throwable
{
System.out.println("This step click on the Reset button.");
}
}


 Creating the Runner Class

Now, the last step is to create the Runner class and execute the program. Basically, your project structure should look like the snapshot shown below:

Project structure - Cucumber Selenium Tutorial - Edureka

Project structure

In the above snapshot, Runner.java is an executable file. Let’s now code this file and understand how it works.

package Runner;
import org.junit.runner.RunWith; 
import cucumber.api.CucumberOptions; 
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class) 
@CucumberOptions(features="features",glue={"stepD"}) 
public class Runner 
{
}


In the above code, I have @CucumberOptions and specify the package name of both the Feature and Runner file. When you run the program, it will display the methods from the step definition.

This Step open the Firefox and launch the application.
This step enter the Username and Password on the login page.
This step click on the Reset button.

1 Scenarios (32m1 passed)
3 Steps (32m3 passed)
0m0.185s


So, this is how you need to write all the files and execute the program.

Test Scenario 2

Now, let’s understand one more scenario. In this, I have integrated Selenium with Cucumber. Consider a scenario where you want to test a Twitter webpage. One of the test scenarios will verify the login credentials. With BDD, you can write this test scenario in this format:

Feature: Test Twitter smoke scenario

Scenario: Test login with valid credentials
Given Open the firefox and start application
When I enter valid username and valid password
Then user should be able to login successfully

Next, let’s write the Step definition class to implement all the scenarios.

package stepD;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class SmokeTest {
WebDriver driver;
@Given("^Open the firefox and start application$")
public void open_the_firefox_and_start_application() throws Throwable {
System.setProperty("webdriver.gecko.driver", "C:geckodriver-v0.23.0-win64geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("<a href="https://twitter.com/login">https://twitter.com/login</a>");
}

@When("^I enter valid username and valid password$")
public void I_enter_valid_username_and_valid_password() throws Throwable {
driver.findElement(By.xpath("//input[@placeholder='Phone, email or username']")).sendKeys("your_username");
driver.findElement(By.xpath("//div[@class='clearfix field']//input[@placeholder='Password']")).sendKeys("your_password");
}
@Then("^user should be able to login successfully$")
public void user_should_be_able_to_login_successfully() throws Throwable {
driver.findElement(By.xpath("//button[@class='submit EdgeButton EdgeButton--primary EdgeButtom--medium']")).click();
}
}


In the above program, I have used Gecko Driver to launch Firefox. Using driver.get(),  it will navigate through the twitter login page. Further, it will locate the username and password text box using the XPath locator and enter the valid credentials. Finally, it will log in successfully and run the test cases. You have to execute the runner as a JUnit test to know whether the tests passed or failed.

The following snapshot shows the output of the program.

Output - Cucumber Selenium Tutorial - Edureka

Output - Cucumber Selenium Tutorial

This is how you need to write all the files and execute the program. So, that was all about Cucumber Selenium Tutorial. I hope you understood the concepts and helped in adding value to your knowledge.


Further Reading

  • Performance Test With Selenium.
  • Selenium 2.0: Using the Webdriver API to Create Robust User Acceptance Tests.
Cucumber (software) Testing mobile app Software development Behavior-driven development

Published at DZone with permission of Neha Vaidya, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Accessibility Testing for Digital Products
  • Compatibility Testing: Checklists and Crucial Things You Need to Know About It
  • Top 11 Selenium Testing Misconceptions You Should Know
  • What BDD Is and Why You Need It: Java and Behavior Driven Development

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!