21
votes

I am testing with RSpec + Capybara + Selenium (Firefox). No matter what subset of my acceptance tests I run, the first one fails (next tests do work correctly) with "reason" like this:

 Failure/Error: visit '/'
 Timeout::Error:
   Timeout::Error

My application relies on GoogleMaps and BackboneJS heavily. When I run tests, page does not finish to load and "transferring data from maps.googleapis.com" message stays in bottom-left of Firefox window, however page looks like it loaded properly (maps and other content are present). I've already set timeout to 60 secs to exclude any slow network problems. And all the subsequent tests do work very fast (like proper Google scripts were fetched and are already cached). Also when I start server in development environment and access it (localhost:3000) everything works fine.

Firefox 17.0.1. Some of my gems:

capybara (2.0.1)
database_cleaner (0.9.1)
mongoid (3.0.13)
rspec (2.12.0)
rspec-core (2.12.1)
rspec-expectations (2.12.0)
rspec-mocks (2.12.0)
rspec-rails (2.12.0)
selenium-webdriver (2.26.0)

Do you have any idea why this happens and how to prevent it?

EDIT:

spec/spec_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.infer_base_class_for_anonymous_controllers = false
  #config.order = "random"
end

spec/features/features_spec_helper.rb:

require_relative "../spec_helper"

require 'capybara/rspec'

Capybara.default_driver = :selenium
Capybara.default_wait_time = 60

EDIT2:

It used to work properly before (few weeks ago; this project was halted for that time). Could be introduced by RSpec upgrade from 2.11 to 2.12 (which I did those few weeks ago), but I've just tried to downgrade it and same things do happen. I've reverted whole codebase to the point month ago to exclude possible gem regression. Problem still happens.

EDIT3:

I've just discovered that if I comment out the line responsible for attaching map from Google:

new google.maps.Map($("#map")[0], mapOptions)

then everything works like a charm.

EDIT4:

Source code of example application: https://github.com/skalee/capybara-google-maps-failure

Running all specs will result with Timeout::Error in the first one (at least for me). However, all specs will pass when:

  1. You remove line which initializes Geocoder (it's not used in example app, but I use it in mine).
  2. You remove line which initializes Map.
  3. What surprised me most, when you provide trivial style sheet, like nothing more than that:
#map{ width: 600px ; height: 600px }

Gems are exactly the same gems as I use in my app. There are some 3rd party scripts in /vendor/assets.

2
Could you write failing example code working with Google map at some site so we can reproduce it?Andrei Botalov
@AndreyBotalov — good point. I've prepared a sample app by stripping my app from stuff not related to map. I've also found two new ways to make specs passing (not fixing things though). The problem is really odd to me…skalee
Unfortunately I don't know Rails. After starting rails server I got the following exception when navigating to localhost:3000: require_tree argument must be a directory (in /path_is_here/capybara-google-maps-failure/app/assets/javascripts/application.js:26). How can I solve it?Andrei Botalov
@AndreyBotalov My mistake, one directory was not under version control. Fixed, thanks. So, if you're not rails guy, to run specs just type in bundle exec rspec.skalee
Have you tried to get capybara to save a screenshot at that point in the test? It can sometimes help. Also, try using WebMock to intercept the request and check what url is being used. Might be configured differently for test e.g. your API key might be in production.rb.Matt Gibson

2 Answers

1
votes
0
votes

Have you tried changing the GemFile around to have a group :production do ?

And are you loading this whole script in the proper place?

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false">

This example is for bootstrap:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE"
type="text/javascript">
</script>