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

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

    Capybara provides some methods to ease finding and switching windows:

    facebook_window = window_opened_by do
      click_button 'Like'
    end
    within_window facebook_window do
      find('#login_email').set('a@example.com')
      find('#login_password').set('qwerty')
      click_button 'Submit'
    end
    

    More details here: Capybara documentation

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

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

    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 
    
    

    Test helpers:

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

    This solution only works for the Selenium driver

    All open windows are stores in Selenium's

    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

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

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

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

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

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

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

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

    :-(

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

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

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

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