Search code examples
watirpage-object-gem

How to access a parent window from inside a page-object page-section?


Is there a way to access a parent page of a page-section from within a modal dialog box on the page-section?

Using Watir/PageObject, on a JS modal window, I can access content inside the modal as below.

dialog_box = ModalDialogBox.new @browser
dialog_box.modal_dialog do
  button.click
end
dialog_box.attach_to_window(title: browser.title)
dialog_box.add_data_to_form
dialog_box.click_save_button

When creating a modal dialog using the above method from inside a page-section, I get the below error as the page-section is no longer a window. App has lots of multi-level dialog boxes (i.e., modal dialogs spawning from a modal dialog)

NoMethodError: undefined method `window' for Watir::Div:0x0000000481e7b0

Is there a way to access the original browser window to attach a modal dialog?


Solution

  • From an Element, you can get it's browser using the #browser method:

    your_element.browser
    #=> Watir::Browser
    

    If you need the window, you could call #window on that:

    your_element.browser.window
    #=> Watir::Window