How to get Cucumber/Capybara/Mechanize to work aga

2019-02-05 03:07发布

问题:

I'm trying to do BDD on a Google App Script. I understand that in principle I should be able to use some combination of Cucumber, Capybara and Mechanize to do BDD on a non-rails external site. In this case I am trying to test a Google App Script I created.

I've got the complete code so far in this project:

https://github.com/tansaku/GoogleAppScriptBDD

However I am currently stuck on this error:

rack-test requires a rack application, but none was given (ArgumentError)

I know that I don't want to use rack, and I have been searching forums and stack overflow, and so far my best guess something like this in my cucumber env file:

require 'capybara/cucumber'
Capybara.run_server=false
Capybara.current_driver = :mechanize 
Capybara.app_host = 'https://script.google.com/macros/s/AKfycbytA2xBsaQ0_FSJXNkPVXQekBnWD4hXPOaCjCT00wo/dev' 

This SO post is relevant:

How to use Cucumber to test non-Ruby, non-Rack API's

But I would like to use Capybara since I am testing an HTML interface. Another alternative is described here:

http://blogs.kent.ac.uk/webdev/2012/08/02/using-capybara-webkit-with-cucumber-without-rails-or-rack/

and I've been following the discussion here:

https://groups.google.com/group/cukes/browse_thread/thread/297163800eaf2968

However I'd really like to understand how to just turn off the Rack thing, and I was hoping to use mechanize rather than the webkit described in the blog post mentioned above, since I understand mechanize better at the moment.

Anyhow, is there a best practice here, or do I just keep experimenting with alternatives?

What's kind of maddening is that the Capybara instructions here: https://github.com/jnicklas/capybara say that:

If you are not using Rails, set Capybara.app to your rack app:

Capybara.app = MyRackApp

but that generates a different error "uninitialized constant Object::MyRackApp (NameError) " and I'm not sure what MyRackApp is supposed to be, or where to find out (a general issue I have working with ruby :-/ ) should I be working through the cucumber or capybara source to find out what a MyRackApp is?

Anyway, thought I'd document all this here - any suggestions very much appreciated. I'd love to be able to BDD my google app scripts ...

回答1:

If you are not using Rails, set Capybara.app to your rack app:

It was meant to be read as:

If the application that you are testing is a Rack app, but not Rails, set Capybara.app to your Rack app:

Capybara's README was updated as the result of this question.

As you want to run tests against external application, you should set Capybara.app_host instead of Capybara.app.


I haven't used capybara-mechanize but I think it may be not the best driver to use to test external non-Rack application. Mechanize inherits from Racktest and Racktest is for testing apps with Rack interface (mostly Rails). If your app doesn't have Rack interface, then capybara-mechanize may be not the best choice.

I recommend you to use the built-in selenium, poltergeist, capybara-webkit or terminus

Also your code can be written a bit nicer using Capybara.configure:

Capybara.configure do |config|
  config.run_server = false
  config.default_driver = :selenium
  config.app_host = 'https://www.google.com' # change url
end


回答2:

From Jeroen van Dijk on this Google group message, just set your Capybara.app to something that evaluates to true. i.e.

Capybara.app = "make sure this isn't nil"

As long as Capybara.run_server is set to false, it will not ever try to boot the app. It is working thus far for me.