2017-01-23 36 views
1

我使用的水豚用硒作爲它的驅動程序。我試圖點擊一個元素,點擊時它會顯示一個div,但點擊從不調用JavaScript來做到這一點。硒不執行JavaScript的

下面是我的代碼有

scenario 'currently used transport mode cannot be re-selected' do 

expect(page).to have_css("h2.summary") 
expect(find('h2.summary').text).to eq("Single event") 
expect(page).to have_content("Change journey") 
page.click_link("Change journey") 
expect(find('#travel-times-preview').visible?).to be_truthy # FAILS here because of previous step not working 

end 

錯誤信息

水豚:: ElementNotFound:無法找到的CSS 「#旅行時間預覽」

HTML

<a class="change-journey gray-text" href="#">Change journey</a> 

的JavaScript代碼來執行

$(".change-journey").on("click", function(e){ 
     var target = $(this).data("preview-target"); 
     $('[data-preview-toggle="'+ target +'"]').toggleClass("hidden"); 
     if($(this).text().indexOf('Change journey') > -1){ 
     $(this).text("Close Preview"); 
     }else{ 
     $(this).text("Change journey"); 
     } 
    e.preventDefault(); 
    }); 

數據庫吸塵器設置

config.before(:suite) do 
    if config.use_transactional_fixtures? 

     raise(<<-MSG) 
     Delete line `config.use_transactional_fixtures = true` from rails_helper.rb 
     (or set it to false) to prevent uncommitted transactions being used in 
     JavaScript-dependent specs. 
     During testing, the Ruby app server that the JavaScript browser driver 
     connects to uses a different database connection to the database connection 
     used by the spec. 

     This Ruby app server database connection would not be able to see data that 
     has been setup by the spec's database connection inside an uncommitted 
     transaction. 
     Disabling the use_transactional_fixtures setting helps avoid uncommitted 
     transactions in JavaScript-dependent specs, meaning that the Ruby app server 
     database connection can see any data set up by the specs. 
     MSG 

    end 
    end 

    config.before(:suite) do 
    DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:each) do 
    DatabaseCleaner.strategy = :transaction 
    end 

    config.before(:each, type: :feature) do 
    # :rack_test driver's Rack app under test shares database connection 
    # with the specs, so we can use transaction strategy for speed. 
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test 

    if driver_shares_db_connection_with_specs 
     DatabaseCleaner.strategy = :transaction 
    else 
     # Non-:rack_test driver is probably a driver for a JavaScript browser 
     # with a Rack app under test that does *not* share a database 
     # connection with the specs, so we must use truncation strategy. 
     DatabaseCleaner.strategy = :truncation 
    end 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 

雖然我可以看到被點擊該鏈接時,不執行底層的JavaScript。

+0

你確定你點擊正確的元素?這個代碼對我們來說沒有任何意義,沒有'HTML' – Andersson

+0

@Andersson我添加了html鏈接。請注意,如果不使用測試環境並進行手動測試,則可以運行似乎是e.preventDefault()沒有觸發的錯誤 - 而是在點擊該鏈接時重新加載頁面。 –

+0

你應該用「水豚」來標記這個問題,因爲這就是你正在使用的東西 - 不是直的硒 –

回答

1

假設你已經打開了firefox窗口中的dev控制檯並且沒有JS錯誤,那麼你看到的行爲有幾個潛在的原因,其中沒有任何與e.preventDefault()(當你聽到馬蹄聲認爲馬,而不是斑馬)

  1. 告訴你沒有一個data-preview-target屬性,所以沒什麼的點擊處理程序來讀取和追加到data-preview-toggle=所以沒有獲取切換的鏈接。這有可能是數據是由其它JS在你的應用程序,所以被設置爲一個屬性添加,是不是在HTML屬性(或者你只是選擇了離開,詳細說明了你表現的元素),它移動到#2

  2. .on JS運行代碼的元素存在之前,因此不附着。這可以在測試模式下顯示出來,因爲JS的加載時多個請求的開發模式由速度比當它加到所有的JS到一個文件中。這會顯示爲鏈接文本沒有改變,因爲點擊處理程序根本沒有運行。如果這是延遲連接監聽器直到DOM被加載。

  3. 這是支持測試第一JS你寫的內容被隱藏/顯示從數據庫中的記錄產生,而你還沒有禁用事務相關測試和/或配置database_cleaner正確。這導致您在測試中創建的對象在您的應用中實際上不可見,因此您期望在頁面上的元素實際上並不存在。您可以通過暫停測試並查看HTML來驗證它是否實際在頁面上。

  4. 根據您提供的錯誤不是您問題的原因,只是爲答案的完整性添加:click_link單擊鏈接並立即返回。這意味着測試繼續運行,而點擊觸發的操作仍在繼續。這可能會導致在你的代碼中的競爭條件(如果你有Capybara.ignore_hidden_elements = false集 - 壞主意)在您的代碼發現元素,它改變了之前檢查其知名度。因爲,你的最後一個步驟應該是如下,因爲它會等待寫入/重試的元素變得可見

    expect(page).to have_css('#travel-times-preview', visible: true) 
    

順便說一句,你的測試代碼可以改進,通過使用功能加快水豚提供

scenario 'currently used transport mode cannot be re-selected' do 

    expect(page).to have_css("h2.summary", text: "Single event") # do it in one query 
    page.click_link("Change journey") # click_link will find the content anyway so no need to check for it before 
    expect(page).to have_css('#travel-times-preview') # By default Capybara only finds visible elements so checking visibility is pointeless, if you've changed that default see #4 

end 
+0

嘿托馬斯謝謝你的詳細回答,我想回復,因爲罪魁禍首還沒有找到。我會回答你提出的每一點。 1.你是對的,我忽略了數據預覽切換。 2.整個功能被封裝在另一個在「turbolinks:load」事件上調用的函數中。 3.是的,這是我第一次接受測試 - 我將編輯帖子並添加數據庫清理配置 - 不過使用命令「save_and_open_page」或打開瀏覽器時使用硒,我可以在那裏看到記錄。你提供的代碼仍然沒有成功。 –

+0

@PetrosKyriakou你的database_cleaner配置塊應該是append_after,如database_cleaner文檔所示 - https://github.com/DatabaseCleaner/database_cleaner#rspec-with- capybara-example。這不會導致你目前的問題,但它會讓你的測試前進。當您在click_link後暫停測試時,是否確認在Firefox控制檯中沒有錯誤?你使用什麼版本的turbolinks?另外,你不會將場景顯示爲標記爲'js:true' - 我假設它在樹上更高(或者當你運行測試時firefox不會打開) –

+0

嘿,我正在使用Capybara.current_driver =:硒「在這個規範的背景(:所有)塊 - 沒有別的,它打開了Firefox。是在控制檯中檢查沒有錯誤。我正在使用Rails 5認爲它的turbolinks 5(無論是隨軌道5發貨)。順便說一句,你提供的錯誤代碼略有不同'預計會發現css「#travel-times-preview」,但沒有匹配。還發現「」,它與選擇器相匹配,但不是所有的過濾器。「感謝數據庫清理技巧,但會檢查出來。由於turbolinks事件,它有可能嗎? –