cucumber
What is BDD?
TLD: Test Last Development (Coding -> Tests Creation)
TDD: Test Driven Develoment (Tests Creation -> Coding)
BDD: Behavior Driven Development
- BDD is an extension of TDD
- a bahavior of the application
- more user-focused
- use human-readable description of user requirements
- written in a shared language(tech non-tech stakeholders)
- e.g. Gherkin(Cucumber provided)
Gherkin will look like
1
2
3
4
5
6Feature:
Scenario:
Given
When
And
ThenTools for BDD: helps in Creating & Automating BDD user stories
Cucumber
JBehave
Behat
Set a Cucumber Java Project
- create a new maven project
- add maven dependencies
- Cucumber Java
- Cucumber JUnit
- JUnit
- Selenium Java
- create a folder features under /test/resources
- create login feature under features
- download plugins to Idea Plugins
- Cucumber for Java
- Gherkin
- add content to feature
- try to run feature file
- add step definition / glue code under test/java package
- create a runner class
First Selenium Test
- add maven dependiency
- Selenium Java
- create feature and add content
- glue code
- download browser driver
- add selenium webdriver code
- run feature and check execution
Page Object Model
- Designe pattern to create Object Repository
- a class which created for each page to indentify web elements of that pag
- also contains methods to do action on the objects
- seperate test objects and test scripts
advantages
- maintainability
- usability
- readability
- easier faster reliable
Page Factory here
- a simple and easier implemtnation of POM in selenium
- selenium’s inbuild and optimize POM concept
- as POM,has seperation of objects and test
- use annotation @FindBy to find WebElements
- @FindBy can use id,name,css,xpath,tagName,linkText,partialLinkText
- @FindBy also can get element list
- use method initElements to initialize web elements
- on calling initElements method all objects in that page gets initialized
- PageFactoy.initElements(driver,HomePage_PF.class) get
OutOfMemoryException
1
2
3
4public HomePage_PF(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this); //right use
}
- use @CacheLookup to instruct the initElements() method to cache element once its located and do that it will not be searched over and over again whenever calling it from any method
- this works well with a basic web application
- not recommended if you have Ajax applicaiton where DOM changes on user actions
- in case ,you will get StaleElementExceptions,avoid using this
- for Ajax application:
- to handle loading time for element and to avoid ‘
NoSuchElementException
‘ - use AjaxElementLocatorFactory Class
- timeout for a WebElement can be assigned to the Object page class with the help of this class
1
PageFactory.initElements(new AjaxElementLocatorFactory(driver,30),this);
- to handle loading time for element and to avoid ‘
Tags
- Features and Scenarios can be marked by tags
- in test runner we canrun specific tags
- can run with single OR mutiples tags
- can run with combination of tags or using AND OR NOT conditions
"@smoke"
"@smoke or @regression"
"@smoke and @regression"
"@smoke","@regression"
"@smoke and not @regression"
- can skip scnarios having specific tags
- a feature or scenarios can have mutiples tags
- tags can be placed above the following Gherkin elements:
- Feature
- Scenario
- Scenario Outline
- Examples
- not possible to place tags above Background or steps(Given,When,Then,And,But)
- create commands with tags combination as required to be run from command line
mvn test -Dcucumber.filter.tags="@smoke and @regression"
Hooks
- Blocks of code that runs before OR after each scenarios
- Hooks in Cucumber are like Listeners in TestNG
- can define hook by using annotations @Before @After
- Scenario Hooks - run before and after each scenarios
- Step Hook - run before and after each step
- Condition Hooks - hook asscociated with tags for condition execution
- Why to use Hooks
- to manage the setup and teardown
- to avoid rewriting the common setup or teardown actions
- allow better management of code workflow
@Before @After @BeforeSteps @AfterSteps @After(value="@smoke",order=2)
Backfround
- a step or a grounp of steps that are common to all the scenaiors in a feature
- is defined once in the feature
- runs before every scenario of the feature
- why use?
- to avoid repeating the common steps
- for better readability & maintainance
- unlike hoooks , background is vixible to the readers of the feature
Run form commoand line
- What
- run your test or features from command line or terminal without using IDE or GUI
- Command
- Terminal
- Console
- Why use
- no dependency on IDE or GUI
- useful in integration with other processes
- Continuous Integration
- Delivery and Deployment
- Easier & Faster
- consumes less memory
- When
- whenever fast run
- whenever need to integration with other processes CI CD DevOps
- whenever using CI tools like Jenkins
- whenever you need any batch or scheduled execution
- whenever you are done with your test creation and setup
- How
mvn test
mvn test -Dcucumber.options --help
use help of cucumbermvn test -Dcucumber.options="feature file path"
mvn test -Dcucumber.options="feature file path:7"
mvn test -Dcucumber.options="--tags @smoke"
mvn test -Dcucumber.options="--plugin html:target/HtmlReports"
mvn test -Dcucumber.options="" -Dcucumber.options=""
HTML Report
- How - maven cucumber reporting
- add maven cuucmber reporting dependency
- add the build plugins in pom.xml
mvn clean test
+mvn verify -DskipTests
=mvn clean verify