首页 > 解决方案 > Rails 6,Docker,系统测试 - 预计在“”中找到文本“Home” - 页面未呈现

问题描述

我刚刚开始了一个在开发中使用 Docker 的新 Rails 6 项目。这是我第一次 Dockerizing 一个 Rails 项目,所以也许这是一个简单的修复,我似乎可以弄清楚。

我正在使用 minitest 和 capybara 运行 systemtests,但出现以下错误:

FAIL["test_visiting_the_index", #<Minitest::Reporters::Suite:0x000056464fc2f9c0 @name="HomesTest">, 3.103664200010826]
  test_visiting_the_index#HomesTest (3.10s)
    expected to find text "Home" in ""
    test/system/homes_test.rb:7:in `block in <class:HomesTest>'

我的猜测是它与铬硒设置有关?我使用了错误的图像还是什么?

我已经尝试了不同的示例项目(Rails 5 项目),效果很好,但我无法让它在我自己的项目中工作。我正要沮丧地拔掉头发,但我想我会先问你们。

你能帮助我或指导我一个方向吗?

我已经粘贴了项目中的一些重要文件。

谢谢

回购https://github.com/jonashan/intercompter/tree/moving-back-to-minitest

Dockerfile

  FROM ruby:2.7.1

  # Set the locale
  ENV LANG C.UTF-8

  RUN apt-get update -qq && apt-get install -y nodejs postgresql-client tzdata
  RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && apt-get install -y yarn

  RUN mkdir /app
  WORKDIR /app
  COPY Gemfile /app/Gemfile
  COPY Gemfile.lock /app/Gemfile.lock
  RUN bundle install
  COPY . /app

  # Start rails
  CMD ["rails", "server", "-b", "0.0.0.0"]

码头工人-compose.yml

  version: '3.2'

  services:
...
    app:
      depends_on:
        - db
        - chrome
      build: .
      command: foreman start -f Procfile.dev -p 3000
      ports:
        - "3000:3000"
        - "35729:35729"
        - "3434:3434"
      environment:
        - DATABASE_HOST=db
        - BUNDLE_PATH=/bundle/vendor
        - HUB_URL=http://chrome:4444/wd/hub
      volumes:
        - .:/app
        - type: tmpfs
          target: /app/tmp/pids/
        - bundle_path:/bundle
...
    chrome:
      image: selenium/standalone-chrome-debug
      volumes:
        - /dev/shm:/dev/shm
      ports:
      - "4444:4444"

  volumes:
    redis:
    postgres:
    bundle_path:
...
  class ActiveSupport::TestCase
    # Run tests in parallel with specified workers
    parallelize(workers: :number_of_processors)

    # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
    fixtures :all

    # Add more helper methods to be used by all tests here...
  end
...

application_system_test_case.rb

  require "test_helper"

  Capybara.register_driver :chrome_headless do |app|
    chrome_capabilities = ::Selenium::WebDriver::Remote::Capabilities.chrome('goog:chromeOptions' => { 'args': %w[no-sandbox headless disable-gpu window-size=1400,1400] })

    if ENV['HUB_URL']
      Capybara::Selenium::Driver.new(app,
                                    browser: :remote,
                                    url: ENV['HUB_URL'],
                                    desired_capabilities: chrome_capabilities)
    else
      Capybara::Selenium::Driver.new(app,
                                    browser: :chrome,
                                    desired_capabilities: chrome_capabilities)
    end
  end

  class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
    parallelize(workers: 1)
    
    driven_by :chrome_headless
  end

home_test.rb

  require "application_system_test_case"

  class HomesTest < ApplicationSystemTestCase
    test "visiting the index" do
      visit root_path
    
      assert_text "Home"
    end
  end

主页 index.html.erb (root_path)

  <h1>Home</h1>

标签: ruby-on-railsdockerseleniumtestingminitest

解决方案


推荐阅读