Save File popup issue - ruby

Issue
I have tried all possibilities by setting profile variables, however the Save dialog in firefox is still appearing every time I try to download a file using my automation framework
Content Type of file which is am trying to download is application/csv (for 1 file, rest are mentioned in below code snipit)
Setup -
Firefox Version -
52.8.0 (64 Bit)
Gemfile
source 'https://rubygems.org'
gem 'actionpack', '~> 4.2.4', require: false
gem 'activemodel', '~> 4.2.4'
gem 'Ascii85'
gem 'browserstack-local'
gem 'cucumber', '< 2.0.0'
gem 'cukeforker'
gem 'cukeforker-webdriver'
gem 'headless'
gem 'httparty'
gem 'json'
gem 'mysql2'
gem 'nokogiri'
gem 'pdf-reader'
gem 'pry'
gem 'rubocop', '~> 0.52.1', require: false
gem 'selenium-webdriver', '3.6.0' # Best practice: keep in sync with hub version
gem 'site_prism'
gem 'uuid'
env.rb
Before do
firefox_profile = Selenium::WebDriver::Firefox::Profile.new
firefox_profile['browser.download.dir'] = ENV['ENV_TEMP_PATH']
firefox_profile['browser.download.folderList'] = 2
firefox_profile['browser.download.panel.shown'] = false
firefox_profile['browser.helperApps.alwaysAsk.force'] = false
firefox_profile['browser.download.manager.showWhenStarting'] = false
firefox_profile['browser.helperApps.neverAsk.openFile'] = 'application/csv, application/octet-stream, text/csv, application/zip, application/pdf, application/xml, application/x-x509-ca-cert'
firefox_profile['browser.helperApps.neverAsk.saveToDisk'] = 'application/csv, application/octet-stream, text/csv, application/zip, application/pdf, application/xml, application/x-x509-ca-cert'
firefox_profile['timeout'] = 480000
firefox_profile['pdfjs.disabled'] = true
firefox_profile['resynchronization_timeout'] = 90
firefox_profile['resynchronize '] = true
firefox_profile['dom.max_chrome_script_run_time'] = 0
firefox_profile['dom.max_script_run_time'] = 0
Capybara.default_selector = :css
Capybara.ignore_hidden_elements = true
Capybara.run_server = false
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 240
Capybara.default_driver = :firefox
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: firefox_profile, marionette: false, http_client: client)
end
end
Please note -
I have tried following as well
firefox_profile['browser.helperApps.neverAsk.openFile'] = 'application/csv; application/octet-stream; text/csv; application/zip; application/pdf; application/xml; application/x-x509-ca-cert'
firefox_profile['browser.helperApps.neverAsk.saveToDisk'] = 'application/csv; application/octet-stream; text/csv; application/zip; application/pdf; application/xml; application/x-x509-ca-cert'
Looking for help from experts in our automation active community

Why are you setting both 'browser.helperApps.neverAsk.openFile' and ''browser.helperApps.neverAsk.saveToDisk' to the same mime types? Those are conflicting settings since one is saying to open those file types in the browser and the other is saying to save them to disk (open in browser takes precedence). Also - Firefox 52 was released 2 years ago, may be time to upgrade.
Downloading of files is tested in Capybaras own test suite so you can see the minimal settings required there (obviously adjust mime types as needed) - https://github.com/teamcapybara/capybara/blob/master/spec/selenium_spec_marionette.rb#L13

Related

Ruby Selenium Firefox only downloading to ~/Downloads on MacOS

I am having trouble getting Selenium to download files to the specified folder on MacOS.
Running:
Ruby 3.0.1p64
Rails 6.1.3.1
selenium-webdriver 4.0.0beta3
This is my current code, which has attempts to change the directory in four places (any help much appreciated):
require 'selenium-webdriver'
require 'fileutils'
Selenium::WebDriver.logger.level = :info
#download_dir = File.join(Dir.pwd, "lib/forecastdownloads")
FileUtils.mkdir_p #download_dir # Create the folder if missing
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = #download_dir
profile['browser.download.default_directory'] = #download_dir
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
options.add_argument('--headless')
options.add_preference('download.directory_upgrade', true)
options.add_preference('download.folderList', 2)
options.add_preference('download.prompt_for_download', false)
options.add_preference('download.dir', #download_dir)
options.add_preference('download.default_directory', #download_dir)
options.add_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, text/csv')
driver = Selenium::WebDriver.for :firefox, capabilities: options
driver.manage.timeouts.implicit_wait = 10 # seconds
wait = Selenium::WebDriver::Wait.new(:timeout => 15)
driver.get "https://...[the webpage]"
element = wait.until { driver.find_element(:class, "buttons-excel") }
element.click
driver.quit
With the above code, files are downloading to the regular ~/Downloads folder instead of #download_dir.
The problem is the setting for download.dir.
Instead of File.join(Dir.pwd, "lib/forecastdownloads"), it should be:
profile['browser.download.dir'] = Dir.pwd + '/forecastdownloads'

Sample ''Hola'" Rubygem push error on windows

I am following the very basic tutorial found here: http://guides.rubygems.org/make-your-own-gem/
hola_username.rb:
class Hola
def self.hi
puts "Hello world!"
end
end
hola_username.gemspec:
Gem::Specification.new do |s|
s.name = 'hola_username'
s.version = '0.0.0'
s.date = '2010-04-28'
s.summary = "Hola!"
s.description = "A simple hello world gem"
s.authors = ["Surname Lastname"]
s.email = 'me.me#gmail.com'
s.files = ["lib/hola_username.rb"]
s.homepage =
'http://rubygems.org/gems/hola_username'
s.license = 'MIT'
end
That really is all there is to the project.
I can build my gem with
gem build .\hola_username.gemspec
I have also tested it by importing and executing the hi function of the Hola class and it works:
PS E:\hola_username> gem install .\hola_username-0.0.0.gem
Successfully installed hola_username-0.0.0
Parsing documentation for hola_username-0.0.0
Done installing documentation for hola_username after 0 seconds
1 gem installed
&
irb(main):001:0> require 'hola_username'
=> true
irb(main):002:0> Hola.hi
Hello world!
=> nil
irb(main):003:0>
But when I try to
gem push .\hola_username-0.0.0.gem
I get:
ERROR: While executing gem ... (Psych::SyntaxError)
(): control characters are not allowed at line 1 column 1
Any ideas?
Edit: I am on a windows 10 machine using ruby 2.0.0p598
Edit v01: Anything I put after gem push will result in the above error, doesn't seem to be a problem with the sample rubygem.
Edit v02: My credentials file that was generated in the .gem folder however stars with hex characters: fffe2d002d00.. Which might be the ones causing trouble?
My credentials file in .gem folder was encoded with UCS2 - Little Endian and converting it to UTF without BOM did the trick.
Although I have absolutey no idea why..

Google charts: Unable to open the url of a google chart from ruby file

I want the google chart url to load by itself when I run the ruby file from terminal. When I open the output link in a browser, I am able to load the chart. What is that I am missing.
require 'gchart'
require 'rubygems'
require 'nokogiri'
require 'open-uri'
pie_chart = Gchart.pie(:data => [20,10,15,5,50], :title => 'SDRuby Fu level', :size => '400x200', :labels => ['matt', 'rob', 'patrick', 'ryan', 'jordan'])
puts "#{pie_chart}"
**Terminal output:**
ruby google_charts.rb
http://chart.apis.google.com/chart?chd=s:YMSG9&chl=matt|rob|patrick|ryan|jordan&chtt=SDRuby+Fu+level&cht=p&chs=400x200&chxr=0,20,50
require 'launchy'
Launchy.open("http://www.stackoverflow.com")
The above gem resolved my question

Unable to activate faraday_middleware-0.9.0 // Gem::LoadError // trying to run the pocket-ruby gem

I am trying to run the pocket-ruby gem and after I clone the repo and bundle, it raises this error:
Unable to activate faraday_middleware-0.9.0, because faraday-0.9.0 conflicts with faraday
(< 0.9, >= 0.7.4)
The gemspecfile for the pocket-ruby gem looks like this:
require File.expand_path('../lib/pocket/version', __FILE__)
Gem::Specification.new do |s|
s.add_development_dependency('sinatra', '~> 1.3.3')
s.add_development_dependency('multi_xml')
s.add_runtime_dependency('faraday', ['>= 0.7', '< 0.9'])
s.add_runtime_dependency('faraday_middleware', '~> 0.8')
s.add_runtime_dependency('multi_json', '>= 1.0.3', '~> 1.0')
s.add_runtime_dependency('hashie', '>= 0.4.0')
s.authors = ["Turadg Aleahmad","Jason Ng PT"]
s.description = %q{A Ruby wrapper for the Pocket API v3 (Add, Modify and Retrieve)}
s.email = ['turadg#aleahmad.net',"me#jasonngpt.com"]
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.files = `git ls-files`.split("\n")
s.homepage = 'https://github.com/turadg/pocket-ruby'
s.name = 'pocket-ruby'
s.platform = Gem::Platform::RUBY
s.require_paths = ['lib']
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
if s.respond_to? :required_rubygems_version=
s.rubyforge_project = s.name
s.summary = %q{Ruby wrapper for the Pocket API v3}
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.version = Pocket::VERSION
end
I messed around with changing the versions on the two dependencies in question but didn't have any luck, since I guess I am not entirely sure what the error is saying. Thoughts?
There's a conflict between the two version specifiers - ~> 0.8 means "any version of the form 0.x", but < 0.9 means that 0.9 is not actually allowed.
You'll need to determine which of these need to change - maybe ~> 0.8 should really be ~> 0.8.0, which would specify "any version of the form 0.8.x".
More info on the pessimistic constraint operator (~>) here:
Meaning of tilde-greater-than (~>) in version requirement?

How to scrape a website with the socksify gem (proxy)

I am reading through the documentation of the socksify gem on Rubyforge. I have installed the gem successfully, and I have run this documented code with success to test that my local implementation can replicate it:
require 'socksify/http'
uri = URI.parse('http://rubyforge.org/')
Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http|
http.get(uri.path)
end
# => #<Net::HTTPOK 200 OK readbody=true>
But how do I e.g. scrape 'http://google.com/', and get the html content? I wish to parse it with e.g. Nokogiri like this:
Nokogiri::HTML(open("http://google.com/))
require 'socksify/http'
http = Net::HTTP::SOCKSProxy(addr, port)
html = http.get(URI('http://google.de'))
html_doc = Nokogiri::HTML(html)

Resources