3

Coming from Rails 3 and having never used Capybara/Selenium I am now running a Rails 5 application in docker and want to use the "updated" way of writing tests, i.e. using system, request and model specs rather than controller specs. I've added another docker instance running the same image with Guard to trigger the appropriate tests whenever something changed. And as RSpecs system tests need Selenium, I'm running the official selenium/standalone-chrome image for docker. Everything seems to be plugged in perfectly, however the system tests fail, because it seems that Selenium is trying to use SSL. Why is that the case, if I've clearly requested to use http? Is there any way to switch that off?

I'm using docker-compose to get everything up and running, with the important parts of the docker-compose.yml being:

version: '3.5'

services:

  app:
    build: .
    volumes:
      - .:/srv/mldb
    ports:
      - "3000:3000"
    depends_on:
      - db
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    tty: true

  guard:
    build: .
    volumes:
      - .:/srv/mldb
    depends_on:
      - app
      - chrome
    command: bundle exec guard --no-bundler-warning --no-interactions
    tty: true
  chrome:
    image: selenium/standalone-chrome
    volumes:
      - /dev/shm:/dev/shm
    ports:
      - "4444:4444"

And here are all relevant lines from the spec/rails_helper.rb that I use to set up Capybara and Selenium:

selenium_url = "http://chrome:4444/wd/hub"

Capybara.register_driver :selenium_remote do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :remote,
    url: selenium_url,
    desired_capabilities: :chrome)
end

RSpec.configure do |config| 
  config.before(:each, type: :system, js: true) do
    driven_by :selenium_remote
    host! "http://app:3000"
  end
end

The errors that I am getting when running an example test that simply tries to fill in an input field and press a submit button.

From the Guard container running the system spec:

1) Movie management given correct input values allows user to create movie
    Capybara::ElementNotFound:
       Unable to find button "New Movie"

From the app container running rails:

2019-04-06 16:28:22 +0000: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.>

The content of the log-file:

   (0.4ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK

And the Screenshot that Capybara takes:

Error

All other tests (request and model specs) run perfectly and testing everything manually works perfectly as well.

Here is the system spec that fails:

require 'rails_helper'                                                                                

RSpec.describe "Movie management", type: :system, js: true do                                         

  context "given correct input values" do                                                             

    it "enables users to create movies" do                                                            
      visit "/movies"                                                                                 
      expect(page).to_not have_text("Hellboy")                                                        

      click_button "New Movie"                                                                        

      fill_in "Titel", with: "Hellboy"                                                                
      fill_in "Jahr", with: "2004"                                                                    

      click_button "Film anlegen"                                                                     

      expect(page).to have_current_path("/movies")                                                    
      expect(page).to have_text("Film erfolgreich angelegt.")                                         
      expect(page).to have_text("Hellboy")                                                            
      expect(page).to have_text("2004")                                                               
    end                                                                                               
  end                                                                                                 
end                        
pygospa
  • 43
  • 5

1 Answers1

4

The problem is that Google owns the TLDs app and enforces the browser to use HTTPS. Here's the link about it.

https://superuser.com/questions/1276048/starting-with-chrome-63-urls-containing-app-redirects-to-https

The solution is to not use app as the container name for you website. When using website as name my websites container my tests were running fine.

More info about Google TLDs in this link

https://chromium.googlesource.com/chromium/src/net/+/master/http/transport_security_state_static.json#285

Samuel
  • 513
  • 3
  • 17