Switch to Popup Windows in Cucumber, Capybara

Switch to popup windows in cucumber, capybara

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.

Switch to new window handle in Rspec

For locating windows, Watir has a title locator. You can see an example for this (and other ways for switching windows) in the watirspec:

it "finds window by :title" do
w = browser.window(title: "closeable window").use
expect(w).to be_kind_of(Window)
end

How interacting popup using selenium?

If your pop up is coming as new window then you can refer this link which is explaining your problem nicely: Switch to popup windows in cucumber, capybara

With Capybara, how do I switch to the new window for links with _blank targets?

Capybara >= 2.3 includes the new window management API. It can be used like:

new_window = window_opened_by { click_link 'Something' }
within_window new_window do
# code
end

Cucumber + Capybara tests to ensure a new window is opened

I see no assertions in your test.

My approach would be to test size of window_handles array after performing clicking action on the link, since before clicking the size should equal 1 and after the clicking window_handles should equal 2.

assert page.driver.browser.window_handles.size == 2

Imho, good enough, since if the webpage is loaded in the same tab, the size will be 1 and the test will fail.

How to handle dynamic elements with cucumber and capybara

None of the elements you are talking about have ids so a CSS find using #<id> (find('#my_button').click) isn't going to work. However to click that button you should just be able to do

click_button('Continue with Email') # case of the text matters

or

click_button(class: 'emailSignInButton')

This all assumes you are using a driver with Capybara that supports JS - https://github.com/teamcapybara/capybara#drivers

Here's code that shows it works if using a JS capable driver

require 'capybara/dsl'
require 'selenium-webdriver'

session = Capybara::Session.new(:selenium_chrome)
session.visit "https://www.redfin.com"

session.click_link('Sign In', href: nil)

session.click_button('Continue with Email')


Related Topics



Leave a reply



Submit