不推荐将字符串参数传递给#within_window

时间:2014-08-21 22:25:57

标签: capybara

我正在尝试更新我的代码,因为我在运行它时目前收到以下弃用警告:

" DEPRECATION警告:不推荐将字符串参数传递给#within_window。传递窗口对象或lambda。"

以下是代码:

new_window=page.driver.browser.window_handles.last 
    page.within_window new_window do
        expect(current_url).to eq("url")
    end
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)

我应如何编辑以上内容,以便不再收到弃用警告?谢谢!

1 个答案:

答案 0 :(得分:9)

within_window方法已更改为期望Capybara :: Window或proc / lamda。通过字符串定位窗口,这是window_handles.last返回的内容,是不推荐使用的内容。

要获取最后一个Capybara :: Window,请使用windows method。它的工作方式类似于window_handles

new_window = windows.last
page.within_window new_window do
    expect(current_url).to eq("url")
end

请注意,文档说明"未定义返回数组中窗口的顺序。驱动程序可以按创建时间对窗口进行排序,但不是必需的。"。我认为使用window_handles时也是如此,因此可以安全地假设最后一个窗口是新窗口。

但是,在可能的情况下,最好通过标题:

等特定内容来定位窗口
within_window(->{ page.title == 'New window title' }) do
  expect(current_url).to eq("url")
end