Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用Selenium&;Mac上停靠Rails环境中的Chrome_Ruby On Rails_Docker_Selenium_Google Chrome_Capybara - Fatal编程技术网

Ruby on rails 如何使用Selenium&;Mac上停靠Rails环境中的Chrome

Ruby on rails 如何使用Selenium&;Mac上停靠Rails环境中的Chrome,ruby-on-rails,docker,selenium,google-chrome,capybara,Ruby On Rails,Docker,Selenium,Google Chrome,Capybara,我觉得我离得很近,但是撞到了墙,所以我希望得到一些建议 我的目标是能够使用Chrome运行我的系统测试和(截图) 非JS测试通过使用:rack\u test 当由:远程浏览器(我的Chrome设置)驱动时,我可以访问路径,截图显示无法访问此网站127.0.0.1拒绝连接 另外值得一提的是,如果我尝试在open的情况下使用vncvnc://localhost:5900我收到一个错误连接到“localhost”失败无法与“localhost”通信。确保远程计算机可用,并且防火墙未阻止屏幕共享 我知道

我觉得我离得很近,但是撞到了墙,所以我希望得到一些建议

我的目标是能够使用Chrome运行我的系统测试和(截图)

非JS测试通过使用
:rack\u test

当由
:远程浏览器
(我的Chrome设置)驱动时,我可以访问路径,截图显示无法访问此网站127.0.0.1拒绝连接

另外值得一提的是,如果我尝试在
open的情况下使用vncvnc://localhost:5900
我收到一个错误连接到“localhost”失败无法与“localhost”通信。确保远程计算机可用,并且防火墙未阻止屏幕共享

我知道我错过了一些东西,但我被什么困扰着

这是我的
docker compose.yml
文件。我使用的是独立的Chrome图像

version: '3'

services:

  app: &app
    build: .
    tmpfs:
      - /tmp

  backend: &backend
    <<: *app
    stdin_open: true
    tty: true
    volumes:
      - .:/usr/src/app:cached
      - rails_cache:/usr/src/app/tmp/cache
      - bundle:/usr/local/bundle
      - node_modules:/app/node_modules
      - packs:/app/public/packs
    env_file:
      - .env/development/database
      - .env/development/web
    environment:
      - WEBPACKER_DEV_SERVER_HOST=webpack-dev-server

  web:
    <<: *backend
    entrypoint: ./bin/docker-entrypoint.sh
    command: bundle exec rails server -b 0.0.0.0
    ports:
      - "3000:3000"

  chrome:
    image: selenium/standalone-chrome:3.141.59
    ports:
      - "4444:4444"
      - "5900:5900"

  webpack-dev-server:
    <<: *app
    command: ./bin/webpack-dev-server
    volumes:
      - .:/usr/src/app:cached
      - bundle:/usr/local/bundle
      - node_modules:/app/node_modules
      - packs:/app/public/packs
    ports:
      - "3035:3035"
    environment:
      - WEBPACKER_DEV_SERVER_HOST=0.0.0.0

  database:
    image: postgres
    env_file:
      - .env/development/database
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:
  rails_cache:
  bundle:
  node_modules:
  packs:
然后我的
rails\u helper.rb
文件包括

要求“水豚/rspec”
需要“selenium webdriver”
Capybara.server_host='0.0.0.0'
Capybara.server_端口=3000
Capybara.app_主机=”http://localhost:3000"
Capybara.register_驱动程序:远程浏览器do|应用程序|
capabilities=Selenium::WebDriver::Remote::capabilities.chrome(
chromeOptions:{args:%w(禁用gpu无沙盒)}
)
水豚::硒::驱动程序。新(应用程序,
:browser=>:远程,
url:“http://chrome:4444/wd/hub",
期望的功能:功能
)
终止
Capybara.javascript\u驱动程序=:远程\u浏览器
当我运行测试时,我启动Docker

docker-compose up chrome database
在我的日志中,我看到了关于Chrome服务启动的信息

chrome_1              | 2020-04-24 16:17:05,059 INFO spawned: 'xvfb' with pid 10
chrome_1              | 2020-04-24 16:17:05,060 INFO spawned: 'selenium-standalone' with pid 11
chrome_1              | 16:17:05.229 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
chrome_1              | 2020-04-24 16:17:05,230 INFO success: xvfb entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
chrome_1              | 2020-04-24 16:17:05,230 INFO success: selenium-standalone entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
chrome_1              | 16:17:05.289 INFO [GridLauncherV3.lambda$buildLaunchers$3] - Launching a standalone Selenium Server on port 4444
chrome_1              | 2020-04-24 16:17:05.328:INFO::main: Logging initialized @260ms to org.seleniumhq.jetty9.util.log.StdErrLog
chrome_1              | 16:17:05.486 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
chrome_1              | 16:17:05.548 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444
我的示例测试非常简单

require'rails\u helper'
RSpec.description“Example”,类型::system do
在做之前
驱动者(:远程浏览器)
终止
它“应该采取根路径截图”做
访问根路径
截图
终止
终止
它跑来跑去

[Screenshot]: /usr/src/app/tmp/screenshots/r_spec_example_598.png
.

Finished in 0.77216 seconds (files took 1.73 seconds to load)
1 example, 0 failures
这是日志

chrome_1              | 16:29:55.662 INFO [ActiveSessionFactory.apply] - Capabilities are: {
chrome_1              |   "browserName": "chrome",
chrome_1              |   "chromeOptions": {
chrome_1              |     "args": [
chrome_1              |       "disable-gpu",
chrome_1              |       "no-sandbox"
chrome_1              |     ]
chrome_1              |   },
chrome_1              |   "cssSelectorsEnabled": true,
chrome_1              |   "javascriptEnabled": true,
chrome_1              |   "nativeEvents": false,
chrome_1              |   "rotatable": false,
chrome_1              |   "takesScreenshot": false,
chrome_1              |   "version": ""
chrome_1              | }
chrome_1              | 16:29:55.662 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
chrome_1              | Starting ChromeDriver 81.0.4044.69 (6813546031a4bc83f717a2ef7cd4ac6ec1199132-refs/branch-heads/4044@{#776}) on port 13909
chrome_1              | Only local connections are allowed.
chrome_1              | Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
chrome_1              | [1587745795.669][SEVERE]: bind() failed: Cannot assign requested address (99)
chrome_1              | 16:29:55.943 INFO [ProtocolHandshake.createSession] - Detected dialect: W3C
chrome_1              | 16:29:55.943 INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session d8f42b8387f0160b95f333eac5c1b576 (org.openqa.selenium.chrome.ChromeDriverService)
chrome_1              | 16:29:56.287 INFO [ActiveSessions$1.onStop] - Removing session d8f42b8387f0160b95f333eac5c1b576 (org.openqa.selenium.chrome.ChromeDriverService)
屏幕截图是这样的

另外,这是我试图打开
vnc://localhost:5900


我已经接近承认失败,并且没有测试这些用例,所以我非常感谢一些建议。谢谢

感谢托马斯·沃尔波尔为我指明了正确的方向

我现在已经开始工作了,我想我应该发布解决方案,以防其他人难以让设置正常工作

第一点-vnc不工作

这很容易修复我使用的Chrome版本,但没有包括这一点,因此有必要使用debug selenium版本

docker compose.yml
中的
selenium\u chrome
服务变为

  selenium_chrome:
    image: selenium/standalone-chrome-debug:3.141.59
    ports:
      - "5900:5900"
主要网络问题

正如Thomas Walpole所解释的,这与
Capybara.app\u主机相关

解决方案采取了若干步骤。首先,测试服务器现在在另一个端口4000上广播

docker compose.yml
中的
web
服务添加了端口

  web:
    <<: *backend
    entrypoint: ./bin/docker-entrypoint.sh
    command: bundle exec rails server -b 0.0.0.0
    ports:
      - "3000:3000"
      - "4000:4000"
我的代码的最后一个问题是该代码的位置。它还需要位于
Rspec.config
块中

完成
rails\u helper.rb
code

因此,
rails\u helper.rb
中完整的工作最终相关代码是这样的

require 'capybara/rspec'
require 'selenium-webdriver'

Capybara.register_driver :headless_selenium_chrome_in_container do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { args: %w(headless disable-gpu window-size=1440x768) }
  )

  Capybara::Selenium::Driver.new(app,
    :browser => :remote,
    url: "http://selenium_chrome:4444/wd/hub",
    desired_capabilities: capabilities
  )
end

Capybara.register_driver :selenium_chrome_in_container do |app|
  Capybara::Selenium::Driver.new(app,
    :browser => :remote,
    url: "http://selenium_chrome:4444/wd/hub",
    desired_capabilities: :chrome
  )
end

RSpec.configure do |config|
  ...

  config.before(:each, type: :system) do
    Capybara.server_host = '0.0.0.0'
    Capybara.server_port = 4000
    Capybara.app_host = "http://web:4000"
    driven_by(:headless_selenium_chrome_in_container)
  end
end

注意:这提供了无头运行Chrome的选项,或通过
vnc://localhost:5900

感谢托马斯·沃尔波尔为我指明了正确的方向

我现在已经开始工作了,我想我应该发布解决方案,以防其他人难以让设置正常工作

第一点-vnc不工作

这很容易修复我使用的Chrome版本,但没有包括这一点,因此有必要使用debug selenium版本

docker compose.yml
中的
selenium\u chrome
服务变为

  selenium_chrome:
    image: selenium/standalone-chrome-debug:3.141.59
    ports:
      - "5900:5900"
主要网络问题

正如Thomas Walpole所解释的,这与
Capybara.app\u主机相关

解决方案采取了若干步骤。首先,测试服务器现在在另一个端口4000上广播

docker compose.yml
中的
web
服务添加了端口

  web:
    <<: *backend
    entrypoint: ./bin/docker-entrypoint.sh
    command: bundle exec rails server -b 0.0.0.0
    ports:
      - "3000:3000"
      - "4000:4000"
我的代码的最后一个问题是该代码的位置。它还需要位于
Rspec.config
块中

完成
rails\u helper.rb
code

因此,
rails\u helper.rb
中完整的工作最终相关代码是这样的

require 'capybara/rspec'
require 'selenium-webdriver'

Capybara.register_driver :headless_selenium_chrome_in_container do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { args: %w(headless disable-gpu window-size=1440x768) }
  )

  Capybara::Selenium::Driver.new(app,
    :browser => :remote,
    url: "http://selenium_chrome:4444/wd/hub",
    desired_capabilities: capabilities
  )
end

Capybara.register_driver :selenium_chrome_in_container do |app|
  Capybara::Selenium::Driver.new(app,
    :browser => :remote,
    url: "http://selenium_chrome:4444/wd/hub",
    desired_capabilities: :chrome
  )
end

RSpec.configure do |config|
  ...

  config.before(:each, type: :system) do
    Capybara.server_host = '0.0.0.0'
    Capybara.server_port = 4000
    Capybara.app_host = "http://web:4000"
    driven_by(:headless_selenium_chrome_in_container)
  end
end

注意:这提供了无头运行Chrome的选项,或通过
vnc://localhost:5900

通过设置
Capybara.app\u主机=”http://localhost:3000"
您是说测试应该告诉浏览器访问
http://localhost:3000
-但浏览器上的localhost是浏览器docker容器,它不是运行应用程序的地方。从chrome容器的角度来看,您需要将其设置为正在运行测试的应用程序的任何位置。谢谢您的帮助,为我指明了找到工作解决方案的正确方向(发布在下面)。非常感谢。通过设置
Capybara.app\u主机=”http://localhost:3000“
您是说测试应该告诉浏览器访问
http://localhost:3000
-但浏览器上的localhost是浏览器docker容器,它不是运行应用程序的地方。从chrome容器的角度来看,您需要将其设置为正在运行测试的应用程序的任何位置。谢谢您的帮助,为我指明了找到工作解决方案的正确方向(发布在下面)。非常感谢。