Unable to upload a file with Selenium - Ruby - 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.

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

How to write a simple Cucumber script

I'm following the tutorial to run my first Cucumber script:
Feature: guru99 Demopage Login
In order to Login in Demopage we have to enter login details
Scenario:
Register On Guru99 Demopage without email
Given I am on the Guru99 homepage
When enter blank details for register
Then error email shown
I have the project in Idea but when I run it I get errors.
When using chrome:
Failed to open TCP connection to 127.0.0.1:9515 (No connection could be made because the target machine actively refused it.
I have no idea how to resolve it.
When using Firefox, the script successfully opens the browser but fails after that:
require 'watir'
require 'colorize'
Selenium::WebDriver::Firefox::Binary.path='C:\soft\Mozilla Firefox\firefox.exe'
case ENV['BROWSER']
when 'chrome'
browser = Watir::Browser.new :chrome
when 'firefox'
browser = Watir::Browser.new :firefox
end
Given(/^I am on the Guru99 homepage$/)do
#browser = Watir::Browser.new :firefox
#browser.goto "http://demo.guru99.com"
end
When(/^enter blank details for register$/) do
browser.text_filed(:name,"emaiid").set("")
browser.button(:name,"btnLogin").click
end
Then(/^error email shown$/) do
puts "Email is Required!".red
browser.close
end
And returns:
NoMethodError: undefined method `text_filed' for nil:NilClass
on this line:
browser.text_filed(:name,"emaiid").set("")
I found some references that I need to write a class to call a method. I tried it but didn't succeed.
Connection refused, I'm unsure but "Watir+Cucumber Connection refused" looks a fix.
Copy pasta:
AfterConfiguration do |config|
yourCodeStartUp() # Put your SETUP code here including the launch of webdriver
at_exit
yourCodeTearDown() # Put your CLOSING routine here
puts 'stopped'
end
end
The code error is a typo, it should be browser.text_field(...
Regarding the issue you are observing on chrome, it sounds like you need to update chromedriver (and make sure the exe is in PATH). If you are running chrome v56-58, you need ChromeDriver 2.29.
Regarding the NoMethodError: undefined method error, you have a typo when you call the text_field method (i.e. browser.text_filed).
Nope, I was mistaken, I had an older version of chromedriver. Now it's running in chrome as well. Thank you very much. Appreciate your time!
So, the answers are:
1. Update chromedriver.
2. Check your code for typos one more time.
Was really easy but took me a lot of time%

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!!

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.

Automating authentication with Watir

I've searched and searched for this, but I can't seem to get this automation to work. Having used all of the basic authentication code on the OpenQA site, I still cannot get the authentication box to work.
I'm using IE8, with a website that has HTTPS enabled.
By using Watir I'm able to open IE to the correct page, but nothing I try allows me to enter any content into the login form.
Here is the code I've whittled it down to:
require 'watir'
url = 'https://thewebsite.com'
#username = 'myusername'
#password = 'mypassword'
browser = Watir::Browser.new
browser.goto url
sleep 5
Watir.autoit.WinWait('Blank Page')
Watir.autoit.Send(#username)
Watir.autoit.Send('{TAB}')
Watir.autoit.Send(#password)
Watir.autoit.Send('{ENTER}')
Does anyone have any suggestions, or links? A lot of the information I've found on the OpenQA site seems quite out of date.
Thanks
Did you try RAutomation instead of autoit?
Have you tried using the url with user and pass in it? like url = 'https://username:password#thewebsite.com', you can try visiting the url manually in a browser, if it works manually it should work in your script too.
I came here having the same problem, although it looks like the answer is different due to the latest version of Watir and watir-webdriver. I'll show what worked for me using:
watir (4.0.2 x86-mingw32)
watir-classic (3.6.0)
watir-webdriver (0.6.2)
Watir doesn't have autoit built in any more and it looks like the other suggestion I found (require 'watir/ie') doesn't work any more either. In the spirit of solving this with the original tech requested:
Make sure after installation AutoIT has been registered with windows.
Go to the AutotIT dll (installed with the rautomation gem mentioned above, think Watir installed this)
cd C:\Ruby193\lib\ruby\gems\1.9.1\gems\rautomation-0.8.0\ext\AutoItX
regsvr32 AutoItX3.dll
Then the code below should work
require 'watir'
require 'win32ole'
$b = Watir::Browser.new :ie
begin
$b.goto( 'http://10.254.157.34:8383/mywebsite/stuff.html');
rescue Exception => e
puts "Trapped Error, expecting modal dialog exception"
puts e.backtrace
puts "Continuing"
end
login_title = "Windows Security" #For Windows 7, dialog title for anything else
username = "myuser"
password = "mypassword"
sleep 1 #Just in case
au3 = WIN32OLE.new("AutoItX3.Control")
win_exists = au3.WinWait(login_title, "", 5)
if (win_exists > 0)
au3.WinActivate(login_title)
au3.Send('!u')
au3.Send(username)
au3.Send('{TAB}')
au3.Send(password)
au3.Send('{ENTER}')
end

Resources