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.com'
   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.com'
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.com'
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.com'
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}
广告