utomation is becoming more and more important in the world of software testing, especially as more development shops move into agile or agile-like methodologies. However, for testers with no development background the idea of learning how to automate can be intimidating.My goal is simple: to demystify the subject by taking a novice tester with no coding experience through the process of writing a simple automated test using using the Cucumber framework. I will take a volunteer from the audience and transform that person from an ordinary QA professional (or whatever their occupation) into an automation engineer in one short hour.
This will be a live demonstration and we will be working without a net. No animals will be harmed during the show, but be prepared to slay your fear of coding once and for all.
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.
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!
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”).
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…
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
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.
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!