Hello Testers, No doubt that Selenium is one of the fantastic tools in the field of Automation Testing, but it might seem a little hard for a non-technical person to understand its work due to the complexity of the code. To ease this task, Cucumber comes into play, which makes the 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 Integrating Cucumber with Selenium 4.
Overview about Selenium
- Selenium is the preferred tool when it comes to automating the tests which are carried out on web browsers. It is valid only for the testing of web applications. Any desktop (software) or mobile application cannot be tested using Selenium.
- It is conducive to writing functional test cases. It also provides reliable performance with an ‘n’ number of test cases and it is obviously the best suitable automation tool for web applications.
- Most organizations use Selenium for functional testing. These organizations which are using Selenium, want to integrate Selenium with Cucumber as Cucumber makes it easy to read and understand the application flow. Now that you know what is Selenium, let’s move further in this article on Cucumber and understand why use Cucumber with Selenium?
Introduction to Cucumber
- Cucumber is a testing approach/tool that supports Behaviour Driven Development (BDD). It provides a way to write tests that anybody can understand, regardless of their extent of technical knowledge.
- Behavior Driven Development gives us an opportunity to create test scripts from both the developer’s and the customer’s perspectives as well.BDD acts as the bridge between the following people:
- Software Engineer and Business Analyst.
- Manual Tester and Automation Tester.
- Manual Tester and Developers.
- Cucumber BDD framework also benefits the client to understand the application code as it uses Gherkin language which is in Plain Text. It is one such open-source tool, which supports behavior-driven development.
- To be more precise, it defines as a testing framework, driven by the plain English text. It serves as documentation, automated tests, and development aid – all in one. Anyone in the organization can understand the behavior of the software. It supports over a dozen different software platforms like −
- Ruby on Rails
- Selenium
- PicoContainer
- Spring Framework
- Watir
Why Cucumber?
Well, Cucumber is one of the most popular tools because of the reasons listed below:
- It is open source and hence, it’s free to use.
- By using Cucumber, you can write your test scripts in multiple languages such as Java, Ruby, .NET, Python, etc.
- It also integrates with Selenium, Ruby on Rails, Watir, and other web-based testing tools.
- In today’s time, most people are using Cucumber as BDD tools.
Advantages of Cucumber Over Other Tools
- Cucumber supports different languages like Java.net and Ruby.
- It acts as a bridge between the business and technical language. We can accomplish this by creating a test case in plain English text.
- It allows the test script to be written without knowledge of any code, it allows the involvement of non-programmers as well.
- It serves the purpose of an end-to-end test framework, unlike other tools.
- Due to simple test script architecture, Cucumber provides code reusability.
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.
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 such as Business Analysts. Cucumber also comes with its ability to support multiple scripts and programming languages and JUnit is used to execute these scripts and generate the output.
Cucumber Selenium WebDriver Integration
Testers can use the Cucumber framework to test the web-based applications along with Selenium WebDriver. Testers write these test cases in simple feature files which are easy to understand by managers, non-technical stakeholders, and business analysts. And then implement those feature file steps in the step definition file. If you are using maven then you have to add dependencies for Cucumber and WebDriver.
So here is the sample test case we have implemented using Cucumber and WebDriver. As given below, the scenario in the feature file is self-explanatory.
Feature: Login Feature File
@selenium
Scenario: Login scenario test for Gmail
Given navigate to Gmail page
When user logged in using username as “userA” and password as “password”
Then home page should be displayed
WebDriver Implementation in Cucumber step definitions:
public class stepDefinition {
WebDriver dr;
@Given("^navigate to gmail page$")
public void navigate(){
dr=new FirefoxDriver();
dr.get("http://www.gmail.com");
}
@When ("^user logged in using username as \"(.*)\" and password as \"
(.*)\"$")
public void login(String username,String password){
dr.findElement(By.xpath("//*[@id='Email']")).sendKeys(username);
dr.findElement(By.xpath("//*[@id='Passwd']")).sendKeys(password);
dr.findElement(By.xpath("//*[@id='signIn']")).click();
dr.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Then("^home page should be displayed$")
public void verifySuccessful(){
String expectedText="Gmail";
String actualText= dr.findElement(By.xpath("//*
[@id='gbq1']/div/a/span")).getText();
Assert.assertTrue("Login not successful",expectedText.equals(actualText));
}
}
In this test, we have used Firefox as the browser to test the Gmail login functionality.
Given statement initializes the browser and navigates to the page.
When statement logs into the application using the username as “user A” and password as “password”. Both the values ‘username’ and ‘password’ pass from the feature file and uses both values in the same order.
Then Statement only validates the conditions after logging into the application.
This is a sample test describing the usage of Cucumber and Selenium. You can create multilayer architecture depending on your project requirement.
Conclusion:
So, that was all about the Integration of Cucumber with Selenium. In this Cucumber Selenium Java Integration blog, I have covered most of the Cucumber concepts which include Cucumber features and its usage along with WebDriver.
This reduces the complexity of code that is written to design the traditional frameworks like Keyword Driven and Hybrid Framework. Cucumber is used in most of the projects where people follow the agile methodology as Behavior Driven Development is an Agile Software practice. I hope you understood the concepts and helped in adding value to your knowledge.
For more Reference Visit:
https://www.selenium.dev/documentation/webdriver/





