I am new in using watir but I can only imagine this to be a bug:
require "watir"
Watir::Browser.default = 'safari'
b = Watir::Browser.new
b.goto()
=> nil
b.link(:title, "Start").click
Leads me to the next page as expected but on the following page no link works even they are there
b.link(:title, "Do something").exist?
=> true
When I then enter
b.link(:title, "Do something").click
nothing happens, even the href attribute of course links to the next page (to be more precise, it is the same page but different request parameters)
Identifying the link with :xpath looks similar.
The same thing works fine with "firefox" as browser.
I use Safari 5.0.1, ruby 1.8.7 and installed beside others safariwatir-0.3.8
In fact controlling safari partially works but I have no glue why it hangs on the second page. Here is an example using a scenario in the internet:
this works fine:
browser.goto("http://reiseauskunft.bahn.de/bin/query.exe/dn?S=Frankfurt(Main)Hbf&Z=Nrnberg%20Hbf&date=%2B30&time=1500&start=1&")
=> nil
this also works fine - the link does exist:
browser.link(:title, /Mit Tickets zum Normalpreis/).exist?
=> true
this does not work - the browser window stays unchanged:
browser.link(:title, /Mit Tickets zum Normalpreis/).click
=> nil
I would like to try fire_event or fireEvent but irb says invalid method. I guess the hints I found about firing an event are outdated?
As far as I know, the only way to drive Safari is to use safariwatir gem (works only on Mac OS X). Try this (from SafariWatir wiki page at Watir wiki):
require 'rubygems'
require 'safariwatir'
browser = Watir::Safari.new
browser.goto("http://google.com")
Related
Capybara, a web testing library for Ruby, has a method Capybara::Session.save_and_show_page that brings up the current web page in the browser. This appears to work asynchronously--the web page is shown some time after the call is made. That's fine, but if the test ends too quickly after the call to #save_and_show_page, then no web page is shown.
Problem code
Given this Gemfile:
source "https://rubygems.org"
gem "launchy"
gem "poltergeist"
Then this code does not display a web page:
require "capybara/poltergeist"
session = Capybara::Session.new(:poltergeist)
session.visit("http://www.google.com")
session.save_and_open_page
Workaround
Adding a brief sleep at the end of the code causes the web page to be displayed, as desired:
require "capybara/poltergeist"
session = Capybara::Session.new(:poltergeist)
session.visit("http://www.google.com")
session.save_and_open_page
sleep 1
# => Opening "/home/wayne/homesmart/franchise-manager-qa/capybara-201610141504469160970532.html" with Google Chrome (text/html)
# => [10253:10281:1014/150446:ERROR:nss_util.cc(809)] After loading Root Certs, loaded==false: NSS error code: -8018
# => Created new window in existing browser session.
How can I cause Capybara's #save_and_open_page method to always display a page, without having to insert a sleep after calling the method?
Versions
Ruby 2.3.1p112
capybara 2.10.1
launchy 2.4.3
poltergeist 1.11.10
PhantomJS 2.1.1
Google Chrome 53.0.2785.143 (64-bit)
Linux 3.16.0-4-amd64 #1 SMP Debian
I'm trying to (ab)use the capybara web testing framework to automate some tasks on github that are not accessible via the github API and which require me to be logged in and click on buttons to send AJAX requests.
Since capybara/selenium is a testing framework it helpfully creates a temporary session which has no cookies in it. I'd like to either stop it from doing that, or else I'd like to know how to load my cookie store into the browser session that it creates.
All I'm trying to do is this:
#!/usr/bin/env ruby
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "https://github.com"
Or this:
#!/usr/bin/env ruby
require 'capybara'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
session = Capybara::Session.new(:selenium)
session.visit "https://www.github.com"
In both cases I get the github.com landing page you'd see as a logged-out user or incognito mode in the browser. I'd like to get my logged-in landing page like I just fired up a web browser myself and navigated to that URL.
Since I have 2FA setup on github that makes automating the login process from the github landing page somewhat annoying, so I'd like to avoid automating logging into github. The tasks that I want to automate do not require re-authenticating via 2FA.
ANSWER:
For MacOSX+Ruby+Selenium this works:
#!/usr/bin/env ruby
require 'selenium-webdriver'
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"debuggerAddress" => "127.0.0.1:20480"}, detach: false)
driver = Selenium::WebDriver.for :chrome, :desired_capabilities => caps
driver.navigate.to "https://github.com"
Then fire up chrome with this:
% /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=/Users/lamont/Library/Application\ Support/Google/Chrome --profile-directory=Default --remote-debugging-port=20480
Obviously the paths will need to be adjusted because they're OSX-centric and have my homedir in them.
There is also a bug in the selenium-webdriver gem for ruby where it inserts a 'detach' option which gets into a fight with 'debuggerAddress':
/Users/lamont/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok': unknown error: cannot parse capability: chromeOptions (Selenium::WebDriver::Error::UnknownError)
from unknown error: unrecognized chrome option: detach
The lib/selenium/webdriver/chrome/bridge.rb file can be edited to take that out as a quick hack:
chrome_options['binary'] = Chrome.path if Chrome.path
chrome_options['nativeEvents'] = true if native_events
chrome_options['verbose'] = true if verbose
#chrome_options['detach'] = detach.nil? || !!detach
chrome_options['noWebsiteTestingDefaults'] = true if no_website_testing_defaults
chrome_options['prefs'] = prefs if prefs
To implement something similar in Ruby, check out this page that goes over that. Thanks to lamont for letting me know in the comments.
You can start chrome using a specific Chrome profile. I am not sure what the ruby implementation would look like, but in python it looks something like:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
options = ChromeOptions()
# more on this line here later.
options.add_experimental_option('debuggerAddress', '127.0.0.1:7878')
driver = webdriver.Chrome(chrome_options=otpions)
In order for this to work you need to do a few things.
manually start chrome from terminal/command prompt with these command line arguments
--user-data-dir=/path/to/any/custom/directory/home/user/Desktop/Chromedir --profile-directory="Profile 1" --remote-debugging-port=7878
make sure "Profile 1" is already existing in the same --user-data-dir (make sure user Profile 1 has necessary chrome://components/
to run any apps that require those components)
you can use any free port in place of 7878
verify that http://localhost:7878 is running and returns value.
This should manually launch chrome with the "Profile 1" profile, and so long as it has logged into the site in question, it will stay logged in like a normal user so long as you follow these instructions to run the tests.
I used this to write a quick netflix bot that clicks the "continue playing" button when it pops up, and it's the only way to get DRM content to play as far as I have found. But it retains the cookies for the login, and also launches chrome with whatever components the profile is set up to have.
I have tried launching chrome with specific profiles before using different methodologies, but this was the only way to really force it to work how I wanted it to.
Edit: There are methods for saving cookie info as well although I don't know how well they work. Check out this link for more info, as my solution is probably not the best solution even if it works.
The show_me_the_cookies gem provides cross-driver cookie manipulation and can let you add new cookies. The one thing to be aware of when using selenium is that you need to visit the domain before you can create cookie for it, so you'll need to do something like
visit "https://www.github.com"
create_cookie(...)
visit "https://www.github.com"
for it to work - first visit just puts the browser/driver in a state where you can create the cookie, second visit actually goes to the page with the cookies set.
I had to tweak the OP's answer (from within her question) to get this going with Ruby in 2022.
Prerequisites
Chromedriver installed and allowed to run even though it's not signed:
> brew install chromedriver
> xattr -d com.apple.quarantine /usr/local/bin/chromedriver
Chrome launched and accepting commands on a specific port:
> /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=~/Library/Application\ Support/Google/Chrome --profile-directory=Default --remote-debugging-port=20480
This created a new profile in Chrome so I signed in to my account and got the browser set up, ready to start interacting with the (legacy EdTech) site I'm trying to automate.
Actual use
require 'selenium-webdriver'
caps = Selenium::WebDriver::Remote::Capabilities.chrome("goog:chromeOptions" => {"debuggerAddress" => "127.0.0.1:20480"})
driver = Selenium::WebDriver.for :chrome, capabilities: caps
driver.navigate.to "https://www.google.com"
I've written some Ruby code (connected with Cucumber) that will go to a website and click a file that I'd like to download. The browser I'm using for this is Google Chrome.
Typically, when you go to download a file in Chrome, it doesn't ask for permission. However, when I run the code I made, it says:
"This type of file can harm your computer. Do you want to keep file_name.exe anyway?" It gives 2 options, "keep" or "discard". I have to click keep.
Obviously, you don't want all executables to just start downloading; however, this particular website/file should always be trustworthy.
Is there a command in Ruby or Cucumber that allows you to click the "keep" button automatically? This could just be a general "click at this pixel" or something. Or is there a way to mark a particular website in Chrome as safe. You can't inspect the element because it's not part of the website, but, instead, part of the browser. Preferably without having to download other software.
With this being said, this suggests that if it is possible, it should also be possible to automate an installation (as in clicking next -> next -> etc) for you. Hopefully this is correct?
Thanks in advance.
You can implement it in any browser. But, for Google Chrome, here is the solution -
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = "Absolute or relative path to your download directory"
browser = Selenium::WebDriver.for :chrome, :profile => profile
You haven't specified which gem you use for browser. But, even if you use watir-webdriver, you can use the same profile you created above with watir-webdriver.
browser = Watir::Browser.new :chrome, :profile => profile
I actually switched to using Sikuli, which worked pretty well. Thanks for the help, though.
Do you really need or want the browser to download the file? Are you really testing the browser's download feature, or do you want to verify that the server can serve the file and that it is what you expect?
I found the idea of setting up a default directory and having to check for the file clumsy, fragile and prone to errors, especially when setting up on a new host, especially for tests that run in multiple browsers.
My solution is to just use Ruby (or whatever language) features to download the file directly, and then validate that it is the file it's supposed to be. I'm not testing the browser, I'm testing the software. The only exception to that idea I can think of is if you use some javascript logic or something browser-dependent to redirect you to a link, but please don't ever do that.
However, you run into a problem if you have to log in to access your file; you either have to implement auth in your Ruby code, which isn't technically part of your Cucumber specification, or you need the cookies. I use this code to copy the cookies to avoid logging in again, and grab the file:
def assert_file_link(uri, filename, content_type)
f = open_uri_with_cookies uri
attachment_filename = f.meta["content-disposition"].sub("Attachment;filename=", "") # "Attachment;filename=Simple Flow - Simple Form.rtf"
content_length = Integer(f.meta["content-length"])
assert(f.status == ["200", "OK"], "Response was not 200 OK")
assert(f.content_type == content_type, "Expected content-type of '#{content_type}' but was '#{f.content_type}'")
assert(attachment_filename == filename, "Expected filename of '#{filename}' but was '#{attachment_filename}'")
assert(content_length > 0, "Expected content-length > 0 but was '#{content_length}'")
end
def open_uri_with_cookies(uri)
# hack the cookies from the existing session so we don't need to log in!
cookies = ""
#driver.manage.all_cookies.each { |cookie| cookies.concat("#{cookie[:name]}=#{cookie[:value]}; ") }
if block_given?
open(uri, "Cookie" => cookies, :proxy => nil) do |f|
yield f
end
else
open(uri, "Cookie" => cookies, :proxy => nil)
end
end
Hope this helps.
I need to do an automated script that fills two text fields and clicks a button on a web page, and stores all resulting text to a string variable.
I know how to do this with Watir, but the problem is that this script will be running on a Windows server (with no physical monitor attached).
So this needs some kind of "emulated browser" without actual browser window... I have never before done anything like this, but after google search I think that Ruby gems "mechanize" or "capybara" might be able to do the trick.
But because I don't have any experience with either capybara or mehcanize, I'm asking a little help here...
Here is what I'm trying to do, written in Watir code. I would really appreciate it if someone could tell me how to do the same thing with either "mechanize" or "capybara". Or, if there is some other way to do this, all suggestion are welcomed:
require "watir"
ie = Watir::Browser.new
ie.goto "http://www.vero.fi/vere/Tarkistus/VeronumeronTarkistus.aspx/"
ie.text_field(:id, "tbNimi").set "John Smith"
ie.text_field(:id, "tbVerotunnus").set "123456789012"
ie.button(:id, "btnHae").click
info = ie.text
You could also use Celerity. It drives headless browser using Watir API.
Can Selenium Help With with Selenium Server Running and Firefox Running in headless state
I have basically wrote an article in this over here
Hope this help
I use PhantomJS for this (with the Capybara driver poltergeist). It runs a headless WebKit (Safari and Chrome's browser engine) and Capybara tells it what to do. It's the simplest-to-setup implementation of this concept that I've found.
The code would look something like:
visit "http://www.vero.fi/vere/Tarkistus/VeronumeronTarkistus.aspx/"
fill_in "tbNimi", :with => "John Smith"
fill_in "tbVerotunnus", :with => "123456789012"
click_on "btnHae"
info = page.html
I'm testing this on ark.com..
the following code works perfectly for firefox with watir-webdriver but gives me an error for google chrome and opera browsers respectively.
heres the code:
require "rubygems"
require "watir-webdriver"
b = Watir::Browser.new :ff
b.goto "http://www.ark.com"
# Signing in
7.times{b.link(:text, "Sign in").flash}
sleep 3
b.link(:text, "Sign in").click
sleep 3
# Popup
b.text_field(:name, "login_email").set "email#gmail.com"
b.send_keys :tab
b.text_field(:name, "login_password").set "password"
b.button(:value, "Sign in").click
puts b.title
changing the code as follows gives me errors:
b = Watir::Browser.new :chrome
or
b = Watir::Browser.new :opera
I get the following error message:
You may only interact with visible elements (Selenium::WebDriver::Error::ElementNotVisibleError)
I've tried the following stack overflow suggestion to no avail How do I use Watir::Waiter::wait_until to force Chrome to wait?
Also, my code works perfectly in firefox but not in other browsers, why might this be the case?
Any help would be appreciated. Thanks.
As mentioned in some other posts, you can full screen the browser with the following:
screen_width = b.execute_script("return screen.width;")
screen_height = b.execute_script("return screen.height;")
b.driver.manage.window.resize_to(screen_width,screen_height)
b.driver.manage.window.move_to(0,0)
Then like anonygoose said, the sign in link should be there (assuming your screen isn't too small).
Looking at the website in both Chrome and Firefox I'd say there's some sort of bug or unwanted feature going on with it.
If the web browser you're using is too narrow in width the sign-up button disappears completely.
I'd say that's why you're getting strange errors.
Test this by maximizing the Chrome browser as soon as it launches. From my testing this allows the signup button to be found.
I am not sure of a way to maximize either browser by default, but perhaps someone else on here will know or be able to suggest a way to go about it.