how to show element in browed page in selenium testing? - ruby

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

Related

Watir WebDriver browser object loses current window upon opening new tab (FIREFOX)

I want to test clicking on multiple links on the same page, but then be able to switch active tabs. However once I click on a link with target=_blank or if I open a new tab on purpose, it routes me to a new tab and I can't get back to the old tab.
Note: I am using Firefox for testing. Chrome does appear to work properly and provides a window count of 2
Here is a simple IRB snippet to show what I mean:
2.2.1 :001 > require 'watir-webdriver'
=> true
2.2.1 :002 > b = Watir::Browser.new :firefox, :profile => 'default'
=> #<Watir::Browser:0x..fa8c7116334ddce82 url="about:blank" title="">
2.2.1 :003 > b.goto 'amazon.com'
=> "http://amazon.com"
2.2.1 :004 > b.a(:class => 'nav-logo-link').click(:command, :shift)
=> nil
2.2.1 :005 > b.windows.count
=> 1
I see two tabs opened, which the (:command, :shift) option is meant to do.. but how do I go back to the first one if the browser object only has 1 window?
It just seems that I can't find anyone having the same problem so perhaps its a firefox issue or my environment maybe?
Running on Mac OSX, FF version 44, Ruby 2.2.1, watir-webdriver 0.9.1
Check this out:
require 'watir-webdriver'
b = Watir::Browser.new
b.goto "amazon.com"
link = b.a(:class => 'nav-logo-link')
b.execute_script("return arguments[0].target = '_blank'", link)
link.click
puts b.windows.count
Main idea is not to try to use a link in a special way to get two windows but modify the link to get two windows in a natural way.

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

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