Module: Coulda::WebSteps

Defined in:
lib/coulda_web_steps.rb

Instance Method Summary collapse

Instance Method Details

#given_a(factory_name, args = {}) ⇒ Object

Creates an object using a factory_girl factory and creates a Given step that reads like “Given a gi_joe with a name of ‘Blowtorch’ and habit of ‘swearing’”

Parameters:

  • factory_name

    A Symbol or String for the name of the factory_girl factory to use

  • args (defaults to: {})

    Arguments to supply to the factory_name factory_girl factory



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/coulda_web_steps.rb', line 10

def given_a(factory_name, args = {})
  Given "a #{factory_name} #{humanize args}" do
    args.each do |key, value|
      if value.is_a? Symbol
        instance_var_named_value = instance_variable_get("@#{value}")
        args[key] = instance_var_named_value if instance_var_named_value
      end
    end
    model = Factory(factory_name.to_sym, args)
    instance_variable_set("@#{factory_name}", model)
  end
end

#save_and_open_pageObject



138
139
140
141
142
# File 'lib/coulda_web_steps.rb', line 138

def save_and_open_page
  And "I display the current page" do
    save_and_open_page
  end
end

#then_i_should_be_on(path, *args) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/coulda_web_steps.rb', line 94

def then_i_should_be_on(path, *args)
  humanized_path = path.to_s.gsub /_/, " " 
  Then "I should be on #{humanized_path}" do
    instance_var_args = args.inject([]) do |new_args, arg|
      new_args << instance_variable_get("@#{arg}")
    end
    expected_path = __send__(path, *instance_var_args)
    assert current_path == expected_path, "I am on '#{current_path}' but I should be on '#{expected_path}'"
  end
end

#then_i_should_have_a_json_responseObject

Asserts that the response contains JSON, parses it into a hash, stores it in *@json*, and creates a “Then I should get a JSON response” step



58
59
60
61
62
63
# File 'lib/coulda_web_steps.rb', line 58

def then_i_should_have_a_json_response
  Then "I should get a JSON response" do
    assert_equal "application/json", @response.content_type
    @json = JSON.parse(@response.body)
  end
end

#then_i_should_not_be_on(path, *args) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/coulda_web_steps.rb', line 84

def then_i_should_not_be_on(path, *args)
  humanized_path = path.to_s.gsub /_/, " " 
  Then "I should not be on #{humanized_path}" do
    instance_var_args = args.inject([]) do |new_args, arg|
      new_args << instance_variable_get("@#{arg}")
    end
    assert current_path != __send__(path, *instance_var_args), "I am on '#{current_path}' but I shouldn't be"
  end
end

#then_i_should_not_see(content) ⇒ Object

Asserts that the current page does not have content and a Then step that reads like “Then I should not see ‘Who’d like a body massage?‘”

Parameters:

  • content

    The content to check for.



50
51
52
53
54
# File 'lib/coulda_web_steps.rb', line 50

def then_i_should_not_see(content)
  Then "I should not see '#{content}'" do
    assert !page.has_content?(content), "Page has \"#{content}\""
  end
end

#then_i_should_receive_a(status) ⇒ Object

Asserts the HTTP status code to be status and creates a step like “Then I should receive a 200 response”

Parameters:

  • status

    The expected HTTP status code as a Fixnum



68
69
70
71
72
# File 'lib/coulda_web_steps.rb', line 68

def then_i_should_receive_a(status)
  Then "I should receive a #{status} response" do
    assert_equal status, @response.status
  end
end

#then_i_should_see(content) ⇒ Object

Asserts that the current page has content and a Then step that reads like “Then I should see ‘You’re not cookin!‘”

Parameters:

  • content

    The content to check for.



41
42
43
44
45
# File 'lib/coulda_web_steps.rb', line 41

def then_i_should_see(content)
  Then "I should see '#{content}'" do
    assert page.has_content?(content), "Page doesn't have \"#{content}\""
  end
end

#then_i_should_see_flash(level, message) ⇒ Object

Asserts that the a flash is on the page, that it has a class of level and the expected message. and creates a step like “Then I should see the flash error ‘PORKCHOP SANDWICHES’”

Parameters:

  • level

    The expected class of the flash on the page: “notice” or “error”

  • message

    The expected message in the flash



78
79
80
81
82
# File 'lib/coulda_web_steps.rb', line 78

def then_i_should_see_flash(level, message)
  Then "I should see the flash #{level} '#{message}'" do
    assert find("#flash").find(".#{level}").has_content? message
  end
end

#then_page_should_have_selector(selector) ⇒ Object



23
24
25
26
27
28
# File 'lib/coulda_web_steps.rb', line 23

def then_page_should_have_selector(selector)
  Then "page should have selector '#{selector}'" do
    assert page.has_selector?(selector),
      "Page is missing expected selector '#{selector}'"
  end
end

#then_page_should_not_have_selector(selector) ⇒ Object



30
31
32
33
34
35
# File 'lib/coulda_web_steps.rb', line 30

def then_page_should_not_have_selector(selector)
  Then "page should have selector '#{selector}'" do
    assert !page.has_selector?(selector),
      "Page has selector '#{selector}' that should not be present"
  end
end

#when_i_click_button(button) ⇒ Object

Clicks the button specified by button and generates a step like “When I click the button ‘What are you doing?!’”

Parameters:

  • button

    The name of the button to click



132
133
134
135
136
# File 'lib/coulda_web_steps.rb', line 132

def when_i_click_button(button)
  When "I click the button '#{button}'" do
    click_button button.to_s
  end
end

Clicks the link specified by link and generates a step like “When I click the link ‘Get the f**k out!’”

Parameters:

  • link

    The name of the link to click



123
124
125
126
127
# File 'lib/coulda_web_steps.rb', line 123

def when_i_click_link(link)
  When "I click the link '#{link}'" do
    click_link link.to_s
  end
end

#when_i_visit(path, *args) ⇒ Object

Visits the page specified by path and creates a step like “When I visit the pork chop sandwich kitchen” (see ##given_a method)

Parameters:

  • path

    A Symbol giving the name of the helper to invoke to generate a path

  • args

    Symbols representing the names of member variables set in previous steps



110
111
112
113
114
115
116
117
118
# File 'lib/coulda_web_steps.rb', line 110

def when_i_visit(path, *args)
  humanized_path = path.to_s.gsub /_/, " " 
  When "I visit the #{humanized_path}" do
    instance_var_args = args.inject([]) do |new_args, arg|
      new_args << instance_variable_get("@#{arg}")
    end
    visit __send__(path, *instance_var_args)
  end
end