Session 10: BDD
Objectives
- Behavior Driven Development (BDD)
- Gherkin
- Build a BDD Framework
- Cucumber with JUnit html report
Notes
introduction_to_bdd_and_gherkin.pptx |
Steps to building a BDD Framework:
1. Download Plugin: Natural
a. Click help > Install New Software > Add
b. Type Name: Natural
c. Paste Location: http://rlogiacco.github.com/Natural
d. Check Natural > Click Next > Next > click "I Accept" > Finish > Install Anyway
e. Click Restart Now > Eclipse will restart.
2. Build a Maven Project with following 8 dependencies:
a. selenium-java,
b. junit
c. cucumber-java
d. cucumber-jvm-deps
e. cucumber-jvm
f. cucumber-junit
g. cucumber-reporting
h. gherkin
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.1.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId> gherkin </artifactId>
<version>2.12.2</version>
</dependency>
3. Add chromedriver.exe in the driver folder,
4. Create Folder/Package structure:
src/main/java
page
util
src/test/java
features
runners
steps
5. Create a feature file called login.feature
a. Right click on the features package > New > File,
b. Provide name: login.feature
c. Click Finish
6. Write the following scenario in the feature file,
Feature: As a user I want a login page so that only authentic
users will be able to login
Scenario: Valid users should be able to login
Given a valid user
When user goes to Techfios Site
Then Login Page should be displayed
When user logs in with valid credentials
Then Dashboard page should display
8. Create a Java class in the steps folder called LoginSteps,
9. Create a Java class in the runners folder called LoginRunners, and paste the following above the class block
a. First paste this before the class signature,
@RunWith(Cucumber.class)
@CucumberOptions(features = "./src/test/java/features/login.feature", glue = {
"steps" },
dryRun = false,
monochrome = true,
plugin = {"pretty", "html:test-output"}
)
Note: No space should be placed between the last parenthesis and the word "public class classname" and change dryRun to false to do an real run,
else, only compilation will take place without the browser.
b. Hover over @RunWith to import JUnit 4 Library,
c. Paste the following imports above @RunWith,
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
d. Paste the feature file location above
I. Right Click on login.feature > Properties
II. Copy the location excluding the "C:",
Or copy the path from "/src" and include a "." before that,
III. Paste after "features = " in LoginRunner.
11. Right click on the LoginRunner> Run As > JUnit Test
13. Copy the missing steps from the console and paste in the steps class,
14. Copy/paste util package with BrowserFactory in the main folder,
15. Copy/paste the page classes in the page package
Note: Error may persist if the entire feature file is written without step implementations. So it is best to proceed with increments.
Project: https://bitbucket.org/codestack1/techfios-bdd-advance.git
Homework
Automate the following scenario using BDD and push the code to create a pull request.
1. Login to TechFios
2. Navigate to Bank & Cash > New Account (Hint: Create navigation method in Dashboard Class)
3. Enter new account information (Hint: Create method in the Add Accounts Page class)
4. Verify success message appeared (Hint: Create method in the Add Accounts Page class)
5. Verify name appeared in the table (Hint: Dynamic Web-Table)
Detailed Instruction:
Level 1:
> Create two classes: NewAccountPage and NewAccountTest
> Create A test method in NewAccountTest class with TestNG annotation
Level 2:
> Copy paste existing methods to login and verify user logged in.
Level 3:
> Create Navigation Method in the Dashboard Page to navigate to New Account Page,
Level 4:
> Create addNewAccount method in the NewAccountPage class
Level 5:
> Write assertion to validate success message
Level 6:
> Write Assertion to validate table contains the newly added data.
OR
Level 7:
> Write Assertion to validate table contains the newly added data using dynamic web-table.
!!Congratulation!! You are an expert now!!
Tricks
“Ctrl + Shift + F + O” - Alight and remove unused imports.
“Ctrl + Alt + Down" - Duplicate the row where is the cursor is.
"Alt + Left" - Navigate back.
"Alt + Right" - Navigate forward.
“Ctrl + Alt + Down" - Duplicate the row where is the cursor is.
"Alt + Left" - Navigate back.
"Alt + Right" - Navigate forward.
References
|
|
the_cucumber_for_java_book.pdf |
cucumber_tutorial.pdf |
bdd_in_action.pdf |