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

后端 未结 15 1986
花落未央
花落未央 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:22

    The main implementation (window_opened_by) raises an error for me:

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

    So, I resolve it by this solution:

    new_window = open_new_window
    
    within_window new_window do
      visit(click_link 'Something')
    end
    
    page.driver.browser.window_handles
    # => ["CDwindow-F7EF6D3C12B68D6B6A3DFC69C2790718", "CDwindow-9A026DEC65C3C031AF7D2BA12F28ADC7"]
    

提交回复
热议问题