Annotations
- Cucumber Tutorial
- Cucumber Useful Resources
- Selected Reading
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) Omnidirectional microphone. apple plaintalk for mac.
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.
The @Access annotation is used to specify the access type of the associated entity class, mapped superclass, or the embeddable class and entity attribute. See the Access type section for more info. Annotating a book is a great way to make notes on the text. It also allows you to do a deep read of the book, where you jot down your thoughts or impressions of the text. You may need to annotate a book for a class or decide to try it to.
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.
What is shift work. 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.
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.
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.
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.
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 Webmin for mac.
Annotations Definition
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.
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.
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.
Annotations In Testng
Step 6
Run the test using option −
Annotations Youtube
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 −
Annotations Examples
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 '.