Watir script timeout problems with Firefox browser - ruby

I am new to Watir and I have this weird situation with AJAX based webapp. Application might render a notification window over the page. This notification is a JS based modal window. If I click or mouse-over the notification it vanishes.. So somewhere in my script I have:
#browser = Watir::Browser.new :firefox
...
notf = notification
notf.click if notf
and the method to get notification is this:
def notification
if browser.div(:class => "popupContent").present?
Notification.new(browser.div(:class => "popupContent"))
end
end
Script is running fine with IE and Chrome but with Firefox I get after 60sec 'Timeout:Error' for the if statement.. When I changed code this way:
def notification
begin
browser.div(:class => "popupContent").wait_until_present(1)
Notification.new(browser.div(:class => "popupContent"))
rescue Exception
puts "timeout..."
end
end
Chrome and IE work fine are working fine - just adding up 1 sec delay in case notification is not present.. But Firefox is still having 60sec timeouts in case notification is not present?!? What am I doing wrong - do I need to set/check some Firefox settings?
I have this configuration:
- Win7 OS with Firefox 17.0.1
- Ruby 1.9.3p125
- watir-webdriver (0.6.1)
- selenium-webdriver (2.26.0)
Thank you for your help!

As mentioned in the comments, the solution is to upgrade to the latest version of selenium-webdriver (2.27.2).

Related

save_and_show_page does not show page if test ends too quickly

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

send Ctrl-S to browser in Capybara Webkit test

I have JavaScript in my application that submits a form when the user hits Ctrl-S or Cmd-S. I want to write an automated test for this using RSpec, Capybara, and Capybara Webkit. I don't think I can just have Capybara execute JavaScript to trigger Ctrl-S/Cmd-S because that's not normally allowed with JavaScript in Chrome as a security concern. I see with Selenium there are page.driver.browser.action.key_down/key_up methods available. Is there anything similar with Capybara Webkit? If not, how can I send Ctrl-S and Cmd-S to the browser in my test?
Edit: I also can't get this to work using the regular Selenium driver with Firefox:
describe 'edit a template and hit Ctrl-S', js: true do
render_views
it 'saves the template' do
visit my_path
page.execute_script("$('#hidden_textarea').val('Fabulous new content')")
builder = page.driver.browser.action
builder.key_down(:control).send_keys('s').key_up(:control).perform
expect(page).to have_text('Record was saved.')
expect(page).to have_text('Fabulous new content')
end
end
It looks like the builder.key_down(:control).send_keys('s').key_up(:control).perform isn't doing anything--the page loads in Firefox but just sits there. This is with Firefox 19 on OS X with selenium-webdriver 2.35.1.
Any suggestions on how to get this to work in either Firefox or Chrome, with either Selenium or Capybara Webkit?
I'm trying to do a similar thing in Chrome using Capybara and SitePrism. This actually works for me in Firefox though:
page.element.native.send_keys :command, 'a'
so I suggest trying this
builder.native.send_keys :control, 's'

Unable to click on a popup with id's using selenium webdriver and ruby

Following is the code i am using
def self.yes_publish
sleep 5
driver.find_element(:id, 'dialogConfirmChanges-publishButton').displayed?
WAIT.until { driver.find_element(:id, 'dialogConfirmChanges-publishButton') }.click
puts driver.find_element(:id, 'embed-left-center-part').displayed?
end
But i am unable to click on it. This id works fine in irb.
I get an error modal dialog present, as webdriver is unable to locate element it closes to window after a particular timeout.
This popup is to publish changes made on a page.
xpath = .//*[#id='dialogConfirmChanges-publishButton']
You have to use the switch_to method to deal with the pop-up. Look at the documentation of JavaScript dialogs :
You can use webdriver to handle Javascript alert(), prompt() and confirm() dialogs. The API for all three is the same.
Note: At this time alert handling is only available in Firefox and IE (or in those browsers through the remote server), and only alerts that are generated post onload can be captured.
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mysite.com/page_with_alert.html"
driver.find_element(:name, 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
a.dismiss
else
a.accept
end
EDIT
As per the comment box HTML given by you..I think below should work :
driver.find_element(:xpath,"//div[#class='ui-dialog-buttonset']/button[#id='dia‌​logConfirmChanges-publishButton']").click

Watir-webdriver: Script working on Firefox but failing on Chrome or Opera. why?

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.

Issues with link on Watir and Safari?

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")

Resources