Contents

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

@javascript

We said before that roles group actions. Usually those actions involve the following:

  1. Visiting a certain path (or more). Use visit and pass it a path, just like with Capybara. You can use Rails path helpers inside role actions.
  2. 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).