Difference between revisions of "Writing code for your test cases"

From Test Automation Wiki
Jump to: navigation, search
 
(7 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
This page will describe how you can start putting actions behind the steps you have written in `[[Writing_gherkin_test_cases_in_cucumber]]`.
 
This page will describe how you can start putting actions behind the steps you have written in `[[Writing_gherkin_test_cases_in_cucumber]]`.
  
== Step definitions ==
+
== Making a custom step ==
  
Start by opening the following files:
+
Let's start by creating a new scenario in <code>1_basic.feature</code>:
* <code>/yourproject/features/step_definitions/interactions_steps.rb</code>
 
* <code>/yourproject/features/example.feature</code>
 
 
 
As you can see, the step <code>Given I navigate to Google in english</code> is defined in the interaction_steps.rb file as following:
 
 
<source>
 
<source>
Given(/^I navigate to (.*) in (.*)$/) do |site,language|
+
  @example1337
  #... code here...
+
  Scenario: example1337 - Going to the Execute scenarios page on TA.info
end
+
    Given the user is on the website testautomation.info
 +
    When the user clicks on "A new scenario" in the side menu
 +
    Then title "Writing code for your test cases" should display
 
</source>
 
</source>
 +
And run the scenario in the console: <code>bundle exec cucumber -t @example1337</code>. This will result saying that there are 3 undefined steps, with the code to use to define the steps.
 +
<source>
 +
You can implement step definitions for undefined steps with these snippets:
  
When you are [[Running_cucumber_projects|executing your scenarios]], with every step you run, the system will search for a definition of that step. If a Defined Step matches the Step in the scenario, it will run the code in the brackets of the step definition.
+
Given(/^the user is on the website testautomation\.info$/) do
<blocknote>
+
  pending # Write code here that turns the phrase above into concrete actions
Actually all definitions are loading before it starts going through the steps, but it's simpler to see it as every step being looked up.
+
end
</blocknote>
 
  
== Making a custom step ==
+
When(/^the user clicks on "([^"]*)" in the side menu$/) do |arg1|
 +
  pending # Write code here that turns the phrase above into concrete actions
 +
end
  
Let's start by creating a new scenario in <code>example.feature</code>:
+
Then(/^title "([^"]*)" should display$/) do |arg1|
<source>
+
   pending # Write code here that turns the phrase above into concrete actions
@example02
+
end
Scenario: example02 - Going to the Execute scenarios page on TA.info
 
   Given I am on the website testautomation.info
 
  When I click on Execute scenarios
 
  Then I should see text "Writing code for your test cases"
 
 
</source>
 
</source>
And run the scenario in the console: <code>cucumber -t @example02</code>. This will result saying that there are 3 undefined steps, with the code to use to define the steps. Copy this code into the <code>interaction_steps.rb</code> file.
+
<blockquote>
[[File:run-undefined-steps.png|thumb|300px]]
 
<blocknote>
 
 
To copy text from console in windows, right click in console and select Mark. Then select the area you want to copy and press Enter. The selected text is now on the clipboard and can be pasted using Ctrl+V.
 
To copy text from console in windows, right click in console and select Mark. Then select the area you want to copy and press Enter. The selected text is now on the clipboard and can be pasted using Ctrl+V.
</blocknote>
+
</blockquote>
  
Now we have the step definitions written down, we can start adding code to the steps.
+
Now we have the step definitions written down, we can start adding code to the steps. First create a new file <code>./features/step_definitions/tutorial_steps.rb</code> and add the following:
 
<source>
 
<source>
Given(/^I am on the website testautomation\.info$/) do
+
Given(/^the user is on the website testautomation\.info$/) do
 
   browser.goto 'http://www.testautomation.info/'
 
   browser.goto 'http://www.testautomation.info/'
 
end
 
end
  
When(/^I click on Execute scenarios$/) do
+
When(/^the user clicks on "(.*?)" in the side menu$/) do |menuitem|
   browser.find('n-5.-Execute-scenario.27s').click
+
   sidebar = browser.wait(:like => [:div, :id, 'mw-panel'])
 +
  browser.find(
 +
          :a => {:text => /#{menuitem}/i},
 +
          :context => sidebar
 +
  ).click
 
end
 
end
  
Then(/^I should see text "([^"]*)"$/) do |arg1|
+
Then(/^title "([^"]*)" should display$/) do |arg1|
   browser.wait(:text => /#{arg1}/)
+
   browser.wait(:like => [:h1, :text, arg1])
 
end
 
end
 
</source>
 
</source>
  
An run the scenario again: <code>cucumber -t @example02</code>
+
Now run the scenario again: <code>bundle exec cucumber -t @example1337</code>
[[File:run-success-steps.png|thumb|200px]]
+
<br />
 +
<source>
 +
1 scenario (1 passed)
 +
3 steps (3 passed)
 +
0m8.114s
 +
</source>
  
 
== Advanced steps & code ==
 
== Advanced steps & code ==
Line 76: Line 81:
 
More info: [[debugging_code_in_test_automation]]
 
More info: [[debugging_code_in_test_automation]]
  
[[Category:Web Test Automation Tutorial|6]]
+
[[Category:Web Test Automation Tutorial|5]]

Latest revision as of 12:44, 20 February 2018

This page will describe how you can start putting actions behind the steps you have written in `Writing_gherkin_test_cases_in_cucumber`.

Making a custom step

Let's start by creating a new scenario in 1_basic.feature:

  @example1337
  Scenario: example1337 - Going to the Execute scenarios page on TA.info
    Given the user is on the website testautomation.info
    When the user clicks on "A new scenario" in the side menu
    Then title "Writing code for your test cases" should display

And run the scenario in the console: bundle exec cucumber -t @example1337. This will result saying that there are 3 undefined steps, with the code to use to define the steps.

You can implement step definitions for undefined steps with these snippets:

Given(/^the user is on the website testautomation\.info$/) do
  pending # Write code here that turns the phrase above into concrete actions
end

When(/^the user clicks on "([^"]*)" in the side menu$/) do |arg1|
  pending # Write code here that turns the phrase above into concrete actions
end

Then(/^title "([^"]*)" should display$/) do |arg1|
  pending # Write code here that turns the phrase above into concrete actions
end

To copy text from console in windows, right click in console and select Mark. Then select the area you want to copy and press Enter. The selected text is now on the clipboard and can be pasted using Ctrl+V.

Now we have the step definitions written down, we can start adding code to the steps. First create a new file ./features/step_definitions/tutorial_steps.rb and add the following:

Given(/^the user is on the website testautomation\.info$/) do
  browser.goto 'http://www.testautomation.info/'
end

When(/^the user clicks on "(.*?)" in the side menu$/) do |menuitem|
  sidebar = browser.wait(:like => [:div, :id, 'mw-panel'])
  browser.find(
           :a => {:text => /#{menuitem}/i},
           :context => sidebar
  ).click
end

Then(/^title "([^"]*)" should display$/) do |arg1|
  browser.wait(:like => [:h1, :text, arg1])
end

Now run the scenario again: bundle exec cucumber -t @example1337

1 scenario (1 passed)
3 steps (3 passed)
0m8.114s

Advanced steps & code

Regular expressions in Step definitions

Step definitions are made using regular expressions, meaning, you can put in code to catch multiple cases. For example, when you write (.*) it mean any character of any length can be here. To learn more about regular expressions, go to http://rubular.com/

Lapis Lazuli

In the example above some simple code is used to change page and to click on an element. The are many many more options to use Lapis Lazuli. Besides finding an element or waiting for an element to become present. You can also select multiple elements and then interact with a random element.

During your time working with test automation you will start using all these different options. You can find the documentation on this same Wiki. See: Lapis_Lazuli:Usage

Debugging

Some scenario's take a long time to execute and something is going wrong at the end of that scenario. Instead of running that scenario over and over again, it is easier to debug the code from the step that is going wrong.

More info: debugging_code_in_test_automation