Program Tip

Capybara에서 "_blank"대상이있는 링크의 새 창으로 어떻게 전환합니까?

programtip 2020. 10. 20. 08:04
반응형

Capybara에서 "_blank"대상이있는 링크의 새 창으로 어떻게 전환합니까?


아마도 이것이 실제로 내가 겪고있는 문제는 아니지만 target = "_ blank"로 링크를 "click_link"하면 세션이 현재 창에 초점을 유지하는 것 같습니다.

따라서 새 창으로 전환하거나 _blank 속성을 무시하고 싶습니다. 기본적으로 링크로 표시된 페이지로 실제로 이동하여 올바른 페이지인지 확인할 수 있습니다.

웹킷과 셀레늄 드라이버를 사용합니다.


지금까지 내 결과를 아래에 제출했습니다. 더 철저한 답변을 주시면 감사하겠습니다.

또한 이것은 셀레늄에서만 작동합니다. 웹킷 드라이버에 해당하는 것 (또는 내가 직접 발견 할 수있는 위치를 지적하는 것)은 많은 도움이 될 것입니다.


Capybara> = 2.3에는 새 창 관리 API가 포함됩니다. 다음과 같이 사용할 수 있습니다.

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

이 솔루션은 Selenium 드라이버에서만 작동합니다.

열려있는 모든 창은 Selenium의 상점입니다.

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)

카피 바라 문제 페이지에서 참조 : https://github.com/jnicklas/capybara/issues/173

Selenium의 창 전환 기능에 대한 자세한 내용 : http://qastuffs.blogspot.com/2010/10/testing-pop-up-windows-using-selenium.html


이것은 이제 Poltergeist와 함께 일하고 있습니다. window_handles아직 구현되지는 않았지만 (JavaScript 팝업을 통해 창 이름이 필요합니다) :

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

편집 : 아래 의견에 따라 Poltergeist는 이제 버전 1.4.0window_handles 부터 구현 합니다.


Capybara는 창을 쉽게 찾고 전환 할 수있는 몇 가지 방법을 제공합니다.

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

자세한 내용은 여기 : 카피 바라 문서


나는 이것이 오래된 게시물이라는 것을 알고 있지만 카피 바라 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

지금은 capybara-webkit으로는 불가능한 것 같습니다 : https://github.com/thoughtbot/capybara-webkit/issues/271

:-(

동시에 https://github.com/thoughtbot/capybara-webkit/issues/129within_window.

또한 https://github.com/thoughtbot/capybara-webkit/issues/47 에서 page.driver.browser.switch_to().window(page.driver.browser.window_handles.last)작동 한다고 제안 합니다. 아 글쎄, 코드 읽기에 대해.

https://github.com/thoughtbot/capybara-webkit/blob/master/lib/capybara/webkit/browser.rb 의 코드에는 적어도 webdriver / firefox에서 작동하는 API가 작동한다는 것을 제안하는 몇 가지 참조가 있습니다. 웹킷.


이제 capybara-webkit http://github.com/thoughtbot/capybara-webkit/pull/314에 대한 within_window가 구현 되었으며 여기에서 사용 방법을 볼 수 있습니다. http://github.com/mhoran/capybara-webkit-demo


2014 년 5 월 현재 다음 코드는 capybara-webkit에서 작동합니다.

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

이것은 capybara-webkit에서 나를 위해 작동합니다.

within_window(windows.last) do
  # code here
end

(저는 capybara 2.4.1 및 capybara-webkit 1.3.0을 사용하고 있습니다)


이름 , URL 또는 제목도 전달할 수 있습니다 (하지만 이제는 구분됨).

  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 labda or a proc also

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

wish it stretches the method usage to more clearly understaing


To explicitly change window, you use 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_window block


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.


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"]

참고URL : https://stackoverflow.com/questions/7612038/with-capybara-how-do-i-switch-to-the-new-window-for-links-with-blank-targets

반응형