Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A child browser window closing itself apparently breaks Watir-webdriver's link to the parent window?

In Chrome, using watir-webdriver, I click a button that opens a child browser window.

I do:

@browser.window(title: 'Child').use

I successfully interact with various items in that child window.

Very shortly, there is an action done in this window that the site considers was the "point" of opening the window in the first place. e.g.,

@browser.button(title: 'Button').click

When this action is performed, the child window closes itself.

When I tell watir-webdriver to go back to using the parent window, however, I get an error:

@browser.window(title: 'Parent').use # => results in
# Selenium::WebDriver::Error::NoSuchWindowError: emptyScript execution failed; 
# The window could not be found

The reason I believe this is a Selenium/Watir-webdriver bug is because the following code works:

@browser.window(title: 'Child').use
# ... Now I do various things in the child window that do NOT
# ... cause it to close itself. They all work as expected.
# ...
puts @browser.window(title: 'Parent').present? #=>true
@browser.window(title: 'Child').close
puts @browser.window(title: 'Parent').present? #=>true
@browser.window(title: 'Parent').use # => No error thrown

The only difference is the action that causes the child window to close itself.

I notice that the watirspec code does not have a test case for a self-closing window, so perhaps this is an overlooked aspect of the windows code in watir-webdriver.

Okay, so here is the code that you can run, to see this problem yourself:

require 'watir-webdriver'
@b = Watir::Browser.new :chrome
@b.goto 'ckeditor.com/demo#full'
@b.div(id: 'cke_editor1').link(title: 'Link').click
@b.link(title: 'Browse Server').wait_until_present
@b.link(title: 'Browse Server').click
@b.window(title: 'CKFinder').use { 
  @b.frame(title: 'CKFinder').link(id: 'r0').right_click
  @b.frame(title: 'CKFinder').frame(id: 'cke_22_frame').wait_until_present 
  @b.frame(title: 'CKFinder').frame(id: 'cke_22_frame').link(id: 'cke_200').click
}

That will result in this error:

Selenium::WebDriver::Error::NoSuchWindowError: emptyScript execution failed;
The window could not be found

Don't say I never did nuthin' for ya! :-)

Now, before you go asking "Why are you testing a 3rd Party editing tool, you crazy guy?" I will answer that for you: I'm not.

What I'm testing is that the source code that gets generated by the CKEditor tool gets properly updated in a particular situation (which goes far beyond the scope of this question here). To set that up requires that I first know what that source code is so that, later, I can verify it gets properly updated by a different process. And to do that, I must first properly set it up, using the CKEditor's link tool. Capiche?

like image 324
Abe Heward Avatar asked Nov 13 '22 07:11

Abe Heward


1 Answers

If you need to get back to the first window opened you can use

@browser.windows[0]

You may also be able to do something like this (not tested)

@browser.windows.each do |window|
  if window.title.include?(target_string)
    window.use
  end
end
like image 159
pmneve Avatar answered Nov 15 '22 06:11

pmneve