用selenium自动化测试工具编写web响应时间探测程序(python)最详细版本

首先安装webdriver(要与浏览器版本对应,这方面帖子很多,就不介绍了)

web时间的获取模型如下图:

在这里插入图片描述
其中参数的意义:
navigationStart 加载起始时间
redirectStart 重定向开始时间(如果发生了HTTP重定向,每次重定向都和当前文档同域的话,就返回开始重定向的fetchStart的值。其他情况,则返回0)
redirectEnd 重定向结束时间(如果发生了HTTP重定向,每次重定向都和当前文档同域的话,就返回最后一次重定向接受完数据的时间。其他情况则返回0)
fetchStart 浏览器发起资源请求时,如果有缓存,则返回读取缓存的开始时间
domainLookupStart 查询DNS的开始时间。如果请求没有发起DNS请求,如keep-alive,缓存等,则返回fetchStart
domainLookupEnd 查询DNS的结束时间。如果没有发起DNS请求,同上
connectStart 开始建立TCP请求的时间。如果请求是keep-alive,缓存等,则返回domainLookupEnd
(secureConnectionStart) 如果在进行TLS或SSL,则返回握手时间
connectEnd 完成TCP链接的时间。如果是keep-alive,缓存等,同connectStart
requestStart 发起请求的时间
responseStart 服务器开始响应的时间
domLoading 从图中看是开始渲染dom的时间,具体未知
domInteractive 未知
domContentLoadedEventStart 开始触发DomContentLoadedEvent事件的时间
domContentLoadedEventEnd DomContentLoadedEvent事件结束的时间
domComplete 从图中看是dom渲染完成时间,具体未知
loadEventStart 触发load的时间,如没有则返回0
loadEventEnd load事件执行完的时间,如没有则返回0
unloadEventStart unload事件触发的时间
unloadEventEnd unload事件执行完的时间

以下是获取时间的代码:

page=[]
a = webdriver.Firefox()
获取的时间是一个javascript类型,需要用execute_script函数来获取
page.append(a.execute_script("return performance.timing.navigationStart"))
                page.append(a.execute_script("return performance.timing.redirectStart"))
                page.append(a.execute_script("return performance.timing.redirectEnd"))
                page.append(a.execute_script("return performance.timing.fetchStart"))
                page.append(a.execute_script("return performance.timing.domainLookupStart"))
                page.append(a.execute_script("return performance.timing.domainLookupEnd"))
                page.append(a.execute_script("return performance.timing.connectStart"))
                page.append(a.execute_script("return performance.timing.connenctEnd"))
                page.append(a.execute_script("return performance.timing.responseStart"))
                page.append(a.execute_script("return performance.timing.requestStart"))
                page.append(a.execute_script("return performance.timing.domLoading"))
                page.append(a.execute_script("return performance.timing.domComplete"))
                page.append(a.execute_script("return performance.timing.loadEventStart"))
                page.append(a.execute_script("return performance.timing.loadEventEnd "))
                page.append(a.execute_script("return performance.timing.responseStart") - a.execute_script("return performance.timing.requestStart"))  # 请求到响应时间
                page.append(a.execute_script("return performance.timing.redirectStart") - a.execute_script("return performance.timing.redirectEnd"))  # 重定向时间
                page.append(a.execute_script("return performance.timing.domainLookupEnd") - a.execute_script("return performance.timing.domainLookupStart"))  # DNS解析时间
                page.append(a.execute_script("return performance.timing.domComplete") - a.execute_script("return performance.timing.domLoading"))  # 页面渲染时间
                page.append(a.execute_script("return performance.timing.responseStart") - a.execute_script("return performance.timing.navigationStart"))  # 白屏时间

然后 print page 就可以获取所有时间了
这里获取的时间是毫秒戳

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值