gpt4 book ai didi

ruby-on-rails - 如何在 Mac 上的 Dockerized Rails 环境中使用 Selenium 和 Chrome 运行 Capybara 测试

转载 作者:行者123 更新时间:2023-12-04 13:14:58 25 4
gpt4 key购买 nike

我觉得我很接近了,但是碰壁了,所以希望得到一些建议。

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

非 JS 测试通过 :rack_test .

当由 :remote_browser 驱动时(我的 Chrome 设置)我可以访问路径,截取显示 的屏幕截图无法访问此站点 127.0.0.1 拒绝连接。

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

我知道我错过了一些东西,但我不知道是什么。

这是我的 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:

我的 Gemfile包括 capybaraselenium-webdriver
group :test do
gem 'capybara', '~> 3.32', '>= 3.32.1'
gem 'selenium-webdriver', '~> 3.142', '>= 3.142.7'
end

然后我的 rails_helper.rb文件包括这个

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

Capybara.server_host = '0.0.0.0'
Capybara.server_port = 3000
Capybara.app_host = "http://localhost:3000"

Capybara.register_driver :remote_browser do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w(disable-gpu no-sandbox) }
)

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

Capybara.javascript_driver = :remote_browser


当我运行测试时,我启动 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

然后我用这个运行系统测试
docker-compose run --rm web bundle exec rspec spec/system/example_spec.rb

我的示例测试非常简单

require 'rails_helper'

RSpec.describe "Example", type: :system do
before do
driven_by(:remote_browser)
end

it "should take a screenshot of root path" do
visit root_path
take_screenshot
end
end

它运行并通过
[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)

屏幕截图看起来像这样......

enter image description here

此外,这是我尝试打开 vnc://localhost:5900 时的错误消息

enter image description here

到了我接近承认失败并且不测试这些用例的地步,所以我真的很感激一些建议。谢谢!

最佳答案

感谢 Thomas Walpole 为我指明了正确的方向。

我现在可以使用它,并认为我会发布解决方案,以防其他人难以使设置正常工作。

第一点 — vnc 不工作

这很容易修复——我使用的 Chrome 版本没有包含这个,有必要使用 debug selenium 版本。
selenium_chrome服务在 docker-compose.yml变成这个

  selenium_chrome:
image: selenium/standalone-chrome-debug:3.141.59
ports:
- "5900:5900"

主要网络问题

这与 Capybara.app_host有关正如托马斯·沃波尔解释的那样。

该解决方案采取了许多步骤。首先,测试服务器现在在不同的端口 4000 上进行广播。
web服务在 docker-compose.yml添加了端口。
  web:
<<: *backend
entrypoint: ./bin/docker-entrypoint.sh
command: bundle exec rails server -b 0.0.0.0
ports:
- "3000:3000"
- "4000:4000"
rails_helper.rb使用此端口和 webweb 的 Docker 内部网络地址服务。
    Capybara.server_host = '0.0.0.0'
Capybara.server_port = 4000
Capybara.app_host = "http://web:4000"

我的代码的最后一个问题是此代码的位置。它还需要在 Rspec.config 内堵塞。

完整 rails_helper.rb代码

完整的工作最终相关代码在 rails_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

注意:这提供了 headless 运行 Chrome 的选项,或使用可通过 vnc://localhost:5900 访问的浏览器.

关于ruby-on-rails - 如何在 Mac 上的 Dockerized Rails 环境中使用 Selenium 和 Chrome 运行 Capybara 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61413196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com