Thursday, July 20, 2023

Cucumber - Gherkins

Introduction:

Before proceeding with this tutorial, you need to have a basic knowledge on testing as well as some hands-on experience of some testing tools. You should have a commanding knowledge on Java, and some familiarity with JUnit and Ruby.

In order to get better advantage of the software testing, organizations are nowadays taking a step forward. They implement important acceptance test scenarios while development is in-progress. This approach is commonly known as Behavior Driven Development (BDD).

What is BDD?

  Behavior-Driven Development (BDD) is a development method that encourages communication between teams. This collaborative approach brings the business and the technical aspects of projects together. This method allows teams to better communicate on requirements, detect problems early on, and maintain software over time with ease.

Teams that use BDD have a couple of goals. The first is to make sure that the requirements can be understood by the entire team. Then the teams can focus on preventing possible issues, rather than putting out fires if they’re found later. Often this means that less re-working is required. 

Behavior Driven Development gives us an opportunity to create test scripts from both the developer’s and the customer’s perspective as well. So in the beginning, developers, project managers, QAs, user acceptance testers and the product owner (stockholder), all get together and brainstorm about which test scenarios should be passed in order to call this software/application successful. This way they come up with a set of test scenarios. All these test scripts are in simple English language, so it serves the purpose of documentation also.

Simply we can describe this development in two phases. Discovery and testing.  

  1. First, teams figure out what they don’t know BEFORE they start working. Then, they have a better understanding of how to stay on track and be more productive. 
  2. When it comes to testing, the process of thinking about these tests starts before development even begins. Tests are written to drive the implementation and the final product.

Example

If we are developing a user authentication feature, then the following can be few key test scenarios, which needs to get passed in order to call it a success.

  • The user should be able to login with correct username and correct password.
  • The user should not be able to login with incorrect username and correct password.
  • The user should not be able to login with correct username and incorrect password.


What is Cucumber?

Cucumber is a popular open-source tool and framework that supports BDD practices. It allows you to write executable specifications in a human-readable format using the Gherkin language. Cucumber enables collaboration among team members and provides a way to automate and execute those specifications as tests. It supports various programming languages, including Java, Ruby, JavaScript, and more, allowing teams to implement step definitions in their preferred language.

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 end-to-end test framework unlike other tools.
  • Due to simple test script architecture, Cucumber provides code reusability.

Prerequisites for Environment Setup

Following are the prerequisites required to set up with − Java

Why we need − Java is a robust programming language. Cucumber supports Java platform for the execution.

How to install −

Step 1 − Download jdk and jre from the following link http://www.oracle.com/technetwork/java/javase/downloads/index.html

Step 2 − Accept license agreement.

Step 3 − Install JDK and JRE.

Step 4 − Set the environment variable as shown in the following screenshots.

System PropertiesEdit System Variable

Eclipse
Maven
System VariablesEdit Path Variable
Configure Cucumber with Maven
New Maven Project
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
Package Explorer

Why we need − Eclipse is an Integrated Development Environment (IDE). It contains a base workspace and an extensible plug-in system for customizing the environment.

How to install −

Step 1 − Make sure JAVA is installed on your machine.

Step 2 − Download Eclipse from https://eclipse.org/downloads/

Step 3 − Unzip and Eclipse is installed.

Why we need − Maven is a build automation tool used primarily for Java projects. It provides a common platform to perform activities like generating source code, compiling code, packaging code to a jar, etc. Later if any of the software versions gets changed, Maven provides an easy way to modify the test project accordingly.

How to install −

Step 1 − Download Maven from the following link − https://maven.apache.org/download.cgi

Step 2 − Unzip the file and remember the location.

Step 3 − Create environment variable MAVEN_HOME as shown in the following screenshot.

Step 4 − Edit Path variable and include Maven as shown in the following screenshot.

Step 5 − Download MAVEN plugin from Eclipse.

Step 6 − Open Eclipse.

Step 7 − Go to Help → Eclipse Marketplace → Search Maven → Maven Integration for Eclipse → INSTALL.

Step 1 − Create a Maven project.

  • Go to File → New → Others → Maven → Maven Project → Next.

  • Provide group Id (group Id will identify your project uniquely across all projects).

  • Provide artifact Id (artifact Id is the name of the jar without version. You can choose any name, which is in lowercase). Click on Finish.

Step 2 − Open pom.xml.

  • Go to package explorer on the left hand side of Eclipse.

  • Expand the project CucumberTest.

  • Locate pom.xml file.

  • Right-click and select the option, open with “Text Editor”.

Step 3 − Add dependency for selenium: This will indicate Maven which Selenium jar files are to be downloaded from the central repository to the local repository.

  • Open pom.xml is in the edit mode, create dependencies tag (<dependencies></dependencies>), inside the project tag.

  • Inside the dependencies tag, create dependency tag (<dependency></dependency>).

  • Provide the following information within the dependency tag.

Step 4 − Add dependency for Cucumber-Java: This will indicate Maven, which Cucumber files are to be downloaded from the central repository to the local repository.

  • Create one more dependency tag.

  • Provide the following information within the dependency tag

Step 5 − Add dependency for Cucumber-JUnit: This will indicate Maven, which Cucumber JUnit files are to be downloaded from the central repository to the local repository.

  • Create one more dependency tag.

  • Provide the following information within the dependency tag

Step 6 − Add dependency for JUnit: This will indicate Maven, which JUnit files are to be downloaded from the central repository to the local repository.

  • Create one more dependency tag.

  • Provide the following information within the dependency tag.

Step 7 − Verify binaries.

  • Once pom.xml is edited successfully, save it.

  • Go to Project → Clean − It will take a few minutes.

You will be able to see a Maven repository like shown in the following screenshot.

  • Create a feature file (to be covered later).

  • Create a step definition file (to be covered later).

  • Create a JUnit runner to run the test (to be covered later).

What is Gherkin?

Gherkin is a domain-specific language (DSL) that is used to describe the behavior of a software system in a structured and readable manner. It serves as the syntax for writing BDD-style acceptance criteria and test scenarios. Gherkin uses a simple, keyword-driven syntax that makes it accessible to non-technical stakeholders. It is designed to be easily understandable by both technical and non-technical team members, fostering effective collaboration.

Gherkin follows a structured format with keywords such as Feature, Scenario, Given, When, Then, And, and But. It allows you to define features, scenarios, and their associated steps in a hierarchical and reusable manner. Gherkin files, written in the Gherkin language, are used as a common language for communication and documentation between business stakeholders, developers, and testers.

Feature

A Feature can be defined as a standalone unit or functionality of a project. Let’s take a very common example of a social networking site. How does the feature of this product/project look like? Few basic features can be determined as −

    • Create and remove the user from the social networking site.
    • User login functionality for the social networking site.
    • Sharing photos or videos on the social networking site.
    • Sending a friend request.
    • Logout.
Example
Feature − Login functionality for a social networking site.
The user should be able to login into the social networking site if the username and the password are correct.
    • Feature − Name of the feature under test.
    • Description (optional) − Describe about feature under test.
    • Scenario − What is the test scenario.
    • Given − Prerequisite before the test steps get executed.
    • When − Specific condition which should match in order to execute the next step.
    • Then − What should happen if the condition mentioned in WHEN is satisfied.

By now, it is clear that, each independent functionality of the product under test can be termed as a feature when we talk about Cucumber. It is a best practice later when you start testing, that before deriving the test scripts, we should determine the features to be tested.

A feature usually contains a list of scenarios to be tested for that feature. A file in which we store features, description about the features and scenarios to be tested is known as Feature File. We will see more about feature files in the following chapter.

The keyword to represent a feature under test in Gherkins is “Feature”. The suggested best practice is, to write a small description of the feature beneath the feature title in the feature file. This will fulfill the need of a good documentation as well.

The user should be shown the error message if the username and the password are incorrect.

The user should be navigated to home page, if the username and password are correct.

Feature Files

The file, in which Cucumber tests are written, is known as feature files. It is advisable that there should be a separate feature file, for each feature under test. The extension of the feature file needs to be “.feature”.

One can create as many feature files as needed. To have an organized structure, each feature should have one feature file.

For Example

Sr.No   Feature                Feature File name

  1         User Login         userLogin.feature

  2         Share the Post         sharePost.feature

  3         Create Account createAccount.feature

  4         Delete Account deleteAccount.feature

The naming convention to be used for feature name, feature file name depends on the individual’s choice. There is no ground rule in Cucumber about names.

A simple feature file consists of the following keywords/parts −

Example

Feature − User login on social networking site.

The user should be able to login into the social networking site when the username and the password are correct.

The user should be shown an error message when the username and the password are incorrect.

The user should be navigated to the home page if the username and the password are correct.

Outline − Login functionality for a social networking site.

The given user navigates to Facebook. When I enter Username as "<username>" and Password as "<password>". Then, login should be unsuccessful.

                                        | username  | password  |

                                        | username1 | password1 |

* AND keyword is used to show conjunction between two conditions. AND can be used with any other keywords like GIVEN, WHEN and THEN.

There are no logic details written in the feature file.

Steps Definitions

We have got our feature file ready with the test scenarios defined. However, this is not the complete job done. Cucumber doesn’t really know which piece of code is to be executed for any specific scenario outlined in a feature file.

This calls the need of an intermediate – Step Definition file. Steps definition file stores the mapping between each step of the scenario defined in the feature file with a code of function to be executed.

So, now when Cucumber executes a step of the scenario mentioned in the feature file, it scans the step definition file and figures out which function is to be called.

Example of Step Definition File

public void goToFacebook() { 
   driver = new FirefoxDriver(); 
   driver.navigate().to("https://www.facebook.com/"); 
@When "^user logs in using Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$"
public void I_enter_Username_as_and_Password_as(String arg1, String arg2) {
   driver.findElement(By.id("email")).sendKeys(arg1);
   driver.findElement(By.id("pass")).sendKeys(arg2);
   driver.findElement(By.id("u_0_v")).click(); 
@Then"^login should be unsuccessful$" 
public void validateRelogin() { 
   if(driver.getCurrentUrl().equalsIgnoreCase(
      "https://www.facebook.com/login.php?login_attempt=1&lwv=110")){ 
         System.out.println("Test Pass");
   } else { 
      System.out.println("Test Failed"); 
   } 
   driver.close(); 

}


So with each function, whatever code you want to execute with each test step (i.e. GIVEN/THEN/WHEN), you can write it within Step Definition file. Make sure that code/function has been defined for each of the steps.

This function can be Java functions, where we can use both Java and Selenium commands in order to automate our test steps.

Scenario

Scenario is one of the core Gherkin structures. Every scenario starts with the keyword “Scenario:” (or localized one) and is followed by an optional scenario title. Each feature can have one or more scenarios and every scenario consists of one or more steps. A very simple example of scenario can be −

Scenario − Verify Help Functionality.

Given user navigates to Facebook.

When the user clicks on Help, then the Help page opens.

Consider a case, where we need to execute a test scenario more than once. Suppose, we need to make sure that the login functionality is working for all types of subscription holders. That requires execution of login functionality scenario multiple times. Copy paste the same steps in order to just re-execute the code, does not seem to be a smart idea. For this, Gherkin provides one more structure, which is scenario outline.

Scenario outline is similar to scenario structure; the only difference is the provision of multiple inputs. As you can see in the following example, the test case remains the same and non-repeatable. At the bottom we have provided multiple input values for the variables “Username” and “Password”. While running the actual test, Cucumber will replace the variable with input values provided and it will execute the test. Once pass-1 has been executed, the test will rerun for second iteration with another input value. Such variable or placeholders can be represented with ”<>” while mentioning with gherkin statements.

Example
| username | password |
| user1 | password1 |
| user2 | password2 |

  • Given the user enters the user name.
  • Clicks on login.

Scenario Outline − Login functionality for a social networking site. The given user navigates to Facebook.

When the user logs in using the Username as "<username>" and the Password as "<password>", then login should be successful.

There are a few tips and tricks to smartly define the Cucumber scenarios.

  1. Each step should be clearly defined, so that it does not create any confusion for the reader.
  2. Do not repeat the test scenario, if needed use scenario outline to implement repetition.
  3. Develop a test step in a way that, it can be used within multiple scenarios and scenario outlines.
  4. As far as possible, keep each step completely independent. For example: “Given the user is logged in”. This can be divided into two steps              

  • Given the user enters the user name.
  • Clicks on login.

Annotation:

It is a predefined text, which holds a specific meaning. It lets the compiler/interpreter know, what should be done upon execution. Cucumber has got the following few annotations −

  • Given −

      • It describes the pre-requisite for the test to be executed.
      • Example − GIVEN I am a Facebook user
  • When −

      • It defines the trigger point for any test scenario execution.
      • Example − WHEN I enter "<username>"
  • Then −

      • Then holds the expected result for the test to be executed.
      • Example − THEN login should be successful.
  • And −

      • It provides the logical AND condition between any two statements. AND can be used in conjunction with GIVEN, WHEN and THEN statement.
      • Example − WHEN I enter my "<username>" AND I enter my "<password>"
  • But −

      • It signifies logical OR condition between any two statements. OR can be used in conjunction with GIVEN, WHEN and THEN statement.
      • Example − THEN login should be successful. BUT home page should not be missing.
  • Scenario −

    • Details about the scenario under the test needs to be captured after the keyword “Scenario:”
    • Example −

Scenario:

GIVEN I am a Facebook user

WHEN I enter my

AND I enter my

THEN login should be successful.

BUT home page should not be missing.

  • Scenario Outline − (To be covered later)

  • Examples − (To be covered later)

  • Background −

    • Background generally has the instruction on what to setup before each scenario runs. However, it gets executed after “Before” hook (to be covered later). So this is ideal to be used for code when we want to set up the web-browser or we want to establish the database connectivity.

    • Example −

Background:

Go to Facebook home page.

Example Scenario

Let’s automate a scenario in order to understand annotations better.

Step 1

Create a Maven Test Project named as AnnotationTest.

  • Go to File → New → Others → Maven → Maven Project → Next.
  • Provide group Id (group Id will identify your project uniquely across all projects).
  • Provide artifact Id (artifact Id is the name of the jar without version. You can choose any name which is in lowercase).
  • Click on Finish.
  • Open pom.xml −
    • Go to package explorer on the left hand side of Eclipse.
    • Expand the project AnnotationTest.
    • Locate pom.xml file.
    • Right-click and select the option, Open with “Text Editor”.
  • Add dependency for Selenium − This will indicate Maven, which Selenium jar files are to be downloaded from the central repository to the local repository.

    • Open pom.xml is in edit mode, create dependencies tag (<dependencies></dependencies>), inside the project tag.
    • Inside the dependencies tag, create dependency tag (<dependency></dependency>).
    • Provide the following information within the dependency tag.

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
  • Add dependency for Cucumber-Java − This will indicate Maven, which Cucumber files are to be downloaded from the central repository to the local repository.

    • Create one more dependency tag.
    • Provide following information within the dependency tag.

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
  • Add dependency for Cucumber-JUnit − This will indicate Maven, which Cucumber JUnit files are to be downloaded from the central repository to the local repository.

    • Create one more dependency tag.
    • Provide the following information within the dependency tag.

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
  • Add dependency for JUnit − This will indicate Maven, which JUnit files are to be downloaded from the central repository to the local repository.

    • Create one more dependency tag.
    • Provide the following information within the dependency tag.

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
  • Verify binaries.

    • Once pom.xml is edited successfully, save it.
    • Go to Project → Clean − It will take a few minutes.

Step 2

Create a package named Annotation under src/test/java

    • Select the newly created project.
    • Right-click and select ‘New’.
    • Select option as ‘Package’.
    • Name it as ‘Annotation’.
    • Save it.

Step 3

Create a feature file named annotation.feature.

    • Select and right-click on the package outline.
    • Click on ‘New’ file.
    • Give the file a name such as outline.feature.
    • Write the following text within the file and save it.

Feature: annotation
#This is how background can be used to eliminate duplicate steps

Background: User navigates to Facebook Given I am on Facebook login page #Scenario with AND Scenario: When I enter username as "TOM" And I enter password as "JERRY" Then Login should fail #Scenario with BUT Scenario: When I enter username as "TOM" And I enter password as "JERRY" Then Login should fail
But Relogin option should be available

Step 4

Create a step definition file.

    • Select and right-click on the package outline.
    • Click on ‘New’ file.
    • Give the file name as annotation.java
    • Write the following text within the file and save it.

package Annotation;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;

public class annotation {
WebDriver driver = null;
@Given("^I am on Facebook login page$")
public void goToFacebook() {
driver = new FirefoxDriver();
driver.navigate().to("https://www.facebook.com/");
}
@When("^I enter username as \"(.*)\"$")
public void enterUsername(String arg1) {
driver.findElement(By.id("email")).sendKeys(arg1);
}
@When ("^I enter password as \"(.*)\"$")
public void enterPassword(String arg1) {
driver.findElement(By.id("pass")).sendKeys(arg1);
driver.findElement(By.id("u_0_v")).click();
}
@Then("^Login should fail$")
public void checkFail() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test1 Pass");
} else {
System.out.println("Test1 Failed");
}
driver.close();
}
@Then("^Relogin option should be available$")
public void checkRelogin() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test2 Pass");
} else {
System.out.println("Test2 Failed");
}
driver.close();
}
}

Step 5

Create a runner class file.

    • Select and right-click on the package outline.
    • Click on ‘New’ file.
    • Give the file a name, such as runTest.java
    • Write the following text within the file and save it.

package Annotation;

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})

public class runTest { }

Step 6

Run the test using option −

    • Select runTest.java file from the package explorer.
    • Right-click and select the option ‘Run as’
    • Select JUnit test.

You will observe the following things when you run this class file −

    • Facebook opens in a new Firefox web-browser instance.
    • TOM will be passed as an input to the username field.
    • JERRY will be passed as an input to the password field.
    • Login will be clicked.
    • Message will be displayed on the browser regarding unsuccessful login.
    • In the console, you will see “Test Pass” printed
    • Step result 1. to 5. Will be re-executed for username as "" and password as "".

Scenario outline: It is basically replaces variable/keywords with the value from the table. Each row in the table is considered to be a scenario.

Let’s continue with the same example of Facebook login feature. So far we have been executing one scenario: Upon providing the correct user name, login is successful. Now, suppose we want to check if login is successful for all three possible types of inputs, which are username, email address, or phone number. To achieve this, we will need to write three different scenarios, where each scenario will vary with the type of input, login is successful. In this case, scenarios will look like the following.

Scenario:

Given user navigates to Facebook

When I enter correct username and password

Then login should be successful

Scenario:

Given user navigates to Facebook

When I enter correct email address and password

Then login should be successful

Scenario:

Given user navigates to Facebook

When I enter correct phone number and password

Then login should be successful

Here, if we see closely, for above three scenarios: statements are the same, only the input parameter (username/email address/phone number) is changing. That’s where the importance of scenario outline comes into picture.

When we define any scenario with scenario outline, we can specify one test scenario and at the bottom of it we can provide a number of inputs. The scenario will get executed as many times as the number of inputs provided.

Example

Let’s create an example of a scenario outline −

Step 1 − Create a Maven Test Project named as ScenarioOutlineTest

  • Go to File → New → Others → Maven → Maven Project → Next.
  • Provide group Id (group Id will identify your project uniquely across all projects).
  • Provide artifact Id (artifact Id is the name of the jar without version. You can choose any name which is in lowercase).
  • Click on Finish.
  • Open pom.xml −
    • Go to package explorer on the left hand side of Eclipse.
    • Expand the project CucumberTest.
    • Locate pom.xml file.
    • Right-click and select the option, Open with “Text Editor”.
  • Add dependency for Selenium − This will indicate Maven, which Selenium jar files are to be downloaded from the central repository to the local repository.

    • Open pom.xml is in edit mode, create dependencies tag (<dependencies></dependencies>), inside the project tag.
    • Inside the dependencies tag, create dependency tag (<dependency></dependency>).
    • Provide the following information within the dependency tag.

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
  • Add dependency for Cucumber-Java: This will indicate Maven, which Cucumber files are to be downloaded from the central repository to the local repository.

    • Create one more dependency tag.
    • Provide following information within the dependency tag.

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
  • Add dependency for Cucumber-JUnit: This will indicate Maven, which Cucumber JUnit files are to be downloaded from the central repository to the local repository.

    • Create one more dependency tag.
    • Provide the following information within the dependency tag.

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.2</version>
<scope>test</scope>
</dependency>
  • Add dependency for JUnit - This will indicate Maven, which JUnit files are to be downloaded from the central repository to the local repository.

    • Create one more dependency tag.
    • Provide the following information within the dependency tag.

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
  • Verify binaries.

    • Once pom.xml is edited successfully, save it.
    • Go to Project → Clean − It will take a few minutes.

Step 2 − Create a package named “outline” under src/test/java

Java Package

Step 3 − Create a feature file named “outline.feature”

  • Select and right-click on the package outline.
  • Click on ‘New’ file.
  • Give the file name such as “outline.feature”
    • Write the following text within the file and save it.

Feature − Scenario Outline

Scenario Outline − Login functionality for a social networking site.

Given user navigates to Facebook

When I enter Username as "<username>" and Password as "<password>"

Then login should be unsuccessful

Example −

| username | password |
| username1 | password1 |
| username2 | password2 |

Note − Here, example annotation describes the range of input to be provided upon scenario execution. Test scenario will be executed for each of the input provided. So, in the given example, test scenario will be executed three times.

Step 4 − Create a step definition file.

  • Select and right-click on the package outline.
  • Click on ‘New’ file.
  • Give the file name as stepdefinition.java
  • Write the following text within the file and save it.

package Outline;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;

public class stepdefinition {
WebDriver driver = null;
@Given("^user navigates to facebook$")
public void goToFacebook() {
driver = new FirefoxDriver();
driver.navigate().to("https://www.facebook.com/");
}
@When("^I enter Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$")
public void I_enter_Username_as_and_Password_as(String arg1, String arg2) {
driver.findElement(By.id("email")).sendKeys(arg1);
driver.findElement(By.id("pass")).sendKeys(arg2);
driver.findElement(By.id("u_0_v")).click();
}
@Then("^login should be unsuccessful$")
public void validateRelogin() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test Pass");
} else {
System.out.println("Test Failed");
}
driver.close();
}
}

Note − In the above code, we have to define a function having two input argument: one username and other will be for password. So for each set of input provided in the example tag, Set of GIVEN, WHEN and THEN will be executed.

Step 5 − Create a runner class file.

  • Select and right-click on the package outline.
  • Click on ‘New’ file.
  • Give the file name such as runTest.java
  • Write the following text within the file and save it.

package Outline;

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber"})

public class runTest { }
  • Run the test using option −

    • Select runTest.java file from package explorer.
    • Right-click and select the option ‘Run as’.
    • Select JUnit test.

You will observe the following things when you run this class file

    • Facebook opens in a new Firefox web-browser instance.
    • Username1 and password1 will be passed as an input to username and password field.
    • Login will be clicked.
    • Message will be displayed on the browser regarding unsuccessful login.
    • In the console, you will see “Test Pass” printed.
    • Step result 1 to 5 will be re-executed for username2 and password2.

 Thank you


Venkata kishore T(Intern)
Shield Warriors,
Data Shield Team,
Enterprise Minds.

 

EM-Tirupati Codeathon Series #08

[Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The fo...