Why does my Cucumber test fail when run with Selen

2020-03-25 08:37发布

I am testing a Rails 3 app with a Cucumber/Capybara combo. I am also trying to use Selenium to test some JavaScript specific scenarios but am running into weird difficulties I don't understand.

My experience with Cucumber/Capybara is pretty low, my experience with Selenium is zero.

Here's the scenario:

Scenario: Browsing events
  Given many events exist
  And I am on the events page
  Then I should see a list of 15 events
  When I follow the first event
  Then I should be on the event page
  And I should see a google map
  And I should see the event details

When that scenario runs under RackTest, it passes all the way up to the Google Map step, at which point it fails because there's no JavaScript. This is expected.

When I run the test with the default JavaScript driver (Selenium) it fails on step three (I should see a list of 15 events). When I observe the browser window, indeed the list of events contains no events at all - almost as if they don't exist in the database.

Incidentally, the first step (many events exist), uses FactoryGirl to create a load of events.

As this is all pretty new to me, I wonder if I'm being caught out by a typical gotcha? I haven't made any configuration changes other than the standard install from rails g cucumber:install. Also, if it's relevant, I'm using OSX.

Thanks

2条回答
欢心
2楼-- · 2020-03-25 09:17

I presume you are trying to use transactional fixtures (the default behavior) with Selenium, but that won't work. The transaction that is managed within the test is out of scope when the browser invokes the Rails app separately, so it can't see any of the uncommitted data that your test has created.

Instead of transactional fixtures, you'll need to use one of the database cleaner gems.

https://github.com/bmabey/database_cleaner

Edit:

I subsequently became aware that it is possible to use transactional fixtures with Selenium tests, and performance is better if you do (credit to Kira Corina's answer). See http://pastie.org/1745020 for details.

查看更多
Ridiculous、
3楼-- · 2020-03-25 09:20

For those who have the same issue with database in selenium tests, here is a really useful chat with three basic solutions summarized by Jonas (see the first April, 5th message): https://groups.google.com/forum/#!msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ

查看更多
登录 后发表回答