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

From Test Automation Wiki
Jump to: navigation, search
Line 3: Line 3:
 
</div>
 
</div>
  
 +
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:
 +
* <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>
 +
Given(/^I navigate to (.*) in (.*)$/) do |site,language|
 +
  #... code here...
 +
end
 +
</source>
 +
 +
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.
 +
<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 <code>example.feature</code>:
 +
<source>
 +
 +
</source>
  
 
[[Category:Web Test Automation Tutorial|6]]
 
[[Category:Web Test Automation Tutorial|6]]

Revision as of 10:21, 27 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: