I use Selenium::WebDriver::Remote::Capabilities.chrome and chromeOptions with --proxy-server (mitmdump is used like proxy-server). But when I run the code and go to check ip, it's still mine.
args = [
"mitmdump",
"--listen-host", "127.0.0.1",
"--listen-port", #port,
"--mode", "upstream:http://#{proxy.ip}:#{proxy.port}",
"--upstream-auth", #{proxy.login}:#{proxy.password}
]
server = ChildProcess.build(*args)
server.start
raise "Oops" unless server.alive?
Capybara.register_driver :selenium do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
"args" => [
"--headless",
"--proxy-server=http://127.0.0.1:#port",
"--disable-dev-shm-usage",
"--blink-settings=imagesEnabled=true",
"window-size=1440,900",
"--no-sandbox"
]
}
)
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: caps)
end
Capybara.javascript_driver = :chrome
Capybara.configure do |config|
config.default_max_wait_time = 20
config.default_driver = :selenium
end
session = Capybara::Session.new(:selenium)
session.visit("https://www.2ip.ru")
The latest Selenium/Chrome combo silently ignores the "chromeOptions" option, the new syntax for that is "goog:chromeOptions" https://github.com/SeleniumHQ/docker-selenium/issues/674
Related
I've got a script which downloads a pdf from a site which keeps updating every month and I want to automate this. It works but I cannot get it to work headless and I think it's because it's not handling the downloads correctly. It seems to start the chrome in headless ok and my navigation commands seem to work, but when it goes to download nothing happens.
#!/usr/bin/env ruby
#
require 'capybara'
require 'rb-inotify'
require 'webdrivers/chromedriver'
def initialise
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: chrome_options)
end
#session = Capybara::Session.new(:chrome)
end
# Settings and profile for the Chrome Browser
# NOTE: still cannot get headless working
def chrome_options
opts = Selenium::WebDriver::Chrome::Options.new
opts.add_argument('--headless') unless ENV['UI']
opts.add_argument('--no-sandbox')
opts.add_argument('--disable-gpu')
opts.add_argument('--disable-dev-shm-usage')
opts.add_argument('--window-size=1920,1080')
opts.add_preference(:download,
directory_upgrade: true,
prompt_for_download: false,
default_directory: "~/Downloads")
opts.add_preference(:plugins,
plugins_disabled: ["Chrome PDF Viewer"])
opts.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
opts
end
Update I'm using Chrome version 81.0.4044.113-1
Across different versions of Chrome and selenium-webdriver the settings required to get downloads working have changed/grown. It looks like you're missing one of them.
opts.add_preference('download.default_directory', '~/Downloads')
Another thing you can also do, depending on versions, is
def initialise
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: chrome_options).tap do |driver|
driver.browser.download_path = '~/Downloads'
end
#session = Capybara::Session.new(:chrome)
end
I added some configurations to your code, probably it will work:
#!/usr/bin/env ruby
#
require 'capybara'
require 'rb-inotify'
require 'webdrivers/chromedriver'
def initialise
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: chrome_options)
end
#session = Capybara::Session.new(:chrome)
end
# Settings and profile for the Chrome Browser
# NOTE: still cannot get headless working
def chrome_options
download_directory = "~/Downloads"
opts = Selenium::WebDriver::Chrome::Options.new
opts.add_argument('--headless') unless ENV['UI']
opts.add_argument('--no-sandbox')
opts.add_argument('--disable-gpu')
opts.add_argument('--disable-dev-shm-usage')
opts.add_argument('--window-size=1920,1080')
opts.add_preference(:download,
directory_upgrade: true,
prompt_for_download: false,
default_directory: download_directory)
opts.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
driver = Capybara::Selenium::Driver.new(app, browser: :chrome,
options: options)
bridge = driver.browser.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
params: {
behavior: 'allow',
downloadPath: download_directory
})
driver
opts.add_preference(:plugins,
always_open_pdf_externally: true)
opts.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
opts
end
I'm using Cucumber with Ruby. When running tests in Chrome via Selenium-Webdriver, I'd like to alter the download location to the test folder instead of the users download folder.
My current chrome driver is set up like so:
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome,
desired_capabilities: {
'chromeOptions' => {
'args' => %w{ window-size=1920,1080 }
}
}
)
end
What would I need to add there to change the download location please?
The download directory can be set with the download.default_directory preference:
require 'capybara'
require 'selenium-webdriver'
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app,
:browser => :chrome,
:desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {
'args' => [ "--window-size=1920,1080" ],
'prefs' => {
'download.default_directory' => File.expand_path("C:\\Download"),
'download.prompt_for_download' => false,
'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
}
}
)
)
end
session = Capybara::Session.new(:chrome)
I've run into this problem recently and was unable to get the previous answer to work due to my setup being different. I have the following setup:
test_helper.rb
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/reporters'
MiniTest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
application_system_test_case.rb
require 'test_helper'
require 'capybara/rails'
require 'capybara/poltergeist'
require 'fileutils'
require 'selenium-webdriver'
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
#driven_by :poltergeist, options: { js_errors: false } #uncomment if you want to run headless
self.use_instantiated_fixtures = true
#downloads = File.expand_path(Rails.root + 'tmp/downloads')
driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: {prefs:{
'download.default_directory' => #downloads,
'download.prompt_for_download' => false,
'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
} }
end
A typical test:
admin_page_test.rb
require 'application_system_test_case'
include ApplicationHelper
class AdminPageTest < ApplicationSystemTestCase
setup do
visit new_user_session_url
fill_in 'Email', with: 'whatever#whatever.com'
fill_in 'Password', with: 'password'
click_on 'commit'
assert_selector 'h1', text: 'Admin Status Board'
end
I looked everywhere for the correct way to pass the options in the class and finally stumbled on it through reading through the Capybara modules, and trial and error. I think I read almost a hundred posts in various places and none of them worked. Hopefully this helps someone who comes across it.
Since my Chrome driver is set up with options rather than capabilities, I ended up going this route:
Capybara.register_driver :selenium_chrome_headless do |app|
Capybara::Selenium::Driver.load_selenium
browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
opts.args << "--headless"
end
browser_options.add_preference(:download, { prompt_for_download: false, default_directory: DOWNLOAD_PATH })
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end
As inspired by this gist
I'm trying to debug why when running remote webdriver tests on a headless linux host download dialogs are presented in chrome. I believe the chrome version is 45.
Couple of Env Details
Selenium 2.53 (gem)
Selenium 2.53 Server Jar
Chrome Driver 2.21
The framework/Tests are written in Ruby utilizing Capybara for driving web tests. Here is a brief snippet of how the remote driver is initialized.
prefernces = {
:download => {
:prompt_for_download => false,
:default_directory => '/home/john.doe/Downloads/'
}
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome()
caps['chromeOptions'] = {'prefs' => prefernces}
http_client = Selenium::WebDriver::Remote::Http::Default.new
http_client.timeout = 240
options = {
browser: :remote,
url: "http://<server_url>:4444/wd/hub",
desired_capabilities: caps,
http_client: http_client
}
# Returns Remote Driver
Capybara::Selenium::Driver.new(app, options)
I have verified via the hub that the chromeOptions are set, but when a file is downloaded, we're presented with a file dialog prompt.
I've burned the candle looking for a solution to this problem. Thanks for the help and consideration!
try removing the / from the end of the default_directory and also setting directory_upgrade: true. Other than that make sure the browser has permission to write to the selected directory (note also the correct spelling of preferences)
preferences = {
:download => {
:default_directory => '/home/john.doe/Downloads',
:directory_upgrade => true,
:prompt_for_download => false,
}
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {'prefs' => preferences}
)
Here is an example to download a file with Capybara / Selenium / Chrome :
require 'capybara'
require 'selenium-webdriver'
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app,
:url => "http://localhost:4444/wd/hub",
:browser => :chrome,
:desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {
'prefs' => {
'download.default_directory' => File.expand_path("C:\\Download"),
'download.directory_upgrade' => true,
'download.prompt_for_download' => false,
'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
}
}
)
)
end
session = Capybara::Session.new(:chrome)
session.visit "https://www.mozilla.org/en-US/foundation/documents"
session.click_link "IRS Form 872-C"
sleep 20
I'm using Selenium-Webdriver with the ChromeDriver installed as well as PhantomJS to automate my test pack. There are some scenarios that I would prefer to run in browser when my pack is running headless. I can tag my scenario as #selenium but when it loads, it opens up in firefox. How do I get it to open up ChromeDriver when using the #selenium tag please?
I have the following in my env.rb file to run in browser:
if ENV['chrome']
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
:browser => :chrome,
desired_capabilities: {
"chromeOptions" => {
"args" => %w{ window-size=1080,720 }
}
}
)
I set chrome=true to run in browser via cmd.
The solution, following on from Dave McNulla's guidance is as follows:
Before ('#tag') do |scenario|
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
:browser => :chrome,
desired_capabilities: {
"chromeOptions" => {
"args" => %w{ window-size=1080,720 }
}
}
)
end
end
I've set it to open in Chrome, only because it's default download action doesn't open up a pop up window, it goes directly to the downloads folder which is ideal for my tests.
I'm having some trouble working out how to get Capybara (1.1.1) to autoaccept SSL certificates on Chrome.
This is my current Capybara configuration
Capybara.run_server = false
Capybara.app_host = 'http://some.host.com'
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
I have read about the -trustAllSSLCertificates flag but I'm not sure how to apply this when running Selenium using Capybara.
This will do it with firefox:
Capybara.register_driver :selenium do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile.assume_untrusted_certificate_issuer = false
Capybara::Selenium::Driver.new(app, :profile => profile)
end