18. And I fill in "Email" with "homer@simpsons.com"
Feature: User Signup
In order to help other people learn Rails and get help myself
And I press "Sign Up"
As an interested community member
I want to sign up
Then I should receive an email
Scenario: Signing up to use the system
When I open the email
Given I go to the home page
And I press "Sign up"
Then I fill in "Login" with "homer" activate your Rails Mentors
And I should see "Please
And I press "Sign account" in the subject
And I fill in "Email" with "homer@simpsons.com"
Up"
When I open the email click the first link in the email
When I
Then I should receive an email
When I click the firstshould see "Almost done..."
Then I link in the email
Then I should see "Please activate your Rails Mentors account" in the subject
When I fill in fill in "Password" with "homer_rocks"
When I "Password" with "homer_rocks"
Then I should see "Almost done..."
And I press "Start in "Password Confirmation" with
And I fill using your account!"
And I fill in "Password Confirmation" with "homer_rocks"
"homer_rocks"
Then I should be on the dashboard page
And I should see "Your account has been activated. Welcome to Rails Mentors!"
And I press "Start using your account!"
And I should see "Please complete your profile!"
Then I should be on the dashboard page
And I should see "Your account has been activated.
Welcome to Rails Mentors!"
And I should see "Please complete your profile!"
22. Feature: Checking grades
As a nervous student who may not graduate
I want to be able to check my grades in
real time
So that I know I will get a diploma
30. Then I should see "Grades for Homer Simpson"
And I should see a table with "CS 462" in row 1, column 1
And I should see a table with "C-" in row 1, column 4`
39. steps to definitions
Then I should see "Grades for Homer Simpson"
Then /I should see "(.*)"/ do |text|
@browser.text.should include(text)
end
40. multiple parameters
When I fill in "username" with "bphogan"
When /^I fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
41. a small demo
Feature: Share code with others
In order to help a student who is in a different state
As a good teacher
I want to share some code I wrote over the internet
Scenario: Create a private paste so it won’t be found by Google
Given I go to "http://www.pastie.org/"
And I fill in "paste[body]" with "This is a simple test case using Cucumber"
And I select "Plain text" from "paste_parser_id"
And I check "paste[restricted]"
When I press "Paste"
Then I should see "less than a minute ago"
When I click "Edit"
Then I should see "This paste will be private."
We’re going to tlk about story-driven testing with Cucumber.
Testing web apps can be frustrating because there are so many places where things can go wrong. Most developers test web sites by running users through the application, but the manual clicking can take time.
Developers would rather be coding new features, and users would rather have a product that works well and not have their time wasted doing the developer’s job for them.
We don’t do things that are hard and not fun. It’s just human nature. We just kind of hide and pretend they’re not problems.
Unfortunately, this leads to shipping bad software.
...then the support calls come, and the angry emails, and the displeased shareholders.
Obviously there has to be some kind of workable solution, but what is it?
We can make the work easy by automating things. We spend so much time as developers automating processes for our clients that we never stop to think about how we might do that for ourselves and our processes.
We can reduce some of our work if we can get the users involved in a real way.
Finally, in order for this to work, management has to enforce testing practices. Applications have to be shipped with good test coverage, and time estimates must include the time it takes to write tests.
Everyone knows testing isn’t a waste of time. We may argue on how to do it, but everyone does some level of testing as they develop.
Unfortunately, every single developer can come up with a list of excuses why they don’t test enough. Usually they blame management or marketing for pushing deadlines.
Management has to make deadlines or stuff doesn’t get done, and they have outside forces pressuring them to deliver results.
Programmers know that if they rush, they’re going to do crappy work and while deadlines may be met in the short term, what is the cost of the technical debt incurred? Bad code needs to be addressed at some point. A professioal craftsman strives for quality.
So we have to be able to test quickly and easily
and we have to allow time to make it happen
and we can achieve some of this with Cucumber, a testing framework designed with the user experience in mind.
This scenario is written using Gherkin, Cucumber’s language for describing user interactions. It’s easy to read and easy to understand. You can sit down with your end users and write these scnearios together and then later use them to test your real application.
When thinking about a feature, you and your user must think about what value the feature has.
Ask yourselves “why do we need this feature? How will having this feature benefit a user?” The user might be a system administrator, a site owner, a basic user, or an anonymous guest, but you should only think about features that directly impact a user.
A feature can best be described by filling in these blanks: “As a somebody, I want to do something, so that I can do something else.
“
“As a nervous student who may not graduate, I want to be able to check my grades in real time so that I know I will get a diploma” is definitely focused on the user.
You describe a scenario using “given, when, then”.
The scenario describes the feature in more details, explaining how a user will move through the application to achieve the feature’s goal.
“Given” steps explain the prerequisites for the test. They let you specify any setup conditions that must have occurred outside of the process.
Given I am logged in as “homer” And I am a current student And I am on the My Info page and I recived a grade of “C-” in “CS 462”
“When” steps describe what you do during the scenario
When I click “Check my grades And I select “Current Term” And I press “View”
“Then” steps describe the outcome you expect.
Then I should see “Grades for Homer Simpson” And I should see a table with “CS 462” in row 1, column 1 And I should see a table with “C-” in row 1 column 4
Step definitions map these sentences to actual code.
We use Ruby and regular expressions to process each line in the story.
Even though we write these definitions in Ruby code, you can run Cucumber against anything that shows up in your browser, including Flash!
One popular way to run Cucumber stories is with WEBRAT. It’s a library mainly used in Rails applications to run a Ruby-based browser.
When you install webrat, you get a bunch of ready-made matchers for common tasks like finding items on a page, filling in forms, clicking links, and pressing buttons
WATIR, or “Web Application Testing In Ruby” automates Internet Explorer or Firefox and works well with Cucumber.
Steps are mapped by using regular expression capture values. The captured values can then be used in the step.
You can use multiple parameters too.
A small demo automating Pastie.
So, stories automate a browser
there are many more advanced features of Cucumber though.
One major advantage of this is that your features stay with your code. They stay fresh, and they change as your code changes. A new developer can look at these features, run them, and know exactly what the application does.
Your non-technical stakeholders, management, and other parties can easily communicate with the developers about features.
If you can get features that are well thought out, well planned, and well communicated in front of your developers so they can implement them, you’ll be a hero.