2017-04-04 16 views
0

這是一個Rspec功能。爲什麼Capybara/Rspec無法在Sweet Alert模式中找到該按鈕,但has_selector返回true?

given (:send_selector){ 'button.confirm'} 

feature "when talent has not applied." do 
    given (:apply_button) { find('button[data-selector="offer-apply-button"]') } 
    before(:each)   { visit offer_path(offer.id)       } 

    scenario "Talent applies to offer with default cv", js: true do 
    apply_button.click 
    expect(page).to have_selector(send_selector) 
    find_button(send_selector).click 
    wait_for_ajax 
    # other expectations 
    end 
end 

它通過期望有選擇但隨後引發錯誤

Capybara::ElementNotFound: 
     Unable to find button "button.confirm" 

指着符合find_button(send_selector).click

我使用page.find_button(send_selector).clickfind_button(send_selector).trigger('click')另外嘗試,但仍是第一拋出錯誤,第二個似乎什麼都不做,因爲負責該操作的控制器永遠不會被調用,並且當我在下一個屏幕截圖時模式仍然存在東北。

我運行了poltergeist調試器,html出現在頁面上。

html code from the poltergeist debugger

這是(使用save_screenshot()法)

enter image description here

回答

0

萬一有人有同樣的問題,它只是罰款模態後加入少許的睡眠由水豚所看到的模式出現。這種行爲仍然是奇怪的,因爲期望說選擇器在那裏是可靠的。即使have_selector(send_selector, visible: true).present?也會返回true。

scenario "Talent applies to offer with default cv", js: true do 
    apply_button.click 
    expect(page).to have_selector(send_selector) 
    sleep 1          <<-- THIS SOLVED THE PROBLEM 
    find(send_selector).click 
    wait_for_ajax 
    ... 
end 
1

的問題是,find_button並不需要一個CSS選擇器 - http://www.rubydoc.info/gems/capybara/Capybara/Node/Finders#find_button-instance_method - 之所以如此命名,編號,標題或文本按鈕的內容(同爲click_button和「have_button」需要)。

在你的答案sleep 1無關與解決的問題,它是固定的,因爲你從find_button交換到find它不使用sweetalert2時CSS選擇

+0

嗨托馬斯,你是對的,find_button不採取CSS選擇器。儘管如此,我運行了這一次在答案中發佈的測試,而沒有「睡眠1」,但失敗了。 – juliangonzalez

+0

@juliangonzalez如果它是相同的錯誤,那麼它可能意味着你需要增加Capybara.default_max_wait_time幾秒鐘。然而,如果它是一個不同的錯誤(關於鼠標點擊的東西會碰到不同的元素),那麼這是因爲模式仍然是動畫,並且您將需要睡眠或更好,但在測試模式中禁用動畫,這將加快您的測試並且防止一些潛在的薄弱 –

0

我也面臨這個問題,我解決了它在功能規格如下。 (它的工作)

scenario 'destroy a company', js: true do 
    within('.shadow.company-info') do 
     find('a[data-behavior="delete"]').click 
     wait_for_ajax 
     within(page.document.find(:css, '.swal2-buttonswrapper')) do 
      find(:css, '.swal2-confirm').click 
      wait_for_ajax 
      expect(page.document).to have_content I18n.t('delete.success') 
     end 
    end 
end 
相關問題