Watir - 頁面效能



Watir 頁面效能功能允許你追蹤響應時間度量,可以在 Chrome、Firefox、IE9 以上版本正常工作。截止目前,Safari 瀏覽器不支援該功能。

讓我們深入瞭解如何使用該功能。為使用該功能,我們需要使用 gem 安裝 watir-performance,如下所示 −

命令

gem install watir-performance
Watir Performance

我們已安裝完畢 watir-performance。支援的度量包括 −

  • 摘要
  • 導航
  • 記憶體
  • 計時

此處討論了使用 watir-performance 的一個實際示例。在這個示例中,我們將檢查站點 www.tutorialspoint.com 的響應時間,如下所示 −

require 'watir'
require 'watir-performance'
10.times do
   b = Watir::Browser.new :chrome
   b.goto 'https://tutorialspoint.tw'
   load_secs = b.performance.summary[:response_time] / 1000
   puts "Load Time: #{load_secs} seconds."
   b.close
end

輸出

Load Time: 7 seconds.
Load Time: 7 seconds.
Load Time: 5 seconds.
Load Time: 5 seconds.
Load Time: 6 seconds.
Load Time: 5 seconds.
Load Time: 5 seconds.
Load Time: 13 seconds.
Load Time: 12 seconds.
Load Time: 5 seconds.

使用 performance.timing

require 'watir'
require 'watir-performance'

b = Watir::Browser.new :chrome
b.goto 'https://tutorialspoint.tw'
load_secs = b.performance.timing[:response_end] - b.performance.timing[:response_start]
puts "Time taken to respond is #{load_secs} seconds."
b.close

輸出

Time taken to respond is 41 seconds.

使用 performance.navigation

require 'watir'
require 'watir-performance'

b = Watir::Browser.new :chrome
b.goto 'https://tutorialspoint.tw'
perf_nav = b.performance.navigation
puts "#{perf_nav}"
b.close

輸出

{:type_back_forward=>2, :type_navigate=>0, :type_reload=>1, 
:type_reserved=>255, :redirect_count=>0, :to_json=>{}, :type=>0}

使用 performance.memory

require 'watir'
require 'watir-performance'

b = Watir::Browser.new :chrome
b.goto 'https://tutorialspoint.tw'
memory_used = b.performance.memory
puts "#{memory_used}"
b.close

輸出

{:js_heap_size_limit=>2, :type_navigate=>0, :type_reload=>1, :ty2136997888, 
:total_js_heap_size=>2, :type_navigate=>0, :type_reload=>1, :ty12990756, 
:used_js_heap_size=>2, :type_navigate=>0, :type_reload=>1, :ty7127092}
廣告