Overview
Scenario: Defining a role
You declare a role by inheriting from Capybara::UI::Role:
- Given the following role definition:
ruby
class Gardener < Capybara::UI::Role; end - Then we can use the role as we do any other Ruby object:
ruby
Gardener.new
Scenario: Roles group actions
We said before that roles group actions. Usually those actions involve the following:
- Visiting a certain path (or more). Use
visitand pass it a path, just like with Capybara. You can use Rails path helpers inside role actions. - Interacting with a widget (or more). These widgets can be defined inside the role, or outside (below).
- Given the following HTML at the path "
/garden":<a id="plants" onclick="this.innerHTML = 'Watered!'">Water plants</div> - And the following widget definition:
WaterPlants = Capybara::UI::Widget('#plants') - And the following role definition:
ruby
class Gardener < Capybara::UI::Role def water_plants visit garden_path click :water_plants end end - When we ask the role to execute the action:
gardener = Gardener.new gardener.water_plants - Then we should see the role did so:
widget(:water_plants).text #=> 'Watered!'
Source: features/roles/overview.feature at commit 37acd1d5, Dill (MIT license).