Ruby - Selenium, disabling "reload page" - ruby

I am using chromium with selenium and recently I have a lot of these "reload page" popups.
def setup_selenium
require "selenium-webdriver" # load in the webdriver gem to interact with Selenium
require 'rubygems'
prefs = { # DISABLES GOOGLE PDF PREVIEWER PLUGIN.
download: {
'prompt_for_download' => false,
'default_directory' => 'location'
},
plugins: {
'always_open_pdf_externally' => true
}
}
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("-incognito");
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-translate')
options.add_argument('--ignore-certificate-errors')
Selenium::WebDriver.for :chrome, options: options, prefs: prefs
Even tho I have the --disable-popup-blocking, the popups still appear. I have seen many of these posts about the same problem, but non have given me the answer I need.
Gemfile
ruby '2.7.4'
gem 'headless', '~> 2.2', '>= 2.2.3'
gem 'selenium-webdriver', '~> 2.53', '>= 2.53.4'
Thank you.

Related

Watir won't download PDF, only opens in viewer

I was trying to test with Selenium, but can not download a pdf, pdfs keep opening.
See my other post: RUBY: Selenium webdriver, setup to download pdf files instead of opening them
It was advised to try Watir, so I did, and I get the same result. Here is my Watir setup. Please advise on how to fix this issue.
require 'watir'
require 'pry'
prefs = {
download: {
prompt_for_download: false,
default_directory: '/Users/ar/pdf_downloads'
}
}
browser = Watir::Browser.new :chrome, options: {prefs: prefs}
# Goto Login Page (file)
url="file:///Users/ar/info.html"
browser.goto url
browser.button(id: 'formsubmit').click
sleep 5
# Goto info
info_url = 'https://webapp.domain.com/info'
browser.goto info_url
sleep 5
elements = browser.elements(css: "#ar-pdfreport a")
link = elements.first.attribute("href")
browser.goto link
There is a bug in Selenium-WebDriver v3.142.7 where using symbols for the prefs generates the wrong result - eg does not set the download directory. See https://github.com/SeleniumHQ/selenium/issues/7917 for more details.
Switch the symbols to Strings:
prefs = {
download: {
'prompt_for_download' => false,
'default_directory' => '/Users/ar/pdf_downloads'
},
plugins: {
'always_open_pdf_externally' => true
}
}

Problems initialising browser with options using Ruby, Watir, Chrome

I have a few web-scraping scripts that I've been using for a while now that have been working without issue. However because of an update of something somewhere (I think chrome+chromedriver), the the browsers are not loading with the preferences/options I specify.
Current code:
preferences = {
:download => {
:prompt_for_download => false,
:directory_upgrade => true,
:default_directory => 'C:/DownloadFolder/',
}
}
args = ['--disable-infobars']
browser = Watir::Browser.new :chrome, :chrome_options => {:detach => true, :prefs => preferences, :args => args}
The problems I'm noticing are that the '--disable-infobars' and download folder location are not being applied.
ruby version: 2.3.3p222
watir version: 6.16.5
selenium webdriver version: 3.142.3
chrome version: 75.0.3770.100
chromedriver version : 75.0.3770.90
Taken from help I got elsewhere:
options = Selenium::WebDriver::Chrome::Options.new.tap do |o|
o.add_argument('--disable-infobars')
o.add_preference(:download, directory_upgrade: true,
prompt_for_download: false,
default_directory: 'C:\\DownloadFolder\\')
o.add_option(:detach, true)
end
browser = Watir::Browser.new :chrome, options: options
Two things solved the problem. 1 is specifying the options through selenium rather that Watir. The other is no longer being able to use single forward slashes in folder paths.

Getting Net::ReadTimeout: visiting a website in Ruby Capybara Cucumber

I'm running automated tests using Ruby/Cucumber/Capybara/Chromedriver, but i get an error while running a simple test scenario. I tried to solve problem by updating version of Ruby, Capybara, Cucumber, Chromdriver and Chrome. I need your help. Here is the details:
Try to:
visit 'http://www.google.com'
Getting:
WARNING: The formatter Teamcity::Cucumber::Formatter is using the deprecated formatter API which will be removed in v4.0 of Cucumber.
2018-04-16 14:32:43 WARN Selenium [DEPRECATION] :args or :switches is deprecated. Use Selenium::WebDriver::Chrome::Options#add_argument instead.
Net::ReadTimeout: Net::ReadTimeout
./features/step_definitions/common_steps.rb:46:in `/^testing$/'
Chrome Version:65.0.3325.181
Chrome Driver: 2.37
cucumber: 3.1.0
capybara: 3.0.1
ruby: 2.2.6
When I removed "--disable-extensions" from env.rb file as you see below it works fine..
Capybara::Selenium::Driver.new(app, :browser => :chrome, :switches => %w[--disable-extensions --disable-web-security --start-maximized])
Capybara::Selenium::Driver.new(
app,browser: :chrome,
desired_capabilities: {
'chromeOptions' => {
'useAutomationExtension' => false,
'args' => ['--disable-web-security', '--start-maximized', '--disable-infobars']
}
}
)
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 120
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {args: %w[headless disable-gpu disable-popup-blocking window-size=10_000,1080 log-level=3]}
)
Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: capabilities,
http_client: client)
end

Download location Selenium-webdriver Cucumber Chrome

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

Block images with chromium watir-webdriver

I'm trying crawl along some pages with watir-webdriver and chromium. I haven't got any success by googling arround so here is my question:
I don't need any images so, to speed up things, I try to disable image loading.
Using firefox as my browser is relatively straigtforward
profile = Selenium::WebDriver::Firefox::Profile.new
profile['network.image.imageBehavior'] = 2
browser = Watir::Browser.new :firefox, :profile => profile
But I haven't had any success with chromium. As far as I've learned, you can set preferences and pass commandline options this way (an example):
prefs = {
:download => {
:prompt_for_download => false,
:default_directory => '/tmp'
},
}
args = ['--start-maximized', '--incognito']
browser = Watir::Browser.new :chrome, :prefs => prefs, :args => args,
That works ok. The problem is that, AFAIK, there are no commandline options nor preferences to block images in chromium.
Any idea?
My setup:
LinuxMint 16 (32.0.1700.107-0ubuntu0.13.10.1~20140204.972.1)
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
watir-webdriver v0.6.8
chromedriver v2.9.248304
chromium-browser 32.0.1700.107 Built on Ubuntu 13.10
I've managed to disable image loading and many more things through a proxy, being any the choosen browser driver.
I used a local proxy called privoxy in my case.
prefs = {
:profile => {
:managed_default_content_settings => {
:images => 2
}
}
}
b = Watir::Browser.new :chrome, :prefs => prefs

Resources