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
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
- 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.
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.
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.
Successful Login to Test Application using Scenario Outline
Given User is at the login page
When User enters Username as < username > and Password as < password >
And User clicks on login button
Then User is able to successfully login to the Test Application.
You can then change the above step in the step definition file and you are doing with data-driven testing.
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!!
Fresher loves this complete guideline on what is Automated Software Testing and what are the Benefits here.