为什么我的 Cucumber 测试在使用 Selenium 运行时失败?

发布于 2024-10-30 14:14:42 字数 841 浏览 8 评论 0原文

我正在使用 Cucumber/Capybara 组合测试 Rails 3 应用程序。我还尝试使用 Selenium 来测试一些 JavaScript 特定场景,但遇到了我不明白的奇怪困难。

我对 Cucumber/Capybara 的经验非常少,我对 Selenium 的经验为零。

场景如下:

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

当该场景在 RackTest 下运行时,它会一直传递到 Google Map 步骤,此时它会失败,因为没有 JavaScript。这是预料之中的。

当我使用默认 JavaScript 驱动程序 (Selenium) 运行测试时,它在第三步失败(我应该看到包含 15 个事件的列表)。当我观察浏览器窗口时,事件列表实际上根本不包含任何事件 - 几乎就好像它们不存在于数据库中一样。

顺便说一句,第一步(存在许多事件),使用 FactoryGirl 创建事件负载。

由于这对我来说都是新鲜事,我想知道我是否遇到了典型的陷阱?除了 rails g cucumber:install 的标准安装之外,我没有进行任何配置更改。另外,如果相关的话,我正在使用 OSX。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小伙你站住 2024-11-06 14:14:42

我假设您正在尝试将事务装置(默认行为)与 Selenium 一起使用,但这不起作用。当浏览器单独调用 Rails 应用程序时,测试中管理的事务超出范围,因此它看不到测试创建的任何未提交的数据。

您需要使用数据库清理工具之一,而不是事务固定装置。

https://github.com/bmabey/database_cleaner

编辑:

我随后意识到它是 可以在 Selenium 测试中使用事务装置,如果这样做,性能会更好(归功于 Kira Corina 的回答)。有关详细信息,请参阅 http://pastie.org/1745020

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.

对于那些在 selenium 测试中遇到与数据库相同问题的人,这里有一个非常有用的聊天,其中包含 Jonas 总结的三个基本解决方案(请参阅 4 月 5 日第一条消息):
https://groups.google.com/forum/#!msg/ruby-capybara/ JI6JrirL9gM/R6YiXj4gi_UJ

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文