我使用的水豚用硒作爲它的驅動程序。我試圖點擊一個元素,點擊時它會顯示一個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。
你確定你點擊正確的元素?這個代碼對我們來說沒有任何意義,沒有'HTML' – Andersson
@Andersson我添加了html鏈接。請注意,如果不使用測試環境並進行手動測試,則可以運行似乎是e.preventDefault()沒有觸發的錯誤 - 而是在點擊該鏈接時重新加載頁面。 –
你應該用「水豚」來標記這個問題,因爲這就是你正在使用的東西 - 不是直的硒 –