Jaime Junior
Jaime Junior

Reputation: 61

Changing the focus of the window on Capybara

Writing some tests I had to click on a link that leads to another tab then I needed to click on another link, that last step isn't working (Unable to find link "Example" (Capybara::ElementNotFound)). Should I need to change focus to the new tab first? How do I do that?

Upvotes: 4

Views: 964

Answers (2)

Jaime Junior
Jaime Junior

Reputation: 61

All I had to do was add switch_to_window(windows.last) before the action

Upvotes: 0

Thomas Walpole
Thomas Walpole

Reputation: 49890

Tabs are treated as new windows in Capybara, so you need to move to the new window using Capybaras window api. It will look something like

new_window = window_opened_by do
  # here you are in the context of the original window but your actions are expected to open a new tab/window
  click_link 'the link that opens the new tab'
end

within_window(new_window) do
  # this is in the context of the newly opened tab/window
  click_link 'the link inside the new tab'
end

# here you will be back in the context of the first window

new_window.close # if you want to close the tab that was opened

Upvotes: 2

Related Questions