Want to load your Application logs with multiple ips during the load test? Check it out now
...RECENT BLOG

2020 / 2 / 27

2020 / 2 / 27

2020 / 2 / 27

2020 / 2 / 27

2020 / 2 / 27
Cucumber is a testing framework which supports BDD Behavior Driven Development. It helps us to define the application behavior in a simple plain english using a simple grammar defined language Gherkin. Cucumber is written in Ruby, but it can be used to test code written in Ruby, Java, C#, Python and many more.
In this series of articles we will be discussing
Setting up Cucumber is not a simple tsk. It has some prerequisites softwares that need to setup correctly in order to use Cucumber. We will start discussing installation of each of the software in detail.
Download and Install Java:
Since our tests will be written using Java language, so we will be requiring JDK- Java Development Kit.
NOTE: Replace this with path of bin folder in your jdk directory. This is for illustration purpose only.
Download and Install Eclipse:
Eclipse is an Integrated Development Environment (IDE). We are going to use eclipse to write, compile and run test scripts.
Install Cucumber Eclipse Plugin
Cucumber Eclipse Plugin is not the main cucumber eclipse plugin for running the test as BDD. When you write feature file for the cucumber test, the code for the feature file is not highlighted.
Cucumber Eclipse Plugin helps eclipse understand the basic Gherkin syntax and works like a syntax highlighter. Highlighting syntax makes it more readable and clear. It also helps to run feature file without the help of JUnit.
Download Cucumber for Eclipse:
In this step we will download the cucumber jar files for Eclipse. Cucumbers functionality is abstracted in jars. We will be using Online Maven Repository to download the jar files.
Steps to download all the above mentioned jar files:
I have downloaded following versions
Libraries | GroupID | Version |
---|---|---|
cucumber-core | io.cucumber | 4.7.2 |
cucumber-java | io.cucumber | 4.7.2 |
cucumber-junit | io.cucumber | 4.7.2 |
cucumber-jvm-deps | io.cucumber | 1.0.6 |
cucumber-reporting | net.masterthought | 4.9.0 |
gherkin | info.cukes | 2.12.2 |
junit | junit | 4.13-beta-3 |
mockito-all | org.mockito | 2.0.2-beta |
cobertura | net.sourceforge.cobertura | 2.1.1 |
Download Webdriver Java Client:
Selenium supports many languages and each language has its own client driver. Here we are configuring Selenium 4 with java so we need "Webdriver Java Client Driver".
Configure Eclipse with Cucumber
In order to configure Cucumber with Eclipse. We first need to launch the Eclipse, create a workspace, create a new Project and finally add External Libraries to the Project. Follow the steps below to correctly Configure Cucumber with Eclipse. If you listen to my advice, It is better to read one step and execute it at you end before going to the next step.
Steps to create Folder Structure:
Steps to write a Selenium Java Test:
We will write a simple Selenium Test Script for Login functionality and then convert that script to Cucumber script.
Code:
**package** cucumberTestScript;
**import** java.util.concurrent.TimeUnit;
**import** org.openqa.selenium.By;
**import** org.openqa.selenium.WebDriver;
**import** org.openqa.selenium.firefox.FirefoxDriver;
**public** **class** SeleniumTestScript {
**private** **static** WebDriver _driver_ = **null**;
**public** **static** **void** main(String[] args) {
_driver_ = **new** FirefoxDriver();// Create a New Chrome Driver
_driver_.manage().timeouts().implicitlyWait(10, TimeUnit.**_SECONDS_**);//Implicit wait means wait for mentioned number of seconds for a response before throwing exceptions.
_driver_.get("http://www.store.demoqa.com");// Launch the Online Store Website.
_driver_.findElement(By._xpath_(".//*[@id='account']/a")).click();//Find element with ID "account"
_driver_.findElement(By._id_("log")).sendKeys("testuser_1");// Find element with ID log and Enter Username in the element with ID "log"
_driver_.findElement(By._id_("pwd")).sendKeys("Test@123");// Find element with ID pwd and Enter Password in the element with ID "pwd"
_driver_.findElement(By._id_("login")).click();//Submit the form.
System.**_out_**.println("Login Successfully");// Print successful logIN Message.
_driver_.findElement (By._xpath_(".//*[@id='account_logout']/a")).click();// FInd element with ID Attribute "account_logout""
System.**_out_**.println("LogOut Successfully");//Print successful LogOut Message.
_driver_.quit();//Close the driver.
}
}
Right Click on the Eclipse code->Run As->Java Application.
After few seconds Chrome browser will open and you will see with the help of the script,Selenium will launch Online Store demo application and perform sign in.
Feature File
Feature file is an entry point to the cucumber tests. In this file we will describe test in descriptive language like English. It is an essential part of Cucumber as it serves as an automation script as well as a live Document.
Feature file can contain a scenario or many scenarios but usually it contains a list of scenarios.
Let’s get started with a Feature File.
We will write the first Cucumber Test Script.
Feature: Login Action
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User Navigate to LogIn Page
And User enters UserName and Password
Then Message displayed Login Successfully
Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
Don’t worry about the syntax and how to write a Cucumber Test Script, we will learn to write a Test Script later in this article. Till then just have a look and get a basic understanding of intent of the test.
Keywords:
You must have noticed from the above screenshot, that there are some highlighted words in the file. These are the Keywords from Gherkin language. This language we use basically for writing test script in Cucumber feature file. There are many more keywords in Gherkin. We will discuss later in the article. For the time being we will focus on the following 4 keywords:
Gherkin
Now let’s get back to Gherkin. It implements the principle of Business readable domain specific language(BRDSL). Domain specific language gives you the ability to describe your application behaviour.
Steps to install Natural Eclipse Editor for Gherkin:
You will get this option automatically when you create a file with .feature extension. Otherwise, you can anytime go to Eclipse Marketplace and look for the same to install it.
Cucumber uses Junit framework to run tests. Since cucumber uses Junit, we need to have a Test Runner class. This class uses the annotation @RunWith(), It is like a starting point for Junit to start executing test.
TestRunner Class
package cucumberTestScript;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
)
public class TestRunner {
}
For the curious minds, we will discuss the code.
Now it's finally time to run the Cucumber Test
Click on the TestRunner class and click Run As->JUnit Test Application.
You must be wondering where is the java code that will execute for these tests? You need to wait for another 5 minutes as I will be discussing the Java code in a short moment. First observe the output on the console window that appears after running the code that we have created till now.
Our next target is to execute feature file. In order to test feature file, we need to write step definition for each step in the feature file.
When Cucumber executes a step in a Scenario it will look for a matching Step Definition to execute.
Step Definition is basically a java method in a class with an annotation above it.
Create a new Class file in the “stepDefinition” package and name it as “TestSteps” by right click on the package-> New->Class. Do not select the public static void main option and click on Finish button.
Take a look at the message displayed in the console. Copy paste the text inside the red rectangle inside the TestSteps class created.
package stepDefinition;
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 Test_Steps {
public static WebDriver driver;
@Given("^User is on Home Page$")
public void user_is_on_Home_Page() throws Throwable {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.store.demoqa.com");
}
@When("^User Navigate to LogIn Page$")
public void user_Navigate_to_LogIn_Page() throws Throwable {
driver.findElement(By.xpath(".//*[@id='account']/a")).click();
}
@When("^User enters UserName and Password$")
public void user_enters_UserName_and_Password() throws Throwable {
driver.findElement(By.id("log")).sendKeys("testuser_1");
driver.findElement(By.id("pwd")).sendKeys("Test@123");
driver.findElement(By.id("login")).click();
}
@Then("^Message displayed Login Successfully$")
public void message_displayed_Login_Successfully() throws Throwable {
System.out.println("Login Successfully");
}
@When("^User LogOut from the Application$")
public void user_LogOut_from_the_Application() throws Throwable {
driver.findElement (By.xpath(".//*[@id='account_logout']/a")).click();
}
@Then("^Message displayed Logout Successfully$")
public void message_displayed_Logout_Successfully() throws Throwable {
System.out.println("LogOut Successfully");
}
}
Now right click on the TestRunner class-> Run As->JUnit Test.
You will see that Cucumber successfully launches browser and logout successfully.
Cucumber starts with reading the feature file. The moment it encounters the first annotation @Given, it will find the matching method in the stepDefinition file and executes the code written inside the function.
2020 / 2 / 27
2020 / 2 / 27
2020 / 2 / 27
2020 / 2 / 27
2020 / 2 / 27
IP Spoofing For Load Testing in JMeter
JMeter - How to handle SSL Handshake Exception?