lamwaiman1988
lamwaiman1988

Reputation: 3742

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

Upvotes: 0

Views: 3751

Answers (3)

Vlad Khomich
Vlad Khomich

Reputation: 5880

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

Upvotes: 0

Yutaka Yamaguchi
Yutaka Yamaguchi

Reputation: 374

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.

Upvotes: 1

Vlad Khomich
Vlad Khomich

Reputation: 5880

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

Upvotes: 0

Related Questions