3

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?

  • Hello! I would write a another test starting from this new tab opened. I use a lot capybara but i never found me in this situation. Tip: best practice is always write a short test – thiaguerd Feb 13 '20 at 01:15

2 Answers2

1

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
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
1

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