ruby watir-webdriver cannot access popup - ruby

Hi All I am having problems accessing a form with in a popup window that is opened after I click on a link. I appears once I have clicked the link it causes the script to hang and will not even time out. I need to be able to access the form, set some text fields and the click the submit button.
Link code:
<a id="ctl00_ContentPlaceHolder2_ctrlPageHeader1_aFilter" class="RightTextHeading" onclick="javascript:openMdlWindow('InvestmentDetailOptions.aspx?IDAssetType=','620','600');if(window.document.RetValue == '2'){window.parent.LoadinIframe('InvestmentDetail.aspx?FromMenu=N&IDAssetType=','Investment Details > Full View','false');}" style="text-decoration:none;">Filter</a>
I have tried everything but nothing seems to work. Has anyone ever came across this before and have a solution?
Thanks

Did you try this:
browser.window(:title => "annoying popup").use do
browser.button(:id => "close").click
end
More information: http://watirwebdriver.com/browser-popups/

Related

selenium script for mouse hovering using Ruby

I am using Ruby on Rails to automate website. I am stuck on mouse hover and click action. I have to write code e.g. following website
http://www.sainsburys-live-well-for-less.co.uk/meal-planning/
I need to automate by hovering then clicking on Occasion > summer
Following code i have written but it does not work
*menu = driver.find_element(:id => "Occasion")
submenu = driver.find_element(:id => "summer")
driver.action.move_to(menu).click(submenu).perform*
This code gives error "undefined local variable or method 'driver'".
please suggest.

Emulate a HTML <button> press within Ruby

There's a website that I want to scrape (FWIW it's svpply) and there's a button that shows up sometimes saying "Show All", it's an HTML <button> element. Is there any way to use Ruby to emulate a click of this button, and get the content of the full page that results after clicking that button, since the button reveals more content?
The "Show All" button triggers a javascript ajax request. The only way to automate that is to use a library that can execute javascript. Libraries such as Mechanize and ScrAPI will not work.
What will work are tools that drive an actual browser such as watir and selenium. I installed watir-webdriver and successfully got it to click the button and show additional products.
require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'svpply.com/editors_pick'
#count products
puts b.elements(:xpath => '//li[#data-class="Product"]').count
#=> 30
#Now click button
show_all = b.button(:id => "btn_all")
show_all.click
sleep 4
#count products again
puts b.elements(:xpath => '//li[#data-class="Product"]').count
#=>60
Mechanize can do this nicely for you.
If the page is reloaded when you press the button, you can, otherwise you need something that can parse javascript. If you understand where the website redirects you after pressing the button (even on the same page but with some parameters set, use firebug for this purpose), you can eventually read what you need.

capybara finds button but clicking it does nothing

UPDATE: I removed most javascript dependencies and it worked. Don't know what library or code is the evil part, I needed none of it.
simple form, two input fields and a button
fill_in 'cellNumber', :with => '13245678'
fill_in 'password', :with => 'mypass'
click_button('OK')
It finds the button, but nothing happens.
Important!, if I click the myself, everything works as expected.
<button type="submit" id="loginid">OK</button>
</form>
Using ruby-debug, I find that find_button("OK").click returns "". If not found would raise error.
If I click manually inside the firefox window, halting the capybara test with sleep or in debug, I see (test window in focus) a blue frame has appeared around the button , as it's been selected somehow.
Anyway, I'm close to checking in to a mental facility right now... Any suggestions?
Javascript may interfere. The page had some unnecessary dependencies to Squeezebox, Mootools-1.2.5 and Function.cbb.js, so I just removed them and refactored to use more css functionality (like hover) and specialized js. I'm not impressed with this. Nonetheless, I hope this answer would help others.
update: Seems like Mootools is incompatible with Selenium. Without including any other JS library/code than the mootools core library, it breaks. I've tried both 1.2.5-core and 1.3.2-full-compact.

How do I negotiate alert boxes in Watir?

I am trying to create an automation script for a website using Ruby with
Watir.
I ran into a situation where I have to choose 'Yes' or 'No' in an
alert box. The problem is that I am not able to identify the alert box
as a component of the page so that I can obtain its identifier and use
it to select an option.
I found some information how to
create an alert box, but I need to navigate through one. Can
anyone help me out?
It sounds like you have a confirm box if you can choose two options ("OK" or "Cancel"). If you want to simulate clicking "OK", you have to put something like this before you trigger the confirm.
# return true for confirm to simulate clicking OK
b.execute_script("window.confirm = function() {return true}")
If you just have an alert with a single "OK" button, do this.
# don't return anything for alert
b.execute_script("window.alert = function() {}")
See this page for more examples.
The approach suggested by Austin Taylor may work fine, but I think that for a more general solution you need to deal with Popups at a OS level as Chuck suggests.
If anyone reaches this page seeking this sort of answer there are different solutions listed here: http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups
You can use these clean watir calls:
browser.alert.ok
browser.alert.close
See Official Framework Documentation for more info.

How to handle new browser pop up after clicking a js button in Watir?

I am trying to using watir in ruby, I am able open a browser,
enter some values to a username/password form, but then I press enter, then I click the submit button (actually it is pressing an enter, it is easiler to code), it would pop up a new browser windows for our application, then I found I have no control to the new browser. What can I do?
In addition to my problem, the new browser window got no menubar, no toolbar, no navigation bar, thus I cannot open the IE developer toolbar to find the name of the element in the webpage in the new browser.
By the way, my app can only support IE.
I tried the attach method, but it does work:
C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.8.1/lib/watir/ie-class.rb:302:
in `attach_browser_window': Unable to locate a window with title of (?-mix:New browser title) (Watir::Exception::NoMatchingWindowFoundException)
from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.8.1/lib/watir/ie-class.rb
:150:in `_attach_init'
from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-1.8.1/lib/watir/ie-class.rb
:144:in `attach'
from test1.rb:36
Attach method is able to handle new window.
http://rdoc.info/gems/watir/1.8.1/Watir/IE.attach
for example
browser = Watir::Browser.new
browser.text_field(:id,'username').set 'username')
browser.button(:index,1).click
# popped up the new window
popup = Watir::Browser.attach(:title,'Foobar')
If you will use only the popped up window, you can overwrite browser variable instead.
You could try to authenticate like so:
examplehost.com/authentication?user=foo&password=bar
You could also try to remove "target" attribute (which is probably equal to "_blank") of that authentication form ( i'm pretty much sure you should be able to achive this with watir )
EDIT:
Also, there's this option:
ie2 = Watir::IE.attach(:title, ‘New Ie window, opened upon form submit’)
it can also attach by :url instead of :title
ok, there's possibly this solution that might work:
b = Watir::Browser.start "http://yourpage"
b.execute_script("document.getElementById('your-image-id').onClick = function() {}")
so, the idea is to get that image and overwrite it's onclick event

Resources