When using capybara-webkit I have come across the issue of selenium not having an error_messages method, although, the documentation for capybara-webkit says that I should be able to access it through page.driver.error_messages once my flag on the scenerio is set with :js => true. However, trying to access it, it throws:
NoMethodError:
undefined method `error_messages' for #<Capybara::Selenium::Driver:0xc501fc0>
and I have tried to access the messages using:
page.should_not have page.driver.error_messages
I have also tried:
page.should_not :have_errors
as well as:
page.should_not have_errors
but with not avail.
My understanding is that error_messages is a method included in the capybara-webkit driver. It is not available for the selenium-webdriver driver.
Based on the exception you are getting, you are using the selenium-webdriver. By default, Capybara uses selenium-webdriver when :js => true is specified.
You likely want to change your javascript_driver to use :webkit (as mentioned in the usage documentation - https://github.com/thoughtbot/capybara-webkit).
Capybara.javascript_driver = :webkit
Related
I need to get the operating system info to handle OS related tasks. I tried to get it via page.driver.browser but it doesn't return os info. It could be in header but there is no function header.
How can I get OS information by page object?
irb#1(main):016:0> page.driver
=> #<Capybara::Selenium::Driver:0x007f84a35c0858 #app=nil, #browser=#<Selenium::WebDriver::Driver:0x1a664c8c1eb09ac0 browser=:firefox>, #exit_status=nil, #frame_handles={}, #options={:browser=>:firefox}>
irb#1(main):017:0>
irb#1(main):018:0* page.driver.browser
=> #<Selenium::WebDriver::Driver:0x1a664c8c1eb09ac0 browser=:firefox>
irb#1(main):019:0>
irb#1(main):020:0* page.driver.browser.header
NoMethodError: undefined method `header' for #<Selenium::WebDriver::Driver:0x007f84a34cb9e8>
Using selenium you should be able to look at
page.driver.browser.capabilities.platform
That will only work for the selenium driver though, not the other available capybara drivers
In your ruby console you can run
Config::CONFIG["host_os"]
Or
RUBY_PLATFORM
OUTPUT:
2.0.0-p598 :001 > Config::CONFIG["host_os"]
(irb):1:in `irb_binding': Use RbConfig instead of obsolete and deprecated Config.
(irb):1:in `irb_binding': Use RbConfig instead of obsolete and deprecated Config.
=> "linux-gnu"
2.0.0-p598 :001 > RUBY_PLATFORM
=> "x86_64-linux"
Hope it helps!
I have an RSpec integration test that needs to execute some JavaScript. I've included Poltergeist and installed PhantomJS, but whenever I run the example, I get this error:
Failure/Error: page.execute_script("$('form')[0].submit();")
Capybara::NotSupportedByDriverError:
Capybara::Driver::Base#execute_script
The spec is:
require 'spec_helper'
describe "Signup", :type => :feature do
describe "workflow" do
it "ensures entry of contact information" do
visit 'signup/action'
# snip - use Capybara to fill out form elements
page.execute_script("$('form')[0].submit();")
page.should have_content("Name can't be blank")
page.should have_content("Email can't be blank")
# snip - use Capybara to fill out more form elements
page.execute_script("$('form')[0].submit();")
page.should have_content("Next page")
end
end
end
I think the problem is that I'm not sure how to indicate that Capybara should use Poltergeist as its JavaScript driver. The Poltergeist documentation says:
Installation
Add poltergeist to your Gemfile, and in your test setup add:
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
But it doesn't say which file specifically it should go into. It also says:
Customization
You can customize the way that Capybara sets up Poltegeist via the following code in your test setup:
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
But it's not clear to me if or when I would need to include this in my tests. And again, I'm not sure where to put it if I need to.
What am I missing?
Where do I need to put the configuration for Capybara and Poltergiest, and what exactly does it need to say (or how can I determine that for myself)?
Is there a step or piece of configuration that I missed?
Try putting js: true in your describe line. I know i had to do that for feature specs on an app at work:
describe "Signup", :type => :feature, :js => true do
I don't see any other configuration for it. Was a while ago when I set it up :)
You can just call the Capybara driver config methods once before your RSpec.configure block:
Capybara.default_selector = :css
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :window_size => [1920, 1080], :phantomjs_logger => nil)
end
Capybara.javascript_driver = :poltergeist
RSpec.configure do |config|
Also be sure to use truncation not transaction with database cleaner. Poltergeist runs on a separate thread, so you'll likely have weird db issues if you use transactional.
Edit
Ah the js true thing is mentioned under here: https://github.com/jnicklas/capybara#using-capybara-with-rspec in the capybara readme.
RSpec2 does not include an have_tag test helper. Using webrat's have_tag or have_selector matchers instead is not possible because Webrat and Rails 3 are not compatible yet. Is there a way to write useful RSpec view tests? It is possible to use assert_select instead of have_tag, but then one could Test::Unit tests in the first place. Or is it no longer recommendable to write RSpec view tests, because integration tests with Capybara or Cucumber are better?
Actually, Webrat works with Rails 3. I have tested this and I was able to use the have_selector matcher (have_tag didn't work).
You can take a look at this Google group discussion. Basically, you don't need the Webrat.configure block mentioned in the webrat readme, and following the mailing list solution, add these lines in your spec_helper.rb:
include Webrat::Methods
include Webrat::Matchers
As you can see, Webrat is not so updated anymore, so yes, you might be better off with integration testing with Cucumber (+ Capybara).
Webrat caused too much trouble, it is also possible to use Capybara with RSpec. The Capybara DSL (with the functions has_selector?, has_content?, etc.) is available for the following RSpec tests: spec/requests, spec/acceptance, or spec/integration.
If you use the latest version of Capybara (~> 1.0.1) - older versions like 0.4.0 won't support this - and add the following lines to your spec_helper.rb file
require "capybara/rspec"
require "capybara/rails"
then you could write for example the following RSpec request test
require 'spec_helper'
describe "Posts" do
describe "GET /blog" do
it "should get blog posts" do
get blog_path
response.status.should be(200)
response.body.should have_selector "div#blog_header"
response.body.should have_selector "div#blog_posts"
end
end
end
I'm getting the following failure during RSpec tests..
no such file to load -- action_controller/integration
..using Rails 3, RSpec 2 and Webrat, if I include the Webrat helpers in the following way (the idea was to use Webrat for the have_tag and have_selector methods instead of assert_select).
RSpec.configure do |config|
..
config.include Webrat::HaveTagMatcher
end
Yet apparently Webrat and Rails 3 are not compatible yet. One solution is to avoid the Webrat gem and to use assert_select instead. Has anyone a better solution? How do you avoid the error?
I have not found a solution for this problem, only a work around. You can use the have_selector method of Capybara instead of Webrat, at least in RSpec request tests. There are certain difficulties with RSpec2 and Capybara (page.should have_selector only works if you use Capybara's 'visit' method, and not the RSpec method get '/some/path'), but basically it works.
I am getting an undefined method 'has_content?' error on an rspec controller_spec file.
I found a thread with similar issues though that thread said the issue was fixed in rspec2.0beta (it was a fairly old thread) but I'm getting this with a more recent version. Some threads on rspec shows that capybara doesn't work in view specs, but I'm working in the controller specs so that shouldn't be the issue...
My Gemfile info looks like this:
rspec-rails+ dependecies 2.6.0.rc6
capybara 0.4.1.2
rails 3.0.7
I am trying to do a simple assert like
response.body.should have_content("Project A")
Thanks for the response,
Tony
Capybara is only included in Rspec request specs by default. Change this file to a be a request spec (put it in the request specs directory, change the title of it...)
Read the capybara Readme section 'Using Capybara with RSpec'
https://github.com/jnicklas/capybara
Also, if these are the types of asserts you're looking to do, this qualifies as a request spec more than it is a controller spec.