Annotation 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.
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 "".