SlideShare a Scribd company logo
Cucumber
From the
Ground Up
Joseph E. Beale
02/17/2015
Who am I and why am I here?
Name: Joe Beale
What is essential to know about me:
 I am NOT brilliant.
What I hope to accomplish today:
 Prove that the bullet point above is
correct.
 Put to death any fear you might have
about writing test automation code.
Agenda
 Installation: 10:30 – 10:45
 Environment Set-up: 10:45 – 11:00
 Build and Test: 11:00 – 11:15
 Q&A + Fun Time: 11:15 – 11:30
Installation
Ruby Installer:
http://rubyinstaller.org/downloads/
Installation
MAKE SURE YOU CHECK THE THREE BOXES THAT
ARE RIGHT UNDER THE FOLDER SELECTION FIELD.
Installation
Download DevKit from the RubyInstaller page:
Extract to C:DevKit
Installation
At command prompt, check for correct
Ruby installation by typing:
 ruby –v [Enter]
Installation
To install DevKit, navigate to C:DevKit and
type the following:
 ruby dk.rb init [Enter]
 ruby dk.rb install [Enter]
Go back to the root directory (C:) and
install Cucumber by typing:
 gem install cucumber [Enter]
You now have Ruby and Cucumber fully installed!
Environment
Using Windows Explorer, create a new
folder on your C drive called test_project.
Environment
Inside of test_project, create a folder called
features, then inside of that create two
more: step_definitions and support.
Environment
RubyMine – A Robust IDE for Ruby Coding
RubyMine is also very Cucumber-friendly!
Environment
We will use RubyMine 5.4.3 for today’s
demo:
Environment
Use the Open Directory command to open
up our test_project folder.
Environment
Create a folder called env.rb inside of the
support folder using New –> File from the
right-click menu.
Environment
Need two more gems for this
demonstration:
 watir-webdriver
 rspec
Q: What is a Ruby gem?
A: A pre-built code module that performs
some useful function in Ruby, available for
free from https://rubygems.org/
Environment
Go back to your command prompt and
install the gems using this command:
 gem install <name of gem> [Enter]
Environment
Require statements tell Ruby that you are
going to use some extra code modules.
Q: Why env.rb in the support folder?
A: Cucumber knows to run env.rb before
anything else and looks for it in support.
Build and Test
Create a feature file and a test scenario.
Cucumber scenarios are written in ordinary
business language using Gherkin keywords
(“Feature:”, “Scenario:”, “Given”, “When”,
“Then”, “And”, “But”).
Build and Test
Create step definitions using RubyMine’s
built-in functionality.
Build and Test
RubyMine creates a new file called
my_steps.rb and adds code blocks with
regular expressions to match your Gherkin
steps.
Build and Test
First step:
Given I am on the Bing home page
To satisfy this step, we need to open a
browser and navigate to the Bing home
page.
How do we do this in Ruby?
Build and Test
Watir Webdriver is a gem that is built to drive
web applications. Since the code is
already written, we only need to learn how
to use it.
http://watirwebdriver.com/
Build and Test
Right on the front page is the code to
create a browser object (along with lots of
other stuff):
We will use these exact lines of code and
modify them slightly for our purposes.
Build and Test
The small ‘b’ in their examples stands for
‘browser’, but when we create an object
we will call it ‘browser’ to avoid confusion.
The browser object is created in env.rb so
that it is executed before any of the steps
Build and Test
Q: Why did you put a ‘$’ in front of your
object name?
A: To indicate that it’s a global variable.
We want the browser object to be
accessible to all steps, so we make it global.
Build and Test
In the first step definition, we will put the
next line of code, but modified to reflect
our global browser object and the URL for
Bing.com:
Now when we run our Cucumber scenario,
the first step will give us…
Build and Test
The Bing home page!
Build and Test
A quick word about method calls: the
format for calling a method in Ruby is
(object name) + dot (‘.’) + (method name).
So in the method call below, the object
name is ‘$browser’ and the method name is
‘goto’.
Build and Test
The ‘goto’ method is just one of the pre-defined
methods inside of the Watir::Browser class. The
method takes one argument: a URL (because it
needs to know where to “go to”).
Arguments can be passed either inside of
parentheses or alongside a method call. So in
the previous example, we could have said:
$browser.goto(‘http://bing.com’)
Build and Test
Second step:
When I search for "Death Star“
To satisfy this step, we need to interact with
two of the elements of the page:
 The search text field
 The search button (indicated by the
magnifying glass icon)
Build and Test
Watir Webdriver has several methods in the
Browser class to interact with the elements
that are common to most web pages. In
fact, the ones we need are also listed on
their front page:
Build and Test
To interact with the two search elements,
we will use the ‘text_field’ and ‘button’
methods. These methods each take one
argument: a location as indicated by the
syntax (:locater => ‘unique_identifier’). The
examples given are these:
b.text_field(:id => 'entry_0').set 'your name'
b.button(:name => 'submit').click
Build and Test
The locators are part of the html code for
the page elements, and this information is
acquired by using Firefox’s “Inspect
Element” tool.
So if you right-click on the search text field
and choose “Inspect Element” from the
drop-down menu, you will see the specific
html code for that element in the shaded
section.
Build and Test
The most common locator to use is ‘id’. So
we will copy the value ‘sb_form_q’ to use in
our argument to the ‘text_field’ method.
Build and Test
One nice feature of Ruby method calls is
the ability to “chain” your methods one
right after another. Ruby will execute them
in the order given, from left to right. That is
what you see in the the ‘text_field’
example:
b.text_field(:id => 'entry_0').set 'your name'
The ‘set’ method is executed right after the
‘text_field’ method.
Build and Test
You can use the ‘set’ method anytime you
have defined a text field element using the
‘text_field’ method. It takes one argument,
the word or phrase you wish to place in that
field. So, putting it all together, the code for
our second step will look like this:
Q: Where did ‘search_phrase’ come from?
Build and Test
As you may recall, our original step
definition looked like this:
I changed the default variable name ‘arg’
to one that reflects what it actually holds:
‘search_phrase’. The capture group with
wildcard grabs the phrase “Star Wars” from
the step and throws it into the variable.
Build and Test
Since the variable ‘search_phrase’ contains
a string, then it can be used as an
argument for the ‘set’ method. And so we
are then able to use that same Ruby code
block no matter what phrase is used in the
Gherkin step.
Step 
Step
Definition
Build and Test
Running the test now gives us…
That’s
No
Moon!!!
Build and Test
For the button element, we’ll do it exactly
like we did the text field, except instead of
the ‘set’ method, we’ll need to find one
that will click the button. The method name
in this case is – surprise! – ‘click’. Here’s the
example from Watir Webdriver’s front page:
b.button(:name => 'submit').click
Build and Test
The locator they are using is ‘name’, but in
our case there is no name, just a magnifying
glass symbol. So we’ll inspect the element
in order to find the ‘id’ for locating it:
Build and Test
Adding that into the step gives us this:
Again, you can see that we “chained” two
methods: first the ‘button’ method to locate
a specific element and then the
‘click’method. The latter method does not
require any arguments, for obvious reasons.
Build and Test
Now let’s run it again. Well, that’s better!
Build and Test
Third step:
Then the search results will
include "Star Wars“
Somehow we have to get our code to
examine the result page and check to see
if our result phrase “Star Wars” is found
anywhere. This is where the Rspec gem
comes into play.
Build and Test
You might recall that in the env.rb file we
told Ruby to require that code:
RSpec gives us the ability to “match” values
and phrases against an expected result.
Build and Test
For our purposes, the most relevant RSpec
method is ‘should’. This method will check
a value from our object against another
value and either pass or fail the step based
on whether or not it matches. Let’s see how
this step might be coded.
Q: What does the ‘text’ method do?
Build and Test
Many of the html elements on a web page will
have text associated with them, and most of
the time it’s displayed on the page.
For any given element, you can request the
associated text using the ‘text’ method. The
code we used, ‘$browser.text’ will give you all
the text on the page.
Build and Test
The ‘should’ method requires a “matcher”
like ‘include’ or ‘==‘ in order to compare
with the expected result. The ‘include’
matcher mimics the behavior of the Ruby
‘include?’ method that is part of the String
class.
If the expression matches the expected
value, then the test passes!
Build and Test
Running one more time gives us this result in
RubyMine:
Congratulations! You have just passed your
first Ruby/Cucumber automated test!
Q&A + Fun Time
You don’t have to be this guy to automate.
Q&A + Fun Time
In fact, even this guy could do it.
Q&A + Fun Time
Questions? Comments? Snark?
You can reach me at:
joseph_beale@att.net
or
joseph.beale92@gmail.com
or
Connect with me on Linked In!

More Related Content

PPTX
Behavior driven development - cucumber, Junit and java
PDF
Behavior Driven Development with Cucumber
PPTX
Gherkin for test automation in agile
PDF
A/B Testing at Scale: Minimizing UI Complexity (SXSW 2015)
PDF
An Introduction to Behaviour Driven Development with Cucumber Java
PDF
WE are Doing it Wrong - Dmitry Sharkov
PDF
[@IndeedEng] Managing Experiments and Behavior Dynamically with Proctor
PDF
Measuring Coverage From E2E Tests
Behavior driven development - cucumber, Junit and java
Behavior Driven Development with Cucumber
Gherkin for test automation in agile
A/B Testing at Scale: Minimizing UI Complexity (SXSW 2015)
An Introduction to Behaviour Driven Development with Cucumber Java
WE are Doing it Wrong - Dmitry Sharkov
[@IndeedEng] Managing Experiments and Behavior Dynamically with Proctor
Measuring Coverage From E2E Tests

What's hot (18)

PPTX
Refactoring page objects The Screenplay Pattern
PDF
Growing Manual Testers into Automators
PDF
Automate Debugging with git bisect
PDF
TDD on Android (Øredev 2018)
PPTX
So What Do Cucumbers Have To Do With Testing
PDF
Specification-by-Example: A Cucumber Implementation
PDF
From Good to Great: Functional and Acceptance Testing in WordPress.
PPTX
Behaviour Driven Development
PDF
@IndeedEng: Tokens and Millicents - technical challenges in launching Indeed...
DOC
Perl web programming
PPTX
ScreenPlay Design Patterns for QA Automation
PPTX
Automated tests
PPT
Google mock for dummies
PPT
Code Quality Practice and Tools
PDF
AngularJS Beginner Day One
PDF
TechSEO Boost 2018: Programming Basics for SEOs
PDF
Test Driven Development in AEM/CQ5
PPT
Selenium and Cucumber Selenium Conf 2011
Refactoring page objects The Screenplay Pattern
Growing Manual Testers into Automators
Automate Debugging with git bisect
TDD on Android (Øredev 2018)
So What Do Cucumbers Have To Do With Testing
Specification-by-Example: A Cucumber Implementation
From Good to Great: Functional and Acceptance Testing in WordPress.
Behaviour Driven Development
@IndeedEng: Tokens and Millicents - technical challenges in launching Indeed...
Perl web programming
ScreenPlay Design Patterns for QA Automation
Automated tests
Google mock for dummies
Code Quality Practice and Tools
AngularJS Beginner Day One
TechSEO Boost 2018: Programming Basics for SEOs
Test Driven Development in AEM/CQ5
Selenium and Cucumber Selenium Conf 2011
Ad

Viewers also liked (15)

PPTX
The Risky Business of Testing by Shaminder Rai and Dave Patel
PDF
Ready, set, go! - Anna Royzman
PPTX
STOP! You're Testing Too Much - Shawn Wallace
PDF
Training for Automated Testing - Kelsey Shannahan
PDF
When Cultures Collide – A tester’s story by Raj Subramanian
PDF
Feedback and its importance in delivering high quality software - Ken De Souza
PDF
Bad metric, bad! - Joseph Ours
PDF
Improv(e) your testing! - Damian Synadinos
PPTX
How to bake in quality in agile scrum projects
PDF
Combinatorial software test design beyond pairwise testing
PPTX
The Art of Gherkin Scripting - Matt Eakin
PPTX
Challenging Your Project’s Testing Mindsets - Joe DeMeyer
PPTX
UNIT TESTING PPT
PPTX
Unit Testing Concepts and Best Practices
PDF
Testing Microservices
The Risky Business of Testing by Shaminder Rai and Dave Patel
Ready, set, go! - Anna Royzman
STOP! You're Testing Too Much - Shawn Wallace
Training for Automated Testing - Kelsey Shannahan
When Cultures Collide – A tester’s story by Raj Subramanian
Feedback and its importance in delivering high quality software - Ken De Souza
Bad metric, bad! - Joseph Ours
Improv(e) your testing! - Damian Synadinos
How to bake in quality in agile scrum projects
Combinatorial software test design beyond pairwise testing
The Art of Gherkin Scripting - Matt Eakin
Challenging Your Project’s Testing Mindsets - Joe DeMeyer
UNIT TESTING PPT
Unit Testing Concepts and Best Practices
Testing Microservices
Ad

Similar to Cucumber From the Ground Up - Joseph Beale (20)

PPTX
Page object from the ground up by Joe Beale
PPTX
Page object from the ground up.ppt
PPTX
Test Automation using Ruby
PPT
What you can do In WatiR
PDF
Ruby with cucmber
PDF
Introduction to Selenium and Ruby
KEY
Week7
PPT
Watir Presentation Sumanth Krishna. A
PDF
full-stack-webapp-testing-with-selenium-and-rails.pdf
PPTX
Cucumber
PPTX
Capybara and cucumber with DSL using ruby
PDF
End-to-end web-testing in ruby ecosystem
PPT
BCS Selenium Workshop
PPT
Introduction To Ruby Watir (Web Application Testing In Ruby)
PDF
Cucumber
PPTX
Cross Browser Automation Testing Using Watir
PDF
Integration Test Cucumber + Webrat + Selenium
 
PPT
Cucumber
PPT
No Va Taig April 7 2010
PPTX
BDD / cucumber /Capybara
Page object from the ground up by Joe Beale
Page object from the ground up.ppt
Test Automation using Ruby
What you can do In WatiR
Ruby with cucmber
Introduction to Selenium and Ruby
Week7
Watir Presentation Sumanth Krishna. A
full-stack-webapp-testing-with-selenium-and-rails.pdf
Cucumber
Capybara and cucumber with DSL using ruby
End-to-end web-testing in ruby ecosystem
BCS Selenium Workshop
Introduction To Ruby Watir (Web Application Testing In Ruby)
Cucumber
Cross Browser Automation Testing Using Watir
Integration Test Cucumber + Webrat + Selenium
 
Cucumber
No Va Taig April 7 2010
BDD / cucumber /Capybara

More from QA or the Highway (20)

PDF
KrishnaToolComparisionPPT.pdf
PPTX
Ravi Lakkavalli - World Quality Report.pptx
PPTX
Caleb Crandall - Testing Between the Buckets.pptx
PDF
Thomas Haver - Mobile Testing.pdf
PDF
Thomas Haver - Example Mapping.pdf
PDF
Joe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdf
PDF
Sarah Geisinger - Continious Testing Metrics That Matter.pdf
PDF
Jeff Sing - Quarterly Service Delivery Reviews.pdf
PDF
Leandro Melendez - Chihuahua Load Tests.pdf
PDF
Rick Clymer - Incident Management.pdf
PPTX
Robert Fornal - ChatGPT as a Testing Tool.pptx
PDF
Federico Toledo - Extra-functional testing.pdf
PPTX
Andrew Knight - Managing the Test Data Nightmare.pptx
PDF
Melissa Tondi - Automation We_re Doing it Wrong.pdf
PDF
Jeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdf
PPTX
DesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptx
PDF
Damian Synadinos - Word Smatter.pdf
PDF
Lee Barnes - What Successful Test Automation is.pdf
PPTX
Jordan Powell - API Testing with Cypress.pptx
PPTX
Carlos Kidman - Exploring AI Applications in Testing.pptx
KrishnaToolComparisionPPT.pdf
Ravi Lakkavalli - World Quality Report.pptx
Caleb Crandall - Testing Between the Buckets.pptx
Thomas Haver - Mobile Testing.pdf
Thomas Haver - Example Mapping.pdf
Joe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdf
Sarah Geisinger - Continious Testing Metrics That Matter.pdf
Jeff Sing - Quarterly Service Delivery Reviews.pdf
Leandro Melendez - Chihuahua Load Tests.pdf
Rick Clymer - Incident Management.pdf
Robert Fornal - ChatGPT as a Testing Tool.pptx
Federico Toledo - Extra-functional testing.pdf
Andrew Knight - Managing the Test Data Nightmare.pptx
Melissa Tondi - Automation We_re Doing it Wrong.pdf
Jeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdf
DesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptx
Damian Synadinos - Word Smatter.pdf
Lee Barnes - What Successful Test Automation is.pdf
Jordan Powell - API Testing with Cypress.pptx
Carlos Kidman - Exploring AI Applications in Testing.pptx

Recently uploaded (20)

PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPT
Teaching material agriculture food technology
PDF
Modernizing your data center with Dell and AMD
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Cloud computing and distributed systems.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Teaching material agriculture food technology
Modernizing your data center with Dell and AMD
Dropbox Q2 2025 Financial Results & Investor Presentation
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
MYSQL Presentation for SQL database connectivity
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Cloud computing and distributed systems.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Cucumber From the Ground Up - Joseph Beale

  • 2. Who am I and why am I here? Name: Joe Beale What is essential to know about me:  I am NOT brilliant. What I hope to accomplish today:  Prove that the bullet point above is correct.  Put to death any fear you might have about writing test automation code.
  • 3. Agenda  Installation: 10:30 – 10:45  Environment Set-up: 10:45 – 11:00  Build and Test: 11:00 – 11:15  Q&A + Fun Time: 11:15 – 11:30
  • 5. Installation MAKE SURE YOU CHECK THE THREE BOXES THAT ARE RIGHT UNDER THE FOLDER SELECTION FIELD.
  • 6. Installation Download DevKit from the RubyInstaller page: Extract to C:DevKit
  • 7. Installation At command prompt, check for correct Ruby installation by typing:  ruby –v [Enter]
  • 8. Installation To install DevKit, navigate to C:DevKit and type the following:  ruby dk.rb init [Enter]  ruby dk.rb install [Enter] Go back to the root directory (C:) and install Cucumber by typing:  gem install cucumber [Enter] You now have Ruby and Cucumber fully installed!
  • 9. Environment Using Windows Explorer, create a new folder on your C drive called test_project.
  • 10. Environment Inside of test_project, create a folder called features, then inside of that create two more: step_definitions and support.
  • 11. Environment RubyMine – A Robust IDE for Ruby Coding RubyMine is also very Cucumber-friendly!
  • 12. Environment We will use RubyMine 5.4.3 for today’s demo:
  • 13. Environment Use the Open Directory command to open up our test_project folder.
  • 14. Environment Create a folder called env.rb inside of the support folder using New –> File from the right-click menu.
  • 15. Environment Need two more gems for this demonstration:  watir-webdriver  rspec Q: What is a Ruby gem? A: A pre-built code module that performs some useful function in Ruby, available for free from https://rubygems.org/
  • 16. Environment Go back to your command prompt and install the gems using this command:  gem install <name of gem> [Enter]
  • 17. Environment Require statements tell Ruby that you are going to use some extra code modules. Q: Why env.rb in the support folder? A: Cucumber knows to run env.rb before anything else and looks for it in support.
  • 18. Build and Test Create a feature file and a test scenario. Cucumber scenarios are written in ordinary business language using Gherkin keywords (“Feature:”, “Scenario:”, “Given”, “When”, “Then”, “And”, “But”).
  • 19. Build and Test Create step definitions using RubyMine’s built-in functionality.
  • 20. Build and Test RubyMine creates a new file called my_steps.rb and adds code blocks with regular expressions to match your Gherkin steps.
  • 21. Build and Test First step: Given I am on the Bing home page To satisfy this step, we need to open a browser and navigate to the Bing home page. How do we do this in Ruby?
  • 22. Build and Test Watir Webdriver is a gem that is built to drive web applications. Since the code is already written, we only need to learn how to use it. http://watirwebdriver.com/
  • 23. Build and Test Right on the front page is the code to create a browser object (along with lots of other stuff): We will use these exact lines of code and modify them slightly for our purposes.
  • 24. Build and Test The small ‘b’ in their examples stands for ‘browser’, but when we create an object we will call it ‘browser’ to avoid confusion. The browser object is created in env.rb so that it is executed before any of the steps
  • 25. Build and Test Q: Why did you put a ‘$’ in front of your object name? A: To indicate that it’s a global variable. We want the browser object to be accessible to all steps, so we make it global.
  • 26. Build and Test In the first step definition, we will put the next line of code, but modified to reflect our global browser object and the URL for Bing.com: Now when we run our Cucumber scenario, the first step will give us…
  • 27. Build and Test The Bing home page!
  • 28. Build and Test A quick word about method calls: the format for calling a method in Ruby is (object name) + dot (‘.’) + (method name). So in the method call below, the object name is ‘$browser’ and the method name is ‘goto’.
  • 29. Build and Test The ‘goto’ method is just one of the pre-defined methods inside of the Watir::Browser class. The method takes one argument: a URL (because it needs to know where to “go to”). Arguments can be passed either inside of parentheses or alongside a method call. So in the previous example, we could have said: $browser.goto(‘http://bing.com’)
  • 30. Build and Test Second step: When I search for "Death Star“ To satisfy this step, we need to interact with two of the elements of the page:  The search text field  The search button (indicated by the magnifying glass icon)
  • 31. Build and Test Watir Webdriver has several methods in the Browser class to interact with the elements that are common to most web pages. In fact, the ones we need are also listed on their front page:
  • 32. Build and Test To interact with the two search elements, we will use the ‘text_field’ and ‘button’ methods. These methods each take one argument: a location as indicated by the syntax (:locater => ‘unique_identifier’). The examples given are these: b.text_field(:id => 'entry_0').set 'your name' b.button(:name => 'submit').click
  • 33. Build and Test The locators are part of the html code for the page elements, and this information is acquired by using Firefox’s “Inspect Element” tool. So if you right-click on the search text field and choose “Inspect Element” from the drop-down menu, you will see the specific html code for that element in the shaded section.
  • 34. Build and Test The most common locator to use is ‘id’. So we will copy the value ‘sb_form_q’ to use in our argument to the ‘text_field’ method.
  • 35. Build and Test One nice feature of Ruby method calls is the ability to “chain” your methods one right after another. Ruby will execute them in the order given, from left to right. That is what you see in the the ‘text_field’ example: b.text_field(:id => 'entry_0').set 'your name' The ‘set’ method is executed right after the ‘text_field’ method.
  • 36. Build and Test You can use the ‘set’ method anytime you have defined a text field element using the ‘text_field’ method. It takes one argument, the word or phrase you wish to place in that field. So, putting it all together, the code for our second step will look like this: Q: Where did ‘search_phrase’ come from?
  • 37. Build and Test As you may recall, our original step definition looked like this: I changed the default variable name ‘arg’ to one that reflects what it actually holds: ‘search_phrase’. The capture group with wildcard grabs the phrase “Star Wars” from the step and throws it into the variable.
  • 38. Build and Test Since the variable ‘search_phrase’ contains a string, then it can be used as an argument for the ‘set’ method. And so we are then able to use that same Ruby code block no matter what phrase is used in the Gherkin step. Step  Step Definition
  • 39. Build and Test Running the test now gives us… That’s No Moon!!!
  • 40. Build and Test For the button element, we’ll do it exactly like we did the text field, except instead of the ‘set’ method, we’ll need to find one that will click the button. The method name in this case is – surprise! – ‘click’. Here’s the example from Watir Webdriver’s front page: b.button(:name => 'submit').click
  • 41. Build and Test The locator they are using is ‘name’, but in our case there is no name, just a magnifying glass symbol. So we’ll inspect the element in order to find the ‘id’ for locating it:
  • 42. Build and Test Adding that into the step gives us this: Again, you can see that we “chained” two methods: first the ‘button’ method to locate a specific element and then the ‘click’method. The latter method does not require any arguments, for obvious reasons.
  • 43. Build and Test Now let’s run it again. Well, that’s better!
  • 44. Build and Test Third step: Then the search results will include "Star Wars“ Somehow we have to get our code to examine the result page and check to see if our result phrase “Star Wars” is found anywhere. This is where the Rspec gem comes into play.
  • 45. Build and Test You might recall that in the env.rb file we told Ruby to require that code: RSpec gives us the ability to “match” values and phrases against an expected result.
  • 46. Build and Test For our purposes, the most relevant RSpec method is ‘should’. This method will check a value from our object against another value and either pass or fail the step based on whether or not it matches. Let’s see how this step might be coded. Q: What does the ‘text’ method do?
  • 47. Build and Test Many of the html elements on a web page will have text associated with them, and most of the time it’s displayed on the page. For any given element, you can request the associated text using the ‘text’ method. The code we used, ‘$browser.text’ will give you all the text on the page.
  • 48. Build and Test The ‘should’ method requires a “matcher” like ‘include’ or ‘==‘ in order to compare with the expected result. The ‘include’ matcher mimics the behavior of the Ruby ‘include?’ method that is part of the String class. If the expression matches the expected value, then the test passes!
  • 49. Build and Test Running one more time gives us this result in RubyMine: Congratulations! You have just passed your first Ruby/Cucumber automated test!
  • 50. Q&A + Fun Time You don’t have to be this guy to automate.
  • 51. Q&A + Fun Time In fact, even this guy could do it.
  • 52. Q&A + Fun Time Questions? Comments? Snark? You can reach me at: joseph_beale@att.net or joseph.beale92@gmail.com or Connect with me on Linked In!