Switch to popup windows in cucumber, capybara

笑着哭i 提交于 2019-11-29 13:41:56

I don't think there is a cucumber/capybara way to do this as such.

But you can still change the window using selenium driver commands like this:

    #Get the main window handle
    main = page.driver.browser.window_handles.first
    #Get the popup window handle
    popup = page.driver.browser.window_handles.last

    #Then switch control between the windows
    page.driver.browser.switch_to.window(popup)

EDIT: Andrews answer below is the correct answer now since new DSL changes were implemented.

You can use Capybara in your Cucumber steps to interact with popup windows:

login_window = window_opened_by do
  click_button 'Open Login Window'
end
within_window(login_window) do
  fill_in :email, with: "email@example.com"
  fill_in :password, with: "password"
  click_button 'Log In'
end

In a simple way we can do so, without within_window and without selenium natives.

page.switch_to_window page.windows.first

page.switch_to_window page.windows.last 

or

page.switch_to_window page.windows[0]

page.switch_to_window page.windows[-1]

0 - is the first window
-1 - is the last window

windows function return an array of all windows.

Another way, using the title of page.

page.switch_to_window { title == 'Google' }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!