5. Tips & Tricks
● Use capture groups
● Distinguish singular/plural with '?'
● Use non-capturing groups (?:visit|go to)
When I visit the homepage
When I go to the homepage
● Use anchors (“^ … $”)
● Use tags and folders to organize
6. Use Doc Strings
Scenario: Ban Unscrupulous Users
When I behave unscrupulously
Then I should receive an email containing:
"""
Dear Sir,
Your account privileges have been revoked due to your unscrupulous behavior.
Sincerely,
The Management
"""
And my account should be locked
7. Doc Strings Step Defn
@When("^I behave unscrupulously$")
@Then("^I should receive an email containing:$")
public void I_should_receive_an_email_containing(String
arg1) throws Throwable {
throw new PendingException();
}
@Then("^my account should be locked$")
8. Data Tables
Scenario:
Given a board like this:
| | 1 | 2 | 3 |
| 1 | | | |
| 2 | | | |
| 3 | | | |
When player x plays in row 2, column 1
Then the board should look like this:
| | 1 | 2 | 3 |
| 1 | | | |
| 2 | x | | |
| 3 | | | |
9. Data tables Step Defn
@Given("^a board like this:$")
public void a_board_like_this(String[][] twoDStringArray)
{}
@When("^player x plays in row (d+), column (d+)$")
public void player_x_plays_in_row_column(int row, int
column) {}
@Then("^the board should look like this:$")
public void the_board_should_look_like_this(String[][]
twoDStringArray) {}
11. Tagged Hooks
@Before(value = {"@txn"}, order = 100)
public void startTransaction() {
}
@After(value = {"@txn"}, order = 100)
public void rollBackTransaction() {
}
SpringTransactionHooks
12. Global Hooks
@Before("@web")
public void beforeScenario() {
if (!tomcat.isRunning()) {
tomcat.start();
tomcat.deploy("myapp");
browser = new FirefoxDriver();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
browser.close();
tomcat.stop();
}
}); } }
13. Features vs Stories
Recommended NOT to do the following:
features/
story_38971_generate_new_report.feature
story_38986_run_report.feature
story_39004_log_in.feature
14. How NOT to have bored
stakeholders
Scenario: Check inbox
Given a User "Dave" with password "password"
And a User "Sue" with password "secret"
And an email to "Dave" from "Sue"
When I sign in as "Dave" with password
"password"
Then I should see 1 email from "Sue" in my
inbox
15. Avoid incidental details
Scenario: Check inbox
Given a User "Dave"
And a User "Sue"
And an email to "Dave" from "Sue"
When I sign in as "Dave"
Then I should see 1 email from "Sue" in my inbox
16. Can be further improved ...
Scenario: Check inbox
Given I have received an email from "Sue"
When I sign in
Then I should see 1 email from "Sue" in my inbox
17. Imperative steps
Feature: Animal Submission
As a Zoologist
I want to add a new animal to the site
So that I can share my animal knowledge with the community
Scenario: successful submission
Given I'm on the animal creation page
When I fill in Name with 'Alligator'
And select Phylum as 'Chordata'
And fill in Animal Class with 'Sauropsida'
And fill in Order with 'Crocodilia'
And fill in Family with 'Alligatoridae'
And fill in Genus with 'Alligator'
And check Lay Eggs
And click the Create button
Then I should see the notice 'Thank you for your animal submission!'
And the page should include the animal's name, phylum, animal
class, order, family, and genus
18. Declarative steps
Feature: Animal Submission
As a Zoologist
I want to add a new animal to the site
So that I can share my animal knowledge with the
community
Scenario: successful submission
Given I'm on the animal creation page
When I add a new animal
Then I should see the page for my newly created animal
And the notice 'Thank you for your animal submission!'
19. StepDefn for Declarative steps
@When("^I add a new animal$")
public void I_add_a_new_animal() throws Throwable {
fillsIn("Name",with("Alligator"));
selects("Chordata"),from("Phylum"));
fillsIn("Animal Class",with("Sauropsida"));
fillsIn("Order",with("Crocodilia"));
fillsIn("Family",with("Alligatoridae"));
fillsIn("Genus",with("Alligator"));
checks("Lay Eggs");
clicks_button("Create");}
20. Don't make it too declarative …
Scenario: The whole system
Given the system exists
When I use it
Then it should work, perfectly
22. Composite steps in JBehavior
@Given("%customer has previously bought a %product") //
used in normal parameter matching
@Alias("<customer> has previously bought a <product>") //
used in parameterised scenarios
@Composite(steps = { "Given <customer> is logged in",
"Given <customer> has a cart",
"When a <product> is added to the cart" })
public void aCompositeStep(@Named("customer") String
customer, @Named("product") String product) { }