Cucumber + testing JS alert

前端 未结 5 552
失恋的感觉
失恋的感觉 2021-02-04 07:05

I\'m trying to test a JS confirmation dialog with Cucumber on Rails. I have a window.onbeforeunload event handler that will prompt you with a confirmation dialog if you try to n

相关标签:
5条回答
  • 2021-02-04 07:20

    I would recommend using screw-unit for testing javascript behavior on a page. You can also take a look at Relevance's blue-ridge plugin which incorporates screw-unit and adds support for command line and in browser js testing. You can find it on github under relevance/blue-ridge. (I don't have the rep yet to post more than one link :(

    It would be an interesting exercise to use screw-unit and/or blue-ridge to drive cucumber tests, and probably not that hard to pull off.

    0 讨论(0)
  • 2021-02-04 07:25

    See the method definitions in http://selenium-client.rubyforge.org/classes/Selenium/Client/Idiomatic.html

    You can invoke them with the selenium helper object in your Cucumber step definitions -- e.g.,

    Then /^I should see a JS confirm dialog saying "([^\"]*)"$/ do |statement|    
      selenium.confirmation.should eql(statement)                               
    end
    
    0 讨论(0)
  • 2021-02-04 07:34

    You can use Webrat or Selenium with Cucumber to test this.

    My guess is that you want Simulated Browser or Automated Browser testing,

    You can use Webrat or Webrat::Selenium or simply Selenium with Cucumber in such cases.

    I have tested this using Selenium and Cucumber before, but can't seem to find the code, will edit the post it if i do.

    HTH

    0 讨论(0)
  • 2021-02-04 07:36

    This gist has steps to test a JS confirm dialog in Rails 2 and 3 with any Capybara driver, should be easy to adapt to an alert box.

    0 讨论(0)
  • 2021-02-04 07:40

    There are various functions of selenium you can use to capture alerts/confirms. They are not directly available with the webrat selenium implementation, but when using webrat's config.mode = :selenium they can be used as follows:

    Then /^I should see a JS alert$/ do
        selenium.is_alert_present.should be_true
    end
    
    # or
    
    Then /^I should see a "Are you sure?" JS confirm dialog$/ do
        selenium.get_alert.should eql("Are you sure?")
    end
    
    # you can also click the OK/Cancel buttons on confirm boxes with
    
    selenium.chooseOkOnNextConfirmation();
    #and
    selenium.chooseCancelOnNextConfirmation();
    

    There are probably not the greatest tests, but gives you an idea. Internally selenium overrides the alert() and confirm() functions of JS so it can captures this information.

    You can find more docs on the selenium faq or on your gem server

    0 讨论(0)
提交回复
热议问题