Watir Webdriver Load Chrome Extension - ruby

I'm trying to load a chrome extension with Watir, and I'm having issues.
I found this related question: Ability to launch chrome with extensions loaded with watir-webdriver. However, I am still having the same issue after following that.
require 'rubygems'
require 'watir-webdriver'
require 'ruby-debug'
require 'nokogiri'
browser = Watir::Browser.new :chrome, :switches => %w[--load-extension=~/.config/google-chrome/Default/Extensions/anepbdekljkmmimmhbniglnnanmmkoja/0.1.12_0]
sleep(10)
browser.close
I also tried copying the extension from /Extensions to /Desktop and loading from there to no avail.
The error I get is Could not load extension from ... Manifest File Missing or Unreadable.
The Manifest file does indeed exist and seems to be a correct file in JSON format.
Trying to load different extensions fails as well.

Download the chrome extension crx file,
Store the args or any other option need to pass in the watir_opts hash
watir_opts[:extensions] = ['path of *.crx file']
browser = Watir::Browser.new :chrome, options: watir_opts
This worked for me.
Note: I didn't encode using 'base64' gem

If you pack the extension and then base64 it, you can load it into the Chrome browser right from your ruby code.
Pack your extension into a *.crx file. You can follow this guide, or just google how to pack a chrome extension.
Base64 it then add it to your desired capabilities list. You could use some code similar to this one:
chrome_extensions = []
chrome_extension_path = '\home\user\packed_chrome_extension.crx'
begin
File.open(chrome_extension_path, "rb") do |file|
chrome_extensions << Base64.encode64(file.read.chomp)
end
rescue Exception => e
raise "ERROR: Couldn't File.read or Base64.encode64 a Chrome extension: #{e.message}"
end
# Append the extensions to your capabilities hash
my_capabilities.merge!({'chrome.extensions' => chrome_extensions})
desired_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(my_capabilities)
browser = Watir::Browser.new(:remote, :url => 'http://localhost:4444/wd/hub' :desired_capabilities => desired_capabilities)
And don't forget to require 'base64' too.
The example is for a remote web-driver instance, but I think it should work when using web-driver locally too. Just adjust the arguments passed to Watir::Browser.new.

Related

how to show element in browed page in selenium testing?

I tried this to do selenium testing
require 'watir'
b = Watir::Browser.start 'http://www.gmail.com'
t = b.text_field id: 'entry_1000000'
t.exists?
t.set 'your name'
t.value
but not fetching any text in browed page(www.gmail.com).
require 'watir'
b = Watir::Browser.start 'http://www.gmail.com'
t = b.text_field id: 'entry_1000000'
t.exists?
t.set 'your name'
t.value
put exact id of that text field . id or xpath or name etc
First lets check if you have whats needed for that script to run, and start from there if not.
Ruby installed
Watir gem installed
Browser installed (chrome or firefox)
Geckodriver downloaded and in path (if firefox installed)
Chromedriver downloaded and in path (if chrome installed)
After that, save the code below in file, lets name it gmail.rb.
require 'watir'
b = Watir::Browser.new :chrome #or :firefox
b.goto "www.gmail.com"
b.text_field(:id => 'identifierId').set "your_email#gmail.com"
b.span(:text => "Next").click
b.text_field(:name => "password").set "your_password"
b.span(:text => "Next").click
Open command prompt or terminal and run the following command from folder where gmail.rb is located
ruby gmail.rb
Browser should open, navigate to gmail, input email, password and submit it, but from there gmail security kicks in so this is not a good use case for automation, at least not this way.
You can try the code above and see how it works, and if not, post errors here. But if you actually need to automate reading gmail, there is a really nice gem that helps you do that https://github.com/gmailgem/gmail

Unable to upload a file with Selenium - Ruby

I am trying to upload a file using a bot written in Ruby. It failed but I think my synthax to be good. I suppose that the recent upgrade of Firefox unabled me to do the job. Have you got an idea please ?
I used these resources to do my code :
https://saucelabs.com/resources/articles/best-practices-tips-selenium-file-upload
http://elementalselenium.com/tips/1-upload-a-file
I got the last version of selenium here :
https://rubygems.org/gems/selenium-webdriver/versions/2.53.0?locale=fr
I got the last version of geckodriver here :
https://rubygems.org/gems/geckodriver-helper/versions/0.0.3
But it didn't work : WebDriverError#chrome://marionnette/content/error.js:235:5 File not found
I am using wordpress in local mode with local by flywheel.
Firefox Quantum 57.0 64bits
Here is my little code :
require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers
def setup
#driver = Selenium::WebDriver.for :firefox
end
def teardown
#driver.quit
end
def run
setup
yield
teardown
end
run do
#driver.get "http://mywebsite.dev/wp-admin/profile.php"
inputlogin = #driver.find_element :id => "user_login"
inputpwd = #driver.find_element :id => "user_pass"
inputlogin.send_keys "mylogin"
inputpwd.send_keys "mypwd"
#driver.find_element(:id, "wp-submit").click
#driver.get "http://mywebsite.dev"
element = #driver.find_element(:id, 'uploadInput1')
element.send_keys "C:\\path\\image.jpg"
# #driver.find_element(id: 'btn-submit').click
puts "Successful on #{#driver.title}"
end
As it didn't work, I was looking for an other way to upload my file so I tried using AutoIT but I failed... Here is an example.
http://www.maisasolutions.com/blog/How-to-upload-images-using-selenium-webdriver-with-ruby
Moreover, this solution is very heavy (downloads, installations, non reproductability etc.) and don't seems to be proper code.
Thank you for your help.
Find a file field on the page and attach a file given its path.
The file field can be found via its name, id or label text.
page.attach_file(locator, '/path/to/file.png')
Here, locator will beCSS id or class of file upload path.

Why does Watir-webdriver open two browsers?

I'm working on website test automation using Cucumber/Ruby/Selenium-Webdriver/Capybara. I want to switch to Watir-Webdriver in combination with Cucumber and Ruby, but I'm struggling with the following:
Every time I run my cucumber test, Watir opens two browser windows, a blank screen to the site I configurated as default, plus another in which the actual test steps are executed.
My 'Support/env.rb' file has:
require 'allure-cucumber'
require 'rspec'
require 'watir-webdriver'
AllureCucumber.configure do |c|
c.output_dir = 'D:\Test\result'
c.clean_dir = true
c.tms_prefix = '#PRACTEST--'
c.issue_prefix = '#JIRA++'
c.severity_prefix = '#URGENCY:'
c.tms_prefix = ''
end
My steps file begins with:
require 'watir-webdriver'
require 'cucumber'
require 'rspec'
require_relative 'D:\EntelTest\src\PageObject\home_page.rb'
Before do
#test = AbstractPage.new(Watir::Browser.new :ff)
#test.full_size
end
After do
#test.quit
end
home_page = nil
When(/^Go to home page$/) do
home_page = #test.goToHomePage
end
Can you put these before do and after do in hooks.rb? In the steps.rb file, just mention the code for your cucumber steps, and before that declare browser = Watir::Browser.new :ff
The best practice is to put it in hooks.rb. env.rb usually should consist the desired capabilities plus server environment codes. :)
What you have put in step file should go in hooks.rb file.
Please install gem called testnow.
It will help you to create most standard and easy to use watir-webdriver framework with all pre-configured browsers.
Steps:
1) gem install testnow
2) testnow watir_cucumber_now
It will as you to install dependecies, enter Y to set it up completely.
It will create robust framework with a sample scenario.
Just run the sample scenario with any of the following commands.
rake testnow BROWSER=firefox
rake testnow BROWSER=chrome
rake testnow BROWSER=opera
This will only work provided you have browsers pre-installed and webdriver present in PATH variable.
Please comment for more information.
Hope it helps!!

Unable to automate (AutoIT) in Ruby because of Watir error

I've tried a few times now to run a Watir browser and then use the AutoIt ruby library (au3) to access a right click context menu but it wasn't working, turns out the au3 library is disappearing for some reason (I'm a little new to Ruby but when I require it again after the browser opens it comes back false for some reason:
irb(main):001:0> require "au3"
=> true
irb(main):002:0> require "watir-webdriver"
=> true
irb(main):003:0> browser = Watir::Browser.new :chrome
Starting ChromeDriver (v2.3) on port 9515
[4868:5640:1025/104947:ERROR:textfield.h(176)] NOT IMPLEMENTED
=> #<Watir::Browser:0x449008c8 url="about:blank" title="about:blank">
<to "https://github.com/lmmx/watir-paper-scanner/blob/master/bookworm.rb"
[WARNING:..\..\..\..\flash\platform\pepper\pep_module.cpp(63)] SANDBOXED
=> "https://github.com/lmmx/watir-paper-scanner/blob/master/bookworm.rb"
irb(main):005:0> require "au3"
=> false
irb(main):006:0>
I'm guessing that whatever's SANDBOXED is crucial to running au3? The browser still works fine... Will try and update the watir-related things, but I only installed it a week or 2 so it shouldn't be out of date already - anyone help me fix it?
The reason it's returning false is because you have already used
require "au3"
on the top line of your code, so when you require it again it's already there.

how to download file to a specific location on .click of watir?

when I click on a link of a web page in chrome web browser with ruby watir
ie.input(:id, "c_ImageButton_Text").click
.text file start downloading. I want to download it to a specific location.I tried this code,but still getting.text file in default download location.How can I download .text file to specific location?
download_directory = "#{Dir.pwd}/downloads"
download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory
b = Watir::Browser.new :chrome, :profile => profile
click here for this code
For latest watir version 6.6.1
prefs = {
prompt_for_download: false,
default_directory: Rails.root.join('tmp').to_s
}
options = Selenium::WebDriver::Chrome::Options.new
options.add_preference(:download, prefs)
browser = Watir::Browser.new :chrome, options: options
As the old saying goes, "There is more than one way to skin a cat":
You can certainly try to skin it the way you are attempting to do in your example code. However, there are a number of issues with this course of action. Namely, it is browser specific.
I really like how Elemental Selenium suggests achieving valid tests and assertions on downloading files by using the rest-client gem to curl the file's URI and assert the expectations of the file from from there: http://elemental-selenium.com/tips/8-download-a-file-revisited

Resources