Running Capybara without rack produces errors when using url parameters
You can set only one of app
and app_host
. So you should remove config.app
from configuration.
Also you should set default_driver
instead of current_driver
.
As it's shown in my answer in linked question working configuration is:
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium
config.app_host = 'https://www.google.com' # change url
end
How to get Cucumber/Capybara/Mechanize to work against external non-rails site
It was meant to be read as:If you are not using Rails, set Capybara.app to your rack app:
Capybara's README was updated as the result of this question.If the application that you are testing is a Rack app, but not Rails, set Capybara.app to your Rack app:
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
capybara-webkit cannot simulate click on (non link/button) element
The problems I had and solutions:
1) Capybara trigger() method isn't supported with selenium-webdriver or capybara-webkit.
- a solution: trigger the jquery event directly from javascript:
page.execute_script(%Q($("#...
although this isn't as useful as it could fire the event on an element that isn't visible to the user.
save_and_open_page
doesn't use the asset pipeline, so is fairly useless if you have hidden clickable elements. The poltergeist driver has save_screenshot, which works, but it's not as useful as having a real browser page with the assets loadeda solution: precompile assets in test, although I couldn't get jQuery to load with any driver ($ is undefined) and poltergiest/phantomjs doesn't allow access to local compiled files (passing command line options to the driver has no effect)
better solution: use unreleased Capybara 2.1+
gem 'capybara', git: 'git://github.com/jnicklas/capybara.git'
, addCapybara.asset_host = "http://localhost:3000"
to spec_helper.rb and leave dev server running
Error undefined method find_xpath for #<Capybara::Webkit::Driver
When using Capybara.javascript_driver = :webkit
- solution: use master branch
gem 'capybara-webkit', git: 'git://github.com/thoughtbot/capybara-webkit.git'
How to use headless chrome with capybara and selenium
1) Make sure you don't have another registered driver, I made this mistake myself and had an iphone
driver, which was using the args
in the old way, that's why I was getting the warning.
2) Make sure you have Chrome version 57+ on Linux, 59+ on macOS or 60+ on Windows;
3) Add/update the gem selenium-webdriver
;
4) Add the following driver to your spec_helper.rb
or rails_helper.rb
:
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new app, browser: :chrome,
options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu])
end
Capybara.javascript_driver = :chrome
Capybara-webkit tries to open example.com
Try using visit products_path
instead. They do not recommend using absolute URLs in "Gotchas" section of README.
Capybara-webkit unexpected behavior with url routes, returns blank page
The problem came from my own mistake. In my controller, I am checking for the subdomain that comes with the request.url
using request.subdomains
. This returns array of subdomains present before the host
in the url
.
In my test, I stub ActionDispatch::Request
to accept :subdomains
method and respond with the provided subdomain. Then, it can proceed with the verification in the controller.
Because there are places where I set the subdomain
attribute in a route url on the fly, I was having different values for request.subdomain
(the new one set in the url) and request.subdomains
(the old one before setting the one in the url). So I decided to go with the new one and just split it with .split('.')
since that is what I need to work with.
After changing that, I forgot to update the stub in the spec to change the request method to subdomain
.
After changing it, everything works fine.
I hope this helps anyone facing this same problem. At least it will remind them to update all stubs they have if any.
[Update]
Apparently, the reason why I was getting different values for the 2 subdomain method was because of the stub in the spec.
save_and_open_page not working with capybara 2.0?
I was able to fix the problem with save_and_open_page after some blog browsing and digging. I have dropped the capybara_screenshot gem from my project. You can see my working code on my github test_capybara_screenshot repository. The solution that I figured out uses some pointers that I found on the capybara github site.
Assumptions:
- You are using rspec for testing.
- Your app is already configured for using the asset pipeline.
spec/spec_helper.rb
file within the RSpec.configure
loop:config.before (scope = :suite) do
%x[bundle exec rake assets:precompile]
end
I specify where the assets are pre-compiled to in the config/environments/test.rb
file:config.assets.prefix = "assets_test" # place test assets in public/assets_test directory
config.action_controller.asset_host = "file://#{::Rails.root}/public"
This makes it so that the test assets are independent of the development assets, and are only generated during a test suite run.If you do this, you will probably want to have git ignore the /public/assets*
directories.
Related Topics
How to Reverse a Linked List in Ruby
Rails: What's Wrong with This Multiple Join with Conditions on The Associations
Running Rails Console with Bundle Exec
Ruby Linkify for Urls in Strings
Cucumber Embed for Screenshots Not Linking to Screenshot
Git: Forcing Tests Before Pushing to Local or Remote Master
Ruby: Parse Excel 95-2003 Files
How to Use (Ruby) Rgeo to Transform (Unproject) Coordinates
Inspect or Clean Up The Working Tree Error When Installing Ruby 2.1.3 on MAC Os X 10.9.5
How to Handle Single Table Inheritance in Simpleform So a Single Helper Handles All Models
I Need to Go from HTML to Markdown, Any Suggestion
How to Install Wraith on Windows 7
Filtering a List Using Checkboxes
Rails + Mongoid - Don't Return Nil Values in JSON
How to Use Rspec to Test That a Model Using Paperclip Is Validating The Size of an Uploaded File
Case Insensitive Search in Rails