编写一些测试时,我不得不单击指向另一个选项卡的链接,然后我需要单击另一个链接,最后一步不起作用(找不到链接"示例"(Capybara::ElementNotFound((。 我是否需要先将焦点更改为新选项卡?我该怎么做?
选项卡在 Capybara 中被视为新窗口,因此您需要使用 Capybaras 窗口 API 移动到新窗口。它看起来像
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
我所要做的就是在操作之前添加switch_to_window(windows.last)