generate new window in watir (intentionally) that can be added to the browser#windows array

StackOverflow https://stackoverflow.com/questions/21440768

  •  04-10-2022
  •  | 
  •  

質問

There are a lot of questions on stackoverflow already addressing how to handle a new Watir window once its already there, i.e. from a popup in most examples.

The answers to those questions talk about the browser.windows[n].use command, and so on.

But I'm trying to intentionally generate a second window that can be added to the stack of windows in the windows command.

I've tried this so far...

b = Watir::Browser.new
bb = Watir::Browser.new
b.windows << bb.window

...but the changes didn't persist.

b.windows.count
#=> 1

Is there any way I can generate a second browser window without creating a new Watir::Browser object? (I already know Watir for Firefox doesn't support tabs)

役に立ちましたか?

解決

One solution I have seen in the past, was to use javascript to open a new window:

# Open the first browser window as normal
b = Watir::Browser.new
b.windows.count
#=> 1

# Execute javascript to open a second window    
b.execute_script('window.open();')
b.windows.count
#=> 2

Note that there is a single Watir browser object, but it knows of 2 windows.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top