首页 > 解决方案 > 连接 Rails 应用程序和无浏览器/chrome 容器

问题描述

我正在尝试使用browserless/chromedocker 映像并从在另一个 docker 容器中运行的 Ruby on Rails (6.0) 应用程序连接到它。我很难让两者联系起来。

无浏览器容器运行正常(我知道是因为我可以访问它附带的 Web 调试界面)。

在 Rails 应用程序中,我尝试使用capybarawithselenium-webdriver来驱动无头 chrome 实例。

我收到错误消息说它无法连接或无法发布到“/会话”,我认为这意味着它正在正确连接但由于某种原因无法在 chrome 中初始化会话。

以下是我当前的配置+代码:

#docker-compose.yml

version: "3"
services:
  web:
    build: .
    image: sync-plus.web:latest
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    environment:
      RAILS_ENV: development
      RACK_ENV: development
      REDIS_URL: redis://redis:6379/0
      DATABASE_URL_TEST: postgres://postgres:super@postgres:5432/salesync_test
    command: "bin/puma -C config/puma.rb"
    depends_on:
      - postgres
      - mailcatcher
      - redis

  sidekiq:
    image: sync-plus.web:latest
    volumes:
      - .:/app
    environment:
      RAILS_ENV: development
      RACK_ENV: development
      REDIS_URL: redis://redis:6379/0
      DATABASE_URL_TEST: postgres://postgres:super@postgres:5432/salesync_test
    command: "bundle exec sidekiq"
    depends_on:
      - postgres
      - mailcatcher
      - web
      - redis

  guard:
    image: sync-plus.web:latest
    volumes:
      - .:/app
    environment:
      RAILS_ENV: test
      RACK_ENV: test
      REDIS_URL: redis://redis:6379/1
      DATABASE_URL_TEST: postgres://postgres:super@postgres:5432/salesync_test
    command: "bin/rspec"
    depends_on:
      - postgres

  redis:
    image: redis:alpine
    command: redis-server
    volumes:
      - 'redis:/data'

  postgres:
    image: postgres:latest
    volumes:
      - postgres_volume:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: super

  mailcatcher:
    image: schickling/mailcatcher:latest
    ports:
      - "1080:1080"

  browserless:
    image: browserless/chrome:latest
    ports:
      - "4444:3000"

volumes:
  postgres_volume:
  redis:
#rails_helper.rb
...
require "rspec/rails"

# This require needs to be below the rspec/rails require
require 'capybara/rails'
...

我正在使用 Rspec 测试来检查无浏览器连接/驱动是否正常工作:

#browserless_spec.rb

require "rails_helper"

RSpec.describe 'Browserless', type: :feature do
  describe "Connection" do
    it "can connect to browserless container" do

      #selenium webdriver looks at localhost:4444 by default
      driver = Selenium::WebDriver.for :remote

      
      driver.visit("/users/sign_in")
      expect(page).to have_selector("btn btn-outline-light my-2")
    end
  end
end

我似乎在其他地方找不到任何可靠的解决方案或线索,所以任何帮助将不胜感激!

谢谢!

标签: ruby-on-railsselenium-webdriverdocker-composecapybaragoogle-chrome-headless

解决方案


推荐阅读