Ruby on Rails: Cucumber: how do I follow a link th

2019-04-08 00:24发布

问题:

I have a link that opens in a new window, and I need to test the part of my app that is within that new window.

Any suggestions will be much appreciated.

回答1:

Could you just validate that the link has it's target set to _blank?



回答2:

Define a step that contains this code:

page.driver.browser.switch_to.window (page.driver.browser.window_handles.last)

switch_to is a selenium command that allows you to change over to a new window window_handles returns a list of currently open windows



回答3:

I realize this is a very old post, but here's a Cucumber step definition using Capybara and the Poltergeist driver:

And /^I follow "([^"]*)" to the new window$/ do |link|
  new_window = window_opened_by { click_link link }
  switch_to_window new_window
end