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

后端 未结 15 1857
花落未央
花落未央 2020-12-02 09:54

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 w

相关标签:
15条回答
  • 2020-12-02 10:25

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

    0 讨论(0)
  • 2020-12-02 10:25

    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
    
    0 讨论(0)
  • 2020-12-02 10:25

    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.

    0 讨论(0)
提交回复
热议问题