Is there an alternative to watir::ie.attach for watir-webdriver since attach is not supported on webdriver
Asked Answered
N

3

5

I have a website which is only rendered in Webkit enabled browser (Google Chrome, Safari). I am using Google Chrome since I am on Windows 7.

I am using Watir-WebDriver to automate the same.

Issue: When I click on a button on the browser window, is launches another window and post click content is rendered in the new browser window. I need a way to be able to Identify this new browser window, in-order to be able to proceed with my testing. I have been reading on various forums, but not getting any certain answer/solution.

Q: Is there an alternative to watir::ie.attach for watir-webdriver since attach is not supported on Watir-Webdriver

Sample code:

require "rubygems"

require "watir-webdriver"


require "selenium-webdriver"

b = Watir::Browser.new(:chrome)

website = "http://xyz.com"

#a new browser is launched and the website is opened

b.goto(website)

#this opens a new browser window

b.link(:xpath,"/html/body/div/ul/li/a").click

#there is a button called "MAP" on the new browser window

b.link(:id,"btn_MAP")

#this gives an error, unknown link
Nagaland answered 26/4, 2011 at 20:27 Comment(2)
Actually it is a API for mobile phones basically concentrating on iPhone and Android, and we are trying to render it on browser so that we are not limited to testing on the device itself or on simulators.Nagaland
Ah ok.. after I posted the comment I wondered if it might be something like that.. It leaves WinPhone out, but that's a smaller segment than i and A in the mobile market, so I can kinda see that logic.Miguelmiguela
E
7

"window" method is the alternative for ie.attach. Webdriver can handle the window opened by itself with window method.

b.link(:href,/server\/getPage/).click
b.window(:url,/server\/getPage/i).use do
  b.link(:id,"btn_MAP").click
end

you can handle popped up windows in the window method block. If you want to keep handling popped up window, use it without block, like window(:url,/foobar/).use

see also: http://groups.google.com/group/watir-general/browse_thread/thread/232df221602d4cfb

Extractor answered 27/4, 2011 at 4:19 Comment(11)
I get the following error with the solution above: //watir-webdriver-0.2.2/lib/watir-webdriver/elements/element.rb:241:in assert_exists': unable to locate element, using {:tag_name=>"a", :id=>"btn_MAP"} (Watir::Exception::UnknownObjectException) from //watir-webdriver-0.2.2/lib/watir-webdriver/elements/element.rb:69:in click' from ft_02.rb:29 from //selenium-webdriver-0.1.4/lib/selenium/webdriver/common/target_locator.rb:37:in window' from //watir-webdriver-0.2.2/lib/watir-webdriver/window_switching.rb:99:in use' from ft_02.rb:28Nagaland
@raat Could you attach the parts of HTML source you want to handle and pop-up window url?Extractor
I second that.. Edit your question and add in the new code you are attempting to use to implement the .window(how, what).use approach, that makes it much easier for someone to see where it may be going wrong.Miguelmiguela
HTML on (main page) first screen <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="w3.org/1999/xhtml" xml:lang="en"> <body id="content"> <div id="hyperlocal"> <ul class="menu"> <li class="menuitem"> <a target="_blank" href="server/getPage/67/1354"> <img class="image" src="server/imagexyz==.png"> </a></li></ul></div></body></html>Nagaland
HTML on second screen <html xmlns="w3.org/1999/xhtml" xml:lang="en"><head> <link href="server/webkitad/css/template1.css?v=2.2.b1"> <script type="text/javascript" src="server/webkitad/js/build/xyz.js?v=2.2.c6"> </script> </head> <body> <div id="wrapper" style="width: 1680px; "> <div id="header">...</div> <div id="scrollWrapper"> <div id="scroller"> <div id="content">...</div> <ul id="menu" class="menu"> <li class="odd"> <a id="btn_MAP"> <p class="buttonIcon MAP"></p> <span>Map</span> </a></li></ul> </div></div></body></html>Nagaland
Ruby/Watir-Webdriver Code: require "rubygems" require "watir-webdriver" require "selenium-webdriver" b = Watir::Browser.new(:chrome) website = "xyz?ver=2.x" b.goto(website) b.link(:xpath,"/html/body/div/ul/li/a").click #clicking on the link on the first Screen #Second Screen Opens-up with the post click content. b.window(:url,"server/getPage/67/1354").use do b.link(:id,"btn_MAP").click endNagaland
@raat I think you should use regular expression for the value of url attribute in the window method. I edited my answer. try it :)Extractor
@Yutaka: //watir-webdriver-0.2.2/lib/watir-webdriver/window_switching.rb:122:in locate': {:url=>/server\/getPage/} (Watir::Exception::NoMatchingWindowFoundException) from //watir-webdriver-0.2.2/lib/watir-webdriver/window_switching.rb:106:in handle' from //watir-webdriver-0.2.2/lib/watir-webdriver/window_switching.rb:99:in `use' from ft_02.rb:29Nagaland
Hmm.. Could you tell me the url of the second screen? It seemed you could not locate window with /server\getPage/. I guess we neet to ignore case-sensitive for the second screen's url.Extractor
@raat I editted my above script. Although I'm not sure this modification really fix your issue.. try it.Extractor
is there some reason you are trying to all this in a comments thread on an answer instead of editing the original question where the appended code could be properly formatted? that makes it far far easier for people to read it and try to help you outMiguelmiguela
N
2

@Yutaka: Thanks a lot for all your help it lead me to use something like the following and it worked!

b.link(:xpath,"/html/body/div/ul/li/a").click

c = b.window(:url,"http:\/\/server\/getPage\/67\/1354")

c.use

b.link(:id,"btn_MAP").click

Nagaland answered 3/5, 2011 at 12:31 Comment(0)
M
0

have you tried making the website the default homepage for the browser?

that might prevent you from having to do an attach.

Miguelmiguela answered 26/4, 2011 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.