Contents

Expectations

This is just a way to make role-based expectations read a little beter. We make the role the base of the expectation, and pass a widget selector (widget name and any required arguments for that widget) to it.

Scenario: Seeing widgets through a role

see sees widgets internal and external to the role

  • Given the following HTML:
    <div id="seen-inner">Seen Inner!</div>
    <div id="seen-outer">Seen Outer!</div>
  • And the following widget definition:
    SeenOuter = Capybara::UI::Widget('#seen-outer')
  • And the following role definition:
    ruby
    class Seer < Capybara::UI::Role
      widget :seen_inner, "#seen-inner"
    end
  • Then we should be able to see that the widgets exist:
    ruby
    seer = Seer.new
    
    expect(seer).to see :seen_inner
    expect(seer).to see :seen_outer

Scenario: Passing selector arguments to see

Since you can only find some widgets by passing in additional arguments, see accepts additional arguments as well.

  • Given the following HTML:
    <div class="seen">Seen Outer!</div>
    <div class="seen">Seen Inner!</div>
  • And the following role definition:
    ruby
    class Seer < Capybara::UI::Role
      widget :seen, -> (position) { ['.seen', :text => /#{position}/] }
    end
  • Then we should be able to see that the widgets exist:
    ruby
    seer = Seer.new
    
    expect(seer).to see :seen, 'Inner'

Scenario: Delegating to role.see_*

Sometimes, what we want the role to see may not map directly to a widget. In those situations, we can define a custom see_XXX? method in the role, which will be used by the generic see matcher.

  • Given the following role definition:
    ruby
    class Seer < Capybara::UI::Role
      def see_vapor?
        true
      end
    
      def see_ice?
        false
      end
    end
  • Then we should be able to use the "see_*" method:
    ruby
    seer = Seer.new
    
    expect(seer).to see :vapor
    expect(seer).not_to see :ice

Scenario: Passing arguments to role.see_*

see passes all arguments except the first to the delegate method.

  • Given the following role definition:
    ruby
    class Seer < Capybara::UI::Role
      def see_something?(arg)
        arg == "nice"
      end
    end
  • Then we should be able to use the "see_*" method:
    ruby
    seer = Seer.new
    
    expect(seer).to see :something, "nice"

Scenario: When a widget doesn't exist

  • Given the following role definition:
    ruby
    class Seer < Capybara::UI::Role; end
  • When we try to find the unknown widget:
    ruby
    seer = Seer.new
    
    expect(seer).to see :unknown, "nice"
  • Then we should get a Capybara::UI::Missing error

Source: features/roles/expectations.feature at commit 37acd1d5, Dill (MIT license).