Error while loading win32ole - ruby

I'am trying to read the data from an excel file for automating a website, using RubyMine as my editor and watir-webdriver for automating my test. while doing this I get an Error in my rubyMine editor "cannot load such file -- win32_ole (LoadError)".
require 'watir'
require 'rubygems'
require 'roo'
require 'win32_ole'
browser = Watir::Browser.new :firefox
Given(/^Iam on guru99 site page$/) do
browser.goto "http://demo.guru99.com/v4/index.php"
xl = WIN32OLE.new('Excel.Application')
workbook = xl.Workbooks.open("C:\\Users\TekUser\Desktop.practiceExcel.xlsx")
wrksheet= workbook.Worksheets(1)
wrksheet.Select
username1= wrksheet.Range("a1").Value
password1= wrksheet.Range("b1").Value
end
When(/^I enter Username and password in the respected field$/) do
browser.text_field(:name, "uid").set(username1)
browser.text_field(:name,"password").set(password1)
end
Then(/^I click on login button$/) do
browser.button(:name,"btnLogin").click
end

The library name does not have an underscore. It should be:
require 'win32ole'

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.

watir get console error

How I can check browser console if there are any error?
I'm following the watir ubuntu tutorial:
require "selenium-webdriver"
browser = Selenium::WebDriver.for :firefox
browser.get "http://watir.com"
Easy , add this lines at the end
console_logs = browser.driver.manage.logs.get(:browser)
puts console_logs

Capybara - visit() not working with firefox

I'm a noob in cucumber and capybara, so please bear with me.
I'm trying to setup a cucumber project that uses chrome and firefox as the test platform. So far, I have got the test to work on chrome, but not on firefox.
Below is the code snippet:
require 'uri'
require 'net/http'
require 'fileutils'
require 'selenium-webdriver'
require File.dirname(__FILE__) + '/throttle.rb'
#CAPYBARA
require 'capybara/cucumber'
require 'capybara/session'
#require 'capybara-webkit'
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.register_driver :selenium_firefox do |app|
Capybara::Selenium::Driver.new(app, :browser => :firefox)
end
driver = case ENV['BROWSER']
when 'chrome'
:chrome
when 'firefox'
:selenium_firefox
when 'webkit'
:webkit
when 'ie'
:internet_explorer
else
:chrome
end
Capybara.default_driver = driver
Capybara.javascript_driver = :selenium_firefox
Capybara.run_server = false
Capybara.default_selector = :css
Then, for the test, I just simply did a visit("http://www.google.com").
If I set my browser parameter to chrome, it worked fine. Cucumber openned up chrome and automatically typed in "www.google.com", and the page loaded without a problem.
However, the moment I set it to firefox: $cucumber features/test.feature BROWSER=firefox, it didn't work. It did open up the firefox browser, but nothing was being automatically typed into the URL box.
So, I must be missing something in the setup process that I'm not aware of. I have been looking for solutions on the web (require 'selenium webdriver', put 'selenium webdriver' into my Gemfiles, etc.), but so far none has worked for me.
What am I missing here? How come visit() didn't automatically populate the URL box of firefox, but it did just fine with chrome?
Thank you for your help.

Watir Webdriver Load Chrome Extension

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.

Resources