English 中文(简体)
与Capybara一起,我如何转向与“空白”目标挂钩的新窗口?
原标题:With Capybara, how do I switch to the new window for links with "_blank" targets?

也许这只是一个实际问题,但似乎在我“click_link”与目标=“空白”挂钩时,本届会议将重点放在目前的窗口上。

因此,我要么希望能够转向新的窗口,要么忽视“空白属性”——基本上,我只希望它实际上走到该链接所显示的网页上,以便我能够确保该链接的正确网页。

我使用网基和 se司机。


页: 1 回答更为彻底。

此外,这只是ium——对网基驱动力(或指出我可以发现自己之处)的等同——的工作将受到高度赞赏。

最佳回答

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
问题回答

这一解决办法只针对 Sel升司机。

所有露天窗都是 Sel的商店

response.driver.browser.window_handles

这似乎是一个阵容。 最后一个项目始终是最近打开的窗口,这意味着你可以做以下工作来转用。

在一栏内:

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

简单地重议本届会议:

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

http://github.com/jnicklas/capybara/issues/173。

http://qastuffs.blogspot.com/2010/10/testing-pop-up-windows-using-selenium.html

I know this is old post, but for what its worth in capybara 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

Seems such as it is not possible withcapybara-webkit now: https://github.com/thoughtbot/capybara-webkit/issues/271

:

与此同时,s http://gith.com/ th/ 申 诉是有可能的。

http://github.com/thoughtbot/capybara-webkit/issues/47“https://github.com/thoughtbot/capybara-webkit/issues/47 建议page.driver.browser.switch_to().window(page.driver.browser.window_handles.last) 工作。 Ah, 待读。

https://github.com/thoughtbot/capybara-webkit/blob/master/lib/capybara/webkit/browser.rb> 至少有一些参考资料表明,为网络用户/消防公司工作的预报软件也在为网络服务。

http://github.com/thoughtbot/capybara-webkit/pull/314。 http://github.com/mhoran/capybara-webkit-demo>。

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

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

这对我来说是靠 cap巴网做的:

within_window(windows.last) do
  # code here
end

(一米,使用帽子2.4.1和帽子13.0)

You can pass a name, url or title of 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

页: 1 并且

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

希望它把方法的使用延伸到更为明显的障碍

I had this issue when opening links in an gmail window: I fixed it like this:

Given /^(?:|I )click the "([^"]*)" link in email message$/ do |field|

  # var alllinks = document.getElementsByTagName("a");
  # for (alllinksi=0; alllinksi<alllinks.length; alllinksi++) {
  #   alllinks[alllinksi].removeAttribute("target");
  # }

  page.execute_script( var alllinks = document.getElementsByTagName("a"); for (alllinksi=0; alllinksi<alllinks.length; alllinksi++) { alllinks[alllinksi].removeAttribute("target"); } )

  within(:css, "div.msg") do
    click_link link_text
  end

end

The best idea is to update capybara to the latests version (2.4.1) and just use windows.last because page.driver.browser.window_handles is deprecated.

主要实施(window_ opened_by)给我带来了错误:

*** Capybara::WindowError Exception: block passed to #window_opened_by opened 0 windows instead of 1

因此,我通过这一解决办法解决这个问题:

new_window = open_new_window

within_window new_window do
  visit(click_link  Something )
end

page.driver.browser.window_handles
# => ["CDwindow-F7EF6D3C12B68D6B6A3DFC69C2790718", "CDwindow-9A026DEC65C3C031AF7D2BA12F28ADC7"]

I personally like the following approach since it works correctly regardless of JS being enabled or not.

My Spec:

  it "shows as expected" do
    visit my_path

    # ...test my non-JS stuff in the current tab

    switch_to_new_tab

    # ...test my non-JS stuff in the new tab
    
    # ...keep switching to new tabs as much as necessary
  end 

  # OR

  it "shows as expected", js: true do
    visit my_path

    # ...test my non-JS stuff in the current tab
    # ...also test my JS stuff in the current tab

    switch_to_new_tab

    # ...test my non-JS stuff in the new tab
    # ...also test my JS stuff in the new tab

    # ...keep switching to new tabs as much as necessary
  end 

测试助手:

  def switch_to_new_tab
    current_browser = page.driver.browser

    if js_enabled?
      current_browser.switch_to.window(current_browser.window_handles.last)
    else
      visit current_browser.last_request.fullpath
    end
  end

  def js_enabled?
    Capybara.current_driver == Capybara.javascript_driver
  end




相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...