Useful Cucumber steps for other Capybara/Selenium tests

[2011-02-03]

To verify the content of a new page, opened with target blank:

Then /^I should see "([^\"]*)" within a new window$/ do |text|
  within_window(page.driver.browser.window_handles.last) do
    if defined?(RSpec::Matchers)
      page.should have_content(text)
    else
      assert page.has_content?(text)
    end
  end
end

To verify the content of a popup:

Then /^I should see "([^\"]*)" within popup "([^\"]*)"$/ do |text, popup_handle|
  within_window(popup_handle) do
    if defined?(RSpec::Matchers)
      page.should have_content(text)
    else
      assert page.has_content?(text)
    end
  end
end

To fill a CK editor with Capybara and Selenium:

When /^I fill the CKeditor enhanced "([^\"]*)" with "([^\"]*)"$/ do |name, value|
  js = "CKEDITOR.instances.#{name}.setData('#{value}');"
  page.execute_script(js)
end

To fill an FCK editor with Capybara and Selenium:

When /^I fill in the FCK enhanced "([^\"]*)" with "([^\"]*)"$/ do |input_id, value|
  if Capybara::Driver::Selenium === page.driver
    browser = page.driver.browser
    browser.switch_to.frame("#{input_id}___Frame")
    browser.switch_to.frame(0)
    editor = page.find(:css, 'body').native
    editor.send_keys(value)
    browser.switch_to.default_content
  end
end

To confirm a JS confirm dialog box with Capybara and Selenium:

When /^I confirm a js popup on the next step$/ do
  evaluate_script("window['confirm'] = function() { return true; }")
end

 

 

Comments

_

Want to leave a msg?

_

 

_

Paul van der Haas@**.***.***.*$
[2012-02-14 12:58:02]
Text that should not be found:
Hey Jarra,

T is een tijd geleden sinds SDU. Alles goed daar?

Ik kwam toevallig op je website na een zoekticht voor Cucumber (met Capybara).

Ik weet hoe ik moet controleren of er een tekst is, maar ik wil nu controleren dat een tekst niet aanwezig is. De negatieve variant dus. Ik krijg dit alleen niet werkend.

Heb jij een idee?

Ik heb geprobeerd: page.should have_no_content ""
page.has_no_content ""

Ik hoor graag van je.

Groet,

Paul van der Haas
Qualityhouse
(END)

~
`
_

Jarra@**.***.***.*#
[2012-02-14 13:28:06]
should_not:
page.should_not have_content
(END)

~
`