ruby 带有Docker映像和www.example.com的Selenium WebDrive存在问题Cucumber.io

nfzehxib  于 2023-02-12  发布在  Ruby
关注(0)|答案(2)|浏览(112)
    • bounty将在4天后过期**。回答此问题可获得+50的声誉奖励。XmalevolentX正在寻找来自声誉良好来源的答案:寻找一个解决方案,给我的最终结果的视频中的问题。

我试图按照以下教程。
https://www.youtube.com/watch?v=cPF3GKkBHHY
我收到以下错误。
sh: 1: /sbin/ip: not found
但我设法解决了这个问题,先做apt update,然后做apt install iproute2 -y
我现在收到以下错误

cucumber
Feature: Search for things on Google and see results.

  Scenario: See related words when searching. # features/basic.feature:3
    When I search for "puppies"               # features/step_defs.rb:1
      Selenium::WebDriver::Remote::Driver needs :options to be set (ArgumentError)
      ./features/step_defs.rb:2:in `"I search for {string}"'
      features/basic.feature:4:in `I search for "puppies"'
    Then I should see "dog"                   # features/step_defs.rb:7

我想解决的错误就是这个。
Selenium::WebDriver::Remote::Driver needs :options to be set (ArgumentError)
这是我的. env. rb文件。

require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'pry'

#if you're accessing an internal app behind a firewall, you may not need the proxy. You can unset it like so:
#ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil

#get IP of host which has 4444 mapped from other container
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip

Capybara.register_driver :remote_chrome do |app|
  Capybara::Selenium::Driver.new(app,
  :browser => :remote,
  :desired_capabilities => :chrome,
  :url => "http://#{docker_ip}:4444/wd/hub",
  :options => chrome_options)
end

Capybara.configure do |config|
  config.run_server = false
  config.default_driver = :remote_chrome
  config.app_host = 'http://www.google.com' # change this to point to your application
end

对此的任何帮助都将不胜感激。
谢谢!

lf5gs5x2

lf5gs5x21#

你需要创建一个Selenium::WebDriver::Chrome::Options的示例,并将其传递给options argument。将这个chrome_options = Selenium::WebDriver::Chrome::Options.new添加到你的代码中,如下所示。

require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'pry'

#if you're accessing an internal app behind a firewall, you may not need the proxy. You can unset it like so:
#ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil

#get IP of host which has 4444 mapped from other container
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip

# Add options for the Chrome browser
chrome_options = Selenium::WebDriver::Chrome::Options.new

# Disable notifications
chrome_options.add_argument("--disable-notifications")


Capybara.register_driver :remote_chrome do |app|
  Capybara::Selenium::Driver.new(app,
  :browser => :remote,
  :browser_name => :chrome,
  :url => "http://#{docker_ip}:4444/wd/hub",
  :options => chrome_options)
end

Capybara.configure do |config|
  config.run_server = false
  config.default_driver = :remote_chrome
  config.app_host = 'http://www.google.com' # change this to point to your application
end
z6psavjg

z6psavjg2#

解决方案:
更新env. rb文件。
之前:

require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'pry'

#if you're accessing an internal app behind a firewall, you may not need the proxy. You can unset it like so:
#ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil

#get IP of host which has 4444 mapped from other container
docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip

Capybara.register_driver :remote_chrome do |app|
  Capybara::Selenium::Driver.new(app,
  :browser => :remote,
  :desired_capabilities => :chrome,
  :url => "http://#{docker_ip}:4444/wd/hub")
end

Capybara.configure do |config|
  config.run_server = false
  config.default_driver = :remote_chrome
  config.app_host = 'http://www.google.com' # change this to point to your application
end

之后:

require 'rspec' #for page.shoud etc
require 'capybara/cucumber'
require 'cucumber'
require 'pry'

require "selenium-webdriver"

# Ask capybara to register a driver called 'selenium'
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(
      app,

      #what browser do we want? Must match whatever is in our seleniarm stand-alone image
      browser: :firefox, 
      
      #where does it live? By passing a URL we tell capybara to use a selenium grid instance (not local)
      url: "http://#{ENV['SELENIUM_HOST']}:#{ENV['SELENIUM_PORT']}" 
  )
end

# make the driver we just registered our default driver
Capybara.default_driver = :selenium

# set the default URL for our tests
Capybara.app_host = "https://www.google.com/"

更新基本功能
之前:

Feature: Search for things on Google and see results.

Scenario: See related words when searching.
  When I search for "puppies"
  Then I should see "dog"

  
Scenario: Don't see unrelated words when searching.
  When I search for "dachshund"
  Then I should NOT see "fish"

之后:

Feature: Search for things on Google and see results.

Background:
  Given I accept cookies

Scenario: See related words when searching.
  When I search for "puppies"
  Then I should see "dog"

  
Scenario: Don't see unrelated words when searching.
  When I search for "dachshund"
  Then I should NOT see "fish"

更新step_defs. rb
之前:

When("I search for {string}") do |string|
  visit "/"
  fill_in "q", with: string
  click_on "Google Search", match: :first
end

Then("I should see {string}") do |string|
  page.should have_content(string)
end

Then("I should NOT see {string}") do |string|
  page.should_not have_content(string)
end

之后:

Given('I accept cookies') do
  visit "/"
  click_on "Accept all"
end

When("I search for {string}") do |string|
  visit "/"
  fill_in "q", with: string
  click_on "Google Search", match: :first
end

Then("I should see {string}") do |string|
  page.should have_content(string)
end

Then("I should NOT see {string}") do |string|
  page.should_not have_content(string)
end

# a handy debugging step you can use in your scenarios to invoke 'pry' mid-scenario and poke around
When('I debug') do
  binding.pry
end

更新停靠文件
之前:

#start with the base ruby image
FROM ruby

#make sure we have a folder called /app
RUN mkdir /app

#cd into our app folder each time we start up
WORKDIR /app

#copy our Gemfile and Gemfile.lock
COPY Gemfile* /app/

#install the gems
RUN bundle

CMD cucumber

之后:

#start with the base ruby image
FROM ruby

#always a good idea to update and have an editor
RUN apt-get update; apt-get install -y vim

#make sure we have a folder called /app
RUN mkdir /app

#cd into our app folder each time we start up
WORKDIR /app

#copy our Gemfile and Gemfile.lock
COPY Gemfile* /app/

#install the gems
RUN bundle

CMD cucumber

更新宝石文件
之前:

source 'https://rubygems.org'

gem 'cucumber'
gem 'capybara'
#gem 'capybara-cucumber'
gem 'capybara-screenshot'
gem 'pry'
gem 'rspec'
gem 'selenium-webdriver'

之后:

source 'https://rubygems.org'

gem 'cucumber'
gem 'capybara'
gem 'capybara-screenshot'
gem 'pry'
gem 'rspec'
#gem 'selenium-webdriver', '3.142.7' # Haven't been able to make 4.x work, yet...
gem 'selenium-webdriver', '~> 4.4.0'

更新docker-compose. yml
之前:

version: '3'
services:
  browser:
    # See inside via VNC with the "debug" version
    image: selenium/standalone-chrome-debug
    
    # Slightly faster headless version
    #image: selenium/standalone-chrome    
    
    ports:
      - "5900:5900" #for VNC access
      - "4444:4444" #for webdriver access

  ruby:
    build: .
    volumes:
      - .:/app
    depends_on: 
      - browser

之后:

version: '3'
services:
  browser:
    
    #use for Apple Silicon
    #image: seleniarm/standalone-chromium

    # Chrome is crashing for me so I'm using firefox for now
    image: seleniarm/standalone-firefox
    
    ports:
      - "5900:5900" #for VNC access
      - "4444:4444" #for webdriver access
      - "7900:7900" #for web VNC access

  ruby:
    build: .
    volumes:
      - .:/app
    depends_on: 
      - browser
    environment: 
      - SELENIUM_HOST=browser
      - SELENIUM_PORT=4444

相关问题