我期待到使用水豚,WebKit的執行應用程序的有些貼近現實的考驗。這絕對是必要的,因爲該應用程序具有非常豐富的基於JS的UI,並且Rails部分主要是API調用。Rails + Capybara-webkit - javascript代碼覆蓋率?
的問題是:是否有任何工具集成到測試管道可能儀器Javascript代碼,並報告其覆蓋範圍?這裏的關鍵是能夠輕鬆地集成到測試工作流程(就像rcov/simplecov) - 我不喜歡這個想法用jscoverage或模擬器自己做。:)
非常感謝提前。
我期待到使用水豚,WebKit的執行應用程序的有些貼近現實的考驗。這絕對是必要的,因爲該應用程序具有非常豐富的基於JS的UI,並且Rails部分主要是API調用。Rails + Capybara-webkit - javascript代碼覆蓋率?
的問題是:是否有任何工具集成到測試管道可能儀器Javascript代碼,並報告其覆蓋範圍?這裏的關鍵是能夠輕鬆地集成到測試工作流程(就像rcov/simplecov) - 我不喜歡這個想法用jscoverage或模擬器自己做。:)
非常感謝提前。
更新:從JSCover版本1.05開始,我在我以前的答案中概述的黑客不再需要。我已經更新了我的答案以反映這一點。
我設法JSCover到Rails +水豚管道的工作,但它確實需要一些黑客得到它的工作。我建立了一個小耙子任務:
然後,我添加了一個設置任務在試驗開始和結束時寫出完整的報告拆除任務,以清除瀏覽器的localStorage的。
def setup
unless $startup_once
$startup_once=true
puts 'Clearing localStorage'
visit('/')
page.execute_script('localStorage.removeItem("jscover");')
end
end
def teardown
out=page.evaluate_script("typeof(_$jscoverage)!='undefined' && jscoverage_serializeCoverageToJSON()")
unless out.blank? then
File.open(File.join(Rails.root,"public/assets/jscoverage.json"), 'w') {|f| f.write(out) }
end
end
不管怎樣,最終的結果很好地工作,做這種方式的優點是,它也適用於無頭的瀏覽器,因此它也可以被包括在CI。
***更新2:這裏是自動的步驟rake任務,在/ lib /任務
# Coverage testing for JavaScript
#
# Usage:
# Download JSCover from: http://tntim96.github.io/JSCover/ and move it to
# ~/Applications/JSCover-1
# First instumentalize the javascript files:
# rake assets:coverage
# Then run browser tests
# rake test
# See the results in the browser
# http://localhost:3000/assets/jscoverage.html
# Don't forget to clean up instrumentalization afterwards:
# rake assets:clobber
# Also don't forget to re-do instrumentalization after changing a JS file
namespace :assets do
desc 'Instrument all the assets named in config.assets.precompile'
task :coverage do
Rake::Task["assets:coverage:primary"].execute
end
namespace :coverage do
def jscoverage_loc;Dir.home+'/Applications/JSCover-1/';end
def internal_instrumentalize
config = Rails.application.config
target=File.join(Rails.public_path,config.assets.prefix)
environment = Sprockets::Environment.new
environment.append_path 'app/assets/javascripts'
`rm -rf #{tmp=File.join(Rails.root,'tmp','jscover')}`
`mkdir #{tmp}`
`rm -rf #{target}`
`mkdir #{target}`
print 'Generating assets'
require File.join(Rails.root,'config','initializers','assets.rb')
(%w{application.js}+config.assets.precompile.select{|f| f.is_a?(String) && f =~ /\.js$/}).each do |f|
print '.';File.open(File.join(target,f), 'w') {|ff| ff.write(environment[f].to_s) }
end
puts "\nInstrumentalizing…"
`java -Dfile.encoding=UTF-8 -jar #{jscoverage_loc}target/dist/JSCover-all.jar -fs #{target} #{tmp} #{'--no-branch' unless ENV['C1']} --local-storage`
puts 'Copying into place…'
`cp -R #{tmp}/ #{target}`
`rm -rf #{tmp}`
File.open("#{target}/jscoverage.js",'a'){|f| f.puts 'jscoverage_isReport = true' }
end
task :primary => %w(assets:environment) do
unless Dir.exist?(jscoverage_loc)
abort "Cannot find JSCover! Download from: http://tntim96.github.io/JSCover/ and put in #{jscoverage_loc}"
end
internal_instrumentalize
end
end
end
這已經被添加到JSCover(樹幹)下降這一點 - 相關線程JSCover是here。
我設法JSCover到Rails +水豚管道的工作,但 它確實需要相當多的黑客來得到它的工作
現在這些變化是在JSCover的軀幹和會部分版本1.0.5。有一些工作示例(包括一個Selenium IDE錄製的示例)以及其中的文檔。
沒有得到分支檢測到 工作,因爲它使用的是不容易被序列化JSON
這是做到這一點的函數,而在新的使用對象需要一些額外的工作碼。
不管怎樣,最終的結果很好地工作
我同意。這使得JSCover可以使用更高級別的工具,這些工具不能很好地處理iFrame或通過此方法避免的多個窗口。這也意味着代碼覆蓋率可以添加到現有的Selenium測試有兩項調整:
有關更多信息,請參閱JSCover的文檔。包含這些更改的版本1.0.5應該在幾天內發佈。
謝謝,現在新版本已發佈,我已經更新了以前的答案以反映這些更改 –
我也對此感興趣。不禁要問,是否可以根據請求規格獲得js覆蓋率。 – steve