Capybara integration with page-objects gem - ruby-on-rails

Is it possible to use the page-object gem and capybara to automate Ruby on Rails testing? When I attempt to access a page object after starting up my test I receive the following error:
Unable to pick a platform for the provided browser (RuntimeError)
This makes sense as I think I'd have to pass the browser instance of capybara to the page-object, not sure if anyone else has tried this before.

There is a gem that lets you use the Page Object pattern with capybara: SitePrism. Find it here: https://github.com/natritmeyer/site_prism

Currently there is no support for capybara but it has been requested a few times. Part of the challenge is that the API was built upon selenium and watir which are much richer than capy and in it would be a very large undertaking to build add that functionality to the page-object gem to make capy behave the same way.
I've thought several times about splitting the page-object gem into a "basic" form that will allow capybara to utilize and an "advanced" form that will add the additional capabilities found in selenium and watir but haven't seen enough demand.

https://github.com/andyw8/capybara-page-object looks like it started existing in the last 8 months or so.

Related

jQuery document.ready not called during rails integration test

I have an integration test that fails for a page that depends heavily on javacript. The same page runs just fine from the browser.
Simplifying the test to a bare minimum I found out that just testing for the presence of a selector that is added by javascript after the page load, would fail.
After precompiling the test assets and using save_and_open_page I found that the handler for the jQuery ready event is not running during the integration test.
I didn't find any references to this problem, so I guess I'm doing something wrong.
Can anyone help me figuring this out?
I'm using using rails 3.2.11, RSpec 2.13.0, Capybara 2.0.3 and capybara-webkit 0.14.2
By default Rails uses webdriver which doesn't handle JS. If you want JS support you'd need to use one of the JS-aware drivers, like Selenium (which runs full featured browser), capybara-webkit or poltergeist (which are headless webkit browsers). There are many others but these three are most popular.
I solved this problem for me by examining my AJAX request. The problem was that I was requesting a 'http://..." instead of '/' of my page. This meant that it raised a CORS ('ajax request from outside the domain') even though the request looked like it was coming from inside the domain.
My original symptom was 'javascript-inserted HTML elements are not on the page when testing using Capybara with Poltergeist or Selenium (but are on the page when running the application).'

How to make cucumber-rails use watir-webdriver instead of Capybara

I have been searching for a simple answer to this for a few days and can't seem to find an one, on either stackoverflow.com or google. So, firstly, if this is answered elsewhere please point me in the correct direction.
So, I have a relatively new Rails 3 project using the cucumber-rails gem. I am relatively new to Rails but have experience in Ruby and other web frameworks such as Sinatra and Ramaze. My understanding is that out of the box, cucumber-rails uses Capybara for interacting with the web application and somehow does this without starting the rails server. It somehow interacts with rack to simulate the requests. Not sure if I have that entirely correct but I'm pretty sure it does not need a running rails server.
I am not a great fan of the Capybara DSL and I much prefer watir-webdriver which I have used in a few non rails projects (and non Ruby projects even). However, I have not been able to find anywhere to show me how to swap out Capybara for watir-webdriver.
So my questions are these:
Does the cucumber-rails gem have the ability to swap Capybara out for watir-webdriver?
If so, what is the best way to do this?
If not, does that mean I need to ditch cucumber-rails and just set cucumber with watir-webdriver up in my project manually?
Well, it doesn't look hopeful from the project description:
Rails Generators for Cucumber with special support for Capybara and DatabaseCleaner
And sure enough, it explicitly, unconditionally, loads Capybara here
I think that means that you're going to need to hook it up yourself.
Having said that, I don't think there's anything stopping you from installing the gem, running rails g cucumber:install, and then replacing the Capybara-specific bits in the files it produces.

Load Rails 3 test fixtures into development database

I have a nice set of fixtures that I use for testing. When a request test fails it is handy to run the app interactively with the test fixtures loaded. But if I run the full server with -e test I get all kinds of crazy errors, probably having to do with the pipeline.
-- edit --
When I visit the home page I get this in the browser:
I say it's crazy, because I don't understand it :) In particular, I am using twitter-bootstrap and I am unclear about sass/scss/css work with Twitter Bootstrap + Rails so it's highly likely that I have some detail wrong in how I am using them together (or that the gem twitter-bootstrap-rails has some bug.)
Ah, much better question :-).
The twitter-bootstrap-rails gem uses LESS, which is similar to SASS in what it does, but not the same. SASS is the default for Rails, and a good choice. If you want to use SASS, (and based on the error, it appears you are), then use the Bootstrap SASS gem instead, linked here. It may be possible to use both LESS and SASS together, but that seems like a Bad Idea to me.
Check out Ryan Bates' excellent RailsCast which covers using Twitter Bootstrap in Rails. It covers both alternatives. And then fork over the $9 to get Pro access to his continued RailsCast with more details.

how to test dynamic parts in a view (dhtml) with rails cucumber, rspec, capybara?

I want both, testing Ajax Content and normal toggled content through Javascript.
My Setup is:
- Rails 3.1
- Rspec (Edge)
- Cucumber (Edge)
- Capybara (Edge)
For expample, I want to have a Form, which shows particular fields only, if a certain type of this model is selected:
Article can be an external article (url)
or an internal.
Type "externa_url" should show 2 input fields and 2 checkboxes more then Type "article",
which has a body textarea instead.
Whats the best way to implement this, also with testing?
Should it be Server sided, so that partials are loaded, if a certain article type is selected,
or with javascript, toggle the needed html?
Actually inspired by Fransico (in the comments), I write down my knowledge.
I write my own answer, it may helps others too....
First I want to mention, I answer my question with – do integration testing only.
With cucumber and selenium.
And specific javascript testing with jasmine.
But, when integration testing with cucumber (edge) rails (3.1), capybara and selenium you have to be aware of some things:
Look, that you have all your gems updated!
1) Activate your driver, if you haven't already
features/support/capybara.rb
Capybara.javascript_driver = :selenium
2) At the moment only Firefox <=4 works with webdriver selenium for rails, as I found out hardly after hours of configuring and installing each component from scratch, like rack etc.
3) Capybara itself not handles much, which serves you for klicking e.g. on lists, jquery-tokeninput especially.
3.1) I use this for selecting an Item from the tokens in the list:
When(/^I select the option containing "([^\"]*)" in the Tag List/) do |text|
find("li:contains('#{text}')").click
end
You may find this method with "locate" instead of find, don't try this, api / driver has changed to find.
Find waits automatically and check for a Ajax respond in addition of dom finding elements.
4) Add your own helper / finder / click routines for your JS / Ajax responded code,
keep in mind, it is "only" an integration test, you may want to test your JS code with yasmine or another js test framework.
For furthter information also check Screencasts from Ryan Bates (http://railscasts.com), he covers several Topics on Testing Rails; check the latest one for Javascript e.g.
Or this blog: http://openmonkey.com/2010/04/09/javascript-testing-with-cucumber-capybara/
(thnx francisco)
hope this helps someone else as well.

Proper way of testing gems

If a gem has rails dependencies, do you think it is better to write the gem tests in a way they can be run standalone or run them under a rails project?
A gem should be a piece of code which acts stand-alone. Otherwise it is part of the application, so the tests should be created stand-alone as well. In this way others (hypothetically) can perform the tests as well. If the tests depend on your application others cannot test your gem.
Furthermore when you want to test your gem it should not fail because your application is failing. After your gem passed the test, you can test the application knowing that your gem is functioning well (assuming that you tested everything).
Whether the gem is depending on Rails or not is not an issue, since Rails also has been tested (and you can assume it is working properly). So these dependencies do not (/should) influence your gem.
I'd say it depends on the kind of dependencies the gem needs. Eg. if it's just the ActiveRecord it's quite easy to include it into your test suite. In more complex cases you can always mock some of the needed functionality. In a really complex cases, creating a test app is better than nothing (IMO).

Resources