Ruby-on-rails 使用 Capybara,如何切换到带有“_blank”目标的链接的新窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7612038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:04:16  来源:igfitidea点击:

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

ruby-on-railstestingseleniumwebkitcapybara

提问by GlyphGryph

Perhaps this isn't actually the issue I'm experiencing, but it seems that when I "click_link" a link with target="_blank", the session keeps the focus on the current window.

也许这实际上并不是我遇到的问题,但似乎当我“click_link”一个带有 target="_blank" 的链接时,会话将焦点保持在当前窗口上。

So I either want to be able to switch to the new window, or to ignore the _blank attribute - essentially, I just want it to actually go to the page indicated by the link so I can make sure it's the right page.

所以我要么希望能够切换到新窗口,要么忽略 _blank 属性 - 本质上,我只是希望它实际转到链接指示的页面,以便我可以确保它是正确的页面。

I use the webkit and selenium drivers.

我使用 webkit 和 selenium 驱动程序。



I submitted my findings thus far below. A more thorough answer is much appreciated.

我在下面提交了我的发现。非常感谢更彻底的答案。

Also, this only works with selenium - the equivalent for the webkit driver (or pointing out where I could discover it myself) would be much appreciated.

此外,这只适用于 selenium - webkit 驱动程序的等效项(或指出我自己可以在哪里发现它)将不胜感激。

回答by Andrei Botalov

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

Capybara >= 2.3 包括新的窗口管理 API。它可以像这样使用:

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

回答by GlyphGryph

This solution only works for the Selenium driver

此解决方案仅适用于 Selenium 驱动程序

All open windows are stores in Selenium's

所有打开的窗口都是 Selenium 的商店

response.driver.browser.window_handles

Which seems to be an array. The last item is always the window that was most recently opened, meaning you can do the following to switch to it.

这似乎是一个数组。最后一项始终是最近打开的窗口,这意味着您可以执行以下操作来切换到该窗口。

Within a block:

在一个块内:

new_window=page.driver.browser.window_handles.last 
page.within_window new_window do
  #code
end

Simply refocus for current session:

只需重新聚焦当前会话:

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

Referenced on the capybara issues page: https://github.com/jnicklas/capybara/issues/173

在水豚问题页面上引用:https: //github.com/jnicklas/capybara/issues/173

More details on Selenium's window switching capabilities: http://qastuffs.blogspot.com/2010/10/testing-pop-up-windows-using-selenium.html

有关 Selenium 窗口切换功能的更多详细信息:http: //qastuffs.blogspot.com/2010/10/testing-pop-up-windows-using-selenium.html

回答by Kridsada Thanabulpong

This is now workingwith Poltergeist. Although window_handlesis still not implemented (you need a window name, i.e. via a JavaScript popup):

现在正在与 Poltergeist 合作。虽然window_handles仍未实现(您需要一个窗口名称,即通过 JavaScript 弹出窗口):

within_window 'other_window' do
  current_url.should match /example.com/
end


Edit:Per comment below, Poltergeist now implements window_handlessince version 1.4.0.

编辑:根据下面的评论,Poltergeist 现在window_handles版本 1.4.0 开始实施。

回答by Aravin

Capybara provides some methods to ease finding and switching windows:

Capybara 提供了一些方法来简化查找和切换窗口:

facebook_window = window_opened_by do
  click_button 'Like'
end
within_window facebook_window do
  find('#login_email').set('[email protected]')
  find('#login_password').set('qwerty')
  click_button 'Submit'
end

More details here: Capybara documentation

更多详细信息:Capybara 文档

回答by bcar

I know this is old post, but for what its worth in capybara 2.4.4

我知道这是旧帖子,但它在水豚 2.4.4 中的价值

within_window(switch_to_window(windows.last)) do 
    # in my case assert redirected url from a prior click action
    expect(current_url).to eq(redirect['url'])
end

回答by user132837

Seems like it is not possible with capybara-webkit right now: https://github.com/thoughtbot/capybara-webkit/issues/271

现在似乎无法使用 capybara-webkit:https: //github.com/thoughtbot/capybara-webkit/issues/271

:-(

:-(

At the same time https://github.com/thoughtbot/capybara-webkit/issues/129claims it is possible to switch windows with within_window.

同时https://github.com/thoughtbot/capybara-webkit/issues/129声称可以使用within_window.

Also https://github.com/thoughtbot/capybara-webkit/issues/47suggests that page.driver.browser.switch_to().window(page.driver.browser.window_handles.last)works. Ah well, on to code reading.

此外https://github.com/thoughtbot/capybara-webkit/issues/47表明page.driver.browser.switch_to().window(page.driver.browser.window_handles.last)作品。嗯,开始阅读代码。

The code at https://github.com/thoughtbot/capybara-webkit/blob/master/lib/capybara/webkit/browser.rbat least has some references that suggest that the API that works for webdriver / firefox is also working for webkit.

https://github.com/thoughtbot/capybara-webkit/blob/master/lib/capybara/webkit/browser.rb上的代码至少有一些参考资料表明适用于 webdriver/firefox 的 API 也适用于网络套件。

回答by E-Hauler

Now within_window implemented for capybara-webkit http://github.com/thoughtbot/capybara-webkit/pull/314and here you can see how to use it http://github.com/mhoran/capybara-webkit-demo

现在为 capybara-webkit 实现了 inside_window http://github.com/thoughtbot/capybara-webkit/pull/314在这里你可以看到如何使用它http://github.com/mhoran/capybara-webkit-demo

回答by thekindofme

As of May 2014 the following code works on capybara-webkit

截至 2014 年 5 月,以下代码适用于 capybara-webkit

 within_window(page.driver.browser.window_handles.last) do
   expect(current_url).to eq('http://www.example.com/')
 end

回答by itsnikolay

You can pass a name, urlor titleof the window also (But now its dipricated)

您也可以传递窗口的名称网址标题(但现在它已简化)

  let(:product) { create :product }

  it 'tests' do
    visit products_path
    click_link(product.id)

    within_window(product_path(product)) do
      expect(page).to have_content(product.title)
    end
  end

You can pass a labdaor a procalso

您可以通过一个labdaPROC

within_window(->{ page.title == 'Page title' }) do
  click_button 'Submit'
end

wish it stretches the method usage to more clearly understaing

希望它可以将方法用法扩展到更清楚地理解

回答by G. I. Joe

To explicitly change window, you use switch_to_window

要显式更改窗口,请使用switch_to_window

  def terms_of_use
    terms_window = window_opened_by do
      click_link(@terms_link)
    end
    switch_to_window(terms_window)
  end

An after that method browser will work in the new page, instead of wrap everything in a within_windowblock

在该方法之后,浏览器将在新页面中工作,而不是将所有内容都包装在一个inside_window块中