Den Silver
Den Silver

Reputation: 321

Switch between windows with frames

I have main window with frames and a popup where I do some operations:

within_frame("MainFrame") do
  find("btnNewItem").click #opens popup window
end

within_window(windows.last) do
   within_frame("frmFrame2"){
      some_operations
      find("btnOK").click #closes a popup
     }
end

within_window(switch_to_window(windows.first)) do
  within_frame("MainFrame") do
     find("btnDeleteItem").click #deletes item
     accept_popup_dialog
  end
end

But when operations are done in popup window and it was closed, I get an error that:

Failure/Error: within_frame("frmFrame2"){
     Selenium::WebDriver::Error::NoSuchWindowError:
       Window is closed

What I do wrong?

I use Capybara 2.4.4

Upvotes: 2

Views: 435

Answers (1)

Justin Ko
Justin Ko

Reputation: 46836

The exception is occurring in the within_frame method when trying to switch back to the parent frame.

It seems like a bug, so th best thing to do would be to raise it as an issue in the Capybara project.

In the meantime, the quickest solution would be to rescue/ignore the exception:

within_frame("MainFrame") do
  find("btnNewItem").click #opens popup window
end

within_window(windows.last) do
  within_frame("frmFrame2"){
    some_operations
    find("btnOK").click #closes a popup
  } rescue Selenium::WebDriver::Error::NoSuchWindowError # Add a rescue here
end

within_window(switch_to_window(windows.first)) do
  within_frame("MainFrame") do
    find("btnDeleteItem").click #deletes item
    accept_popup_dialog
  end
end

Upvotes: 1

Related Questions