Travis CI 无法识别 xvfb 上的大分辨率导致功能规范失败

Travis CI not recognizing large resolution on xvfb causing feature specs to fail

我正在尝试让 travis 通过我的功能规范,但是使用 xvfb 默认设置时,它以较小的 window 运行。由于该网站使用响应式网页设计,这意味着它可以像在移动设备上一样查看页面。因此我的测试失败了。

我四处搜索,找到了各种不同的方法来设置我的 .travis.yml 文件,其中 none 有效,但这是我目前拥有的:

.travis.yml

language: ruby
rvm:
- 2.3.0
addons:
  postgresql: "9.4"

before_install:
  - "export DISPLAY=:99.0"
  - "sh -e /etc/init.d/xvfb start"
  - "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1024x16"
  - "sleep 3"

before_script:
- cp config/database.yml.travis config/database.yml
- cp config/application.yml.example config/application.yml
- psql -c 'create database db_test;' -U postgres

script:
- bundle exec rake db:migrate
- bundle exec rake

具有响应位(在 haml 中)的示例视图代码:

_view.haml

.row
  .col-xs-12
    %label
      %span.visible-lg-inline Line {{question + 1}}:
      %b

如您所见,visible-lg 是一件很无聊的事情。整个应用程序在不同位置使用 visible-lg/hidden-lg 类,因此无法更改它。它也是自举的。

相关的 Travis 错误:

Failure/Error: expect(page).to have_content("Line 1: #{question}")

我的规格在本地通过,因为 Firefox 加载了标准大小的 window 与 mobile-sized/small window。如何更改我的 .travis.yml 文件以使其符合我设置的更大的屏幕分辨率?

Travis 错误的另一个例子:

Failure/Error: click_on 'Preview'

 Capybara::ElementNotFound:
   Unable to find link or button "Preview"

('Preview' 存在但不在移动模式下)

我正在使用 Ruby 2.3、Rails 4.2.5 和 AngularJS

答案不是我最初认为的 travis.yml 文件。

经过一周的搜索,我偶然发现了这个: https://discuss.circleci.com/t/capybara-driver-rack-test/407 which had a link to this: http://blaulabs.de/2011/11/22/acceptance-testing-with-responsive-layouts/

在我的 spec/spec_helper.rb 中,我将以下内容放在配置部分之后的最底部:

def set_selenium_window_size_to_large
  page.driver.browser.manage.window.resize_to(1280, 1280)
end

def set_selenium_window_size_to_small
  page.driver.browser.manage.window.resize_to(600, 600)
end

使用上面的代码,我现在可以调用 2 个不同的 window 尺寸,这样我就可以测试在移动模式或桌面模式下汉堡包菜单选项是否不显示。

在我的功能规范中,然后我在我的 before 块中调用了该方法,因此我所有需要它的测试都得到了它。另外,rails_helper 调用了 spec_helper,所以我被覆盖了。

require 'rails_helper'

feature 'A question page', :devise, js: true do
  before do
    @question = create :question

    set_selenium_window_size_to_large

    visit question_path
    click_on 'Get started'
  end
  scenario 'it has some questions' do
    expect(page).to have_content("Line 1: #{question}")
  end
end

我的 .travis.yml 文件变回了一个更简单的文件:

language: ruby
rvm:
- 2.3.0
addons:
  postgresql: "9.4"

before_install:
- "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -ac -screen 0 1280x1024x16"

before_script:
- cp config/database.yml.travis config/database.yml
- cp config/application.yml.example config/application.yml
- psql -c 'create database db_test;' -U postgres
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

script:
- bundle exec rake db:migrate
- bundle exec rake

我的所有测试都符合预期!