Documente Academic
Documente Profesional
Documente Cultură
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 working due
to the complexity of the code. To ease this task, Cucumber comes into the play, which
makes the complex code simple and easy to understand. But, how exactly it does that? I
will be telling you all about it through this article.
Introduction to Cucumber
1. Why Cucumber?
Introduction to Selenium
Introduction to Cucumber
Why Cucumber?
Well, Cucumber is one of the most popular tools because of the reasons listed below:
2. On using Cucumber, you can write your 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
testingtools.
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 makes it easy for a non-techie to understand the
test cases.
Let’s understand this with the help of a simple example. Consider a scenario where you
want to test the Twitter website. One of the test scenarios is to verify the login
credentials. With BDD, you can write this test scenario in this format:
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 the 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 is the preferred tool when it comes to automating the tests which are carried
out on web browsers. It is useful only for testing of web applications. Any desktop
(software) application or mobile application cannot be tested using Selenium. It is very
helpful in writing functional test cases. It also provides reliable performance with
‘ n’ number of test cases and it is obviously the best suitable automation tool for web
applications.
Now that you know what is Selenium, let’s move further in this article and understand
why use Cucumber with Selenium?
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.
Having understood this, let’s now see the various steps to create Cucumber application
and run the test cases.
1. Download the Jar files of Cucumber and Selenium and configure the build path.
2. Add Cucumber Plugin from Eclipse Marketplace.
Cucumber doesn’t actually interact with the browser and performs actions on the
website under test. Interacting with the website is the action that is performed by
Selenium WebDriver. Due to this reason, you will need to download Cucumber as well as
Selenium Webdriver jar files. Below is the list of jar files for Cucumber.
Cucumber-core
Cucumber-html
Cucumber-java
Cucumber-junit
Cucumber-jvm-deps
Cucumber-reporting
Hamcrest-core
Gherkin
Junit
Once you download these jar files from 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.
Step 2: The next step is to install Cucumber on your Eclipse. For that, you
need to go to Help -> Eclipse Marketplace -> Search Cucumber and
install Cucumber and Naturals on your eclipse. Below snapshot depicts the
same.
Step 3: 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.
Next, you need to specify the name of the feature file as shown below.
Once you create the feature file, you can write the scenarios as shown in the below code.
Test Scenario I:
Feature: Reset functionality on login page of Application
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;
Step 5: Now, the last step is to create the ‘ runner class’ and execute the
program. Basically, your project structure should look like as depicted in the
below snapshot.
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
feature file and Runner file. So that it will implement the methods and run the code.
When you run the program, it will display the methods from the step definition.
Your output should look like as shown below:
So, this is how you need to write all the files and execute the program.
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;
In the above program, I have used GeckoDriver to launch firefox browser and using
driver.get() it will navigate through the Twitter log in page. Further, it will locate the
username and password text box using 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 are passed or failed.
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. If you wish to check out more articles on the market’s most
trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can
refer to Edureka’s official site.