public
Created

  • Download Gist
example.feature
Cucumber
1 2 3 4 5 6
@javascript
Scenario: confiming when saving inactive
Given I expect to click "OK" on a confirmation box saying "Are you sure?"
When I press "Save"
Then the confirmation box should have been displayed
And I should see "TV" in the "Campaign Keywords" section
steps.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Given /^I expect to click "([^"]*)" on a confirmation box saying "([^"]*)"$/ do |option, message|
retval = (option == "OK") ? "true" : "false"
 
page.evaluate_script("window.confirm = function (msg) {
$.cookie('confirm_message', msg)
return #{retval}
}")
 
@expected_message = message
end
 
Then /^the confirmation box should have been displayed$/ do
page.evaluate_script("$.cookie('confirm_message')").should_not be_nil
page.evaluate_script("$.cookie('confirm_message')").should eq(@expected_message)
page.evaluate_script("$.cookie('confirm_message', null)")
end

Nice! I had to change line 5 to:

$.cookie('confirm_message', msg, {path: '/'})

Otherwise the cookie couldn't be read by a script on the home page when it was set by /articles/new (for instance).

Good to know. Thanks!

Hi,
Similar to above my scenario is as follows:
Scenario: When i click update button An alert box appears which contains "OK" and "Cancel" buttons.
Then Click on "Ok" then new form appears.

I am writing request specs i.e. using rspec and capybara
Following is my code :
context "update" do
before(:all) do
Capybara.current_driver = :selenium
end
after(:all) do
Capybara.use_default_driver
end
it "update user to trainer" do
click_button('Upgrade') #Here after clicking on the "update" button alert box will appear
(retval = "Ok") ? "true" : "false"
page.evaluate_script("window.confirm = function (msg) {
$.cookie('confirm_message', msg)
return #{retval}
}")
p retval #Here i am getting value as "OK"
save_and_open_page
end

Now here no error is displayed but the expected page is also not displayed.I do not know whether actual it is hitting OK button or not.
On the last line i.e. save_and_open_page,blank page appears.
Please let me know if i am missing something

Thanks,
Amit Kulkarni

you have (retval = "Ok") rather than (retval == "Ok") which is likely the problem

Thanks a lot.Will try this out and let you know.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.