mercredi 24 juin 2020

Rails 6, Docker, system tests - expected to find text "Home" in "" - Page is not rendered

I just started a new Rails 6 project that uses Docker in development. This is my first time Dockerizing a Rails project, so maybe this is an easy fix, that I just can seem to figure out.

I am using minitest and capybara to run systemtests, but I get the following error:

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>'

My guess is that it has something to do with the chrome selenium setup? Am I using a wrong image or something?

I have tried out different example projects (Rails 5 projects), that is working fine, but I can't get it to work in my own project. I was about to pull out my hair in frustration, but figured that I would ask you guys first.

Can you help me or guide me in a direction?

I have pasted some of the important files from the project.

Thank you

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"]

docker-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

homes_test.rb

  require "application_system_test_case"

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

home index.html.erb (root_path)

  <h1>Home</h1>

Aucun commentaire:

Enregistrer un commentaire