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

From Test Automation Wiki
Jump to: navigation, search
Line 34: Line 34:
 
</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.
 
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.
[[File:run-undefined-steps.png|thumb}300px]]
+
[[File:run-undefined-steps.png|thumb|300px]]
 
<blocknote>
 
<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.

Revision as of 09:29, 28 June 2016

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

Start by opening the following files:

  • /yourproject/features/step_definitions/interactions_steps.rb
  • /yourproject/features/example.feature

As you can see, the step Given I navigate to Google in english is defined in the interaction_steps.rb file as following:

Given(/^I navigate to (.*) in (.*)$/) do |site,language|
  #... code here...
end

When you are 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. <blocknote> 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. </blocknote>

Making a custom step

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

@example02
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"

And run the scenario in the console: cucumber -t @example02. This will result saying that there are 3 undefined steps, with the code to use to define the steps. Copy this code into the interaction_steps.rb file.

Run-undefined-steps.png

<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. </blocknote>

Now we have the step definitions written down, we can start adding code to the steps.

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

When(/^I click on Execute scenarios$/) do
  browser.find('n-5.-Execute-scenario.27s').click
end

Then(/^I should see text "([^"]*)"$/) do |arg1|
  browser.wait(:text => /#{arg1}/)
end

An run the scenario again: cucumber -t @example02

Run-success-steps.png

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