使用python实现测试工具(三)

乙醇 创建于 almost 6 years 之前

最后更新: about 2 months 之前

阅读数: 1

使用python实现测试工具(三)

本系列教程使用的python版本是3.6.3

背景

这一节我们实现一个简单的寻找页面上无效链接的工具。

我们这样定义无效链接: http响应的返回状态码 >=400的链接。 为什么?大家可以去搜索一下。

该工具的作用是访问某个页面,然后找到页面上所有的链接,依次对这些链接进行访问,通过返回的状态码来确定该链接是不是无效链接。

工具设计

我们设计一个命令行工具,给工具传1个参数,也就是该被测页面的url

所以工具大概是这样用的: python dead_link.py www.example.com

代码实现

简单起见,我们会用到requests-html库。安装文档在这里

from requests_html import HTMLSession
import requests
from sys import argv
from urllib.parse import urlparse, urljoin
DEBUG = True

USAGE = '''
USAGE:
python dead_link.py www.itest.info
'''

if len(argv) != 2:
  print(USAGE)
  exit(1)

script_name, url = argv 

if url[:4] != 'http':
  url = 'http://' + url

res = urlparse(url)
if res.netloc == '':
  print('无法获取站点的domain信息')
  exit(1)

domain = res.netloc
print(f"站点domain: {domain}")

session = HTMLSession()
r = session.get(url)

links = r.html.find('a')

for link in links:
  if 'href' in link.attrs:
    href = link.attrs['href']
  else:
    continue
  result = urlparse(href)
  if result.netloc == '':
    href = urljoin(url, href)
    url_type = '内链'
  else:
    if domain in href:
      url_type = '内链'
    else:
      url_type = '外链'
  try:
    response = requests.get(href)
    if response.status_code >= 400:
      print(f"{url_type} {href} 失败")
    else:
      print(f"{url_type} {href} 成功")
  except:
    print("出现异常")

精讲

  • 我们需要注意的是内链的情况,也就是有些链接是/xxx/yyy/zzz的情况,这种情况下需要将域名或者ip补全再访问
  • requests-html库的基本使用参考这里

运行示例

python dead_link.py www.itest.info

站点domain: www.itest.info
内链 http://www.itest.info/ 成功
内链 http://www.itest.info/ 成功
内链 http://www.itest.info/newclass 成功
内链 http://www.itest.info 成功
内链 http://www.itest.info/courses/8 成功
内链 http://www.itest.info/courses/7 成功
内链 http://www.itest.info/courses/6 成功
内链 http://www.itest.info/courses/3 成功
内链 http://www.itest.info/courses/2 成功
内链 http://www.itest.info/faq 成功
内链 http://www.itest.info/videos 成功
外链 http://www.testclass.net 成功
外链 http://www.testpub.cn 成功
内链 http://www.itest.info/about 成功
内链 http://www.itest.info/courses/2 成功
内链 http://www.itest.info/courses/7 成功
内链 http://www.itest.info/courses/7 成功
内链 http://www.itest.info/courses/6 成功
内链 http://www.itest.info/courses/6 成功
内链 http://www.itest.info/courses/3 成功
内链 http://www.itest.info/courses/3 成功
内链 http://www.itest.info/courses/2 成功
内链 http://www.itest.info/courses/2 成功
内链 http://www.itest.info/about 成功
外链 http://www.cnblogs.com/fnng/ 成功
外链 http://www.cnblogs.com/nbkhic/ 成功
外链 http://testpub.cn/ 成功
外链 http://www.testclass.net/ 成功
内链 http://www.itest.info/about 成功

动手时间

  • 抄一遍代码,看自己能不能运行起来
  • 给这段代码每一行都加上注释,理解代码做了些什么
  • 试着将上面的脚本改为实现判断页面上是否有无效图片的工具

扩展阅读

源码地址

github地址

0

相关课程

mockito简明教程
图文
mockito简明教程

课程分类: 测试工具

mock工具

  • 已完结
  • 已更新7集
  • 最后更新时间: 2024-03-18 12:50:29

免费

查看详情
requests从入门到精通
图文
requests从入门到精通

课程分类: 测试工具 接口测试

python接口测试必会

  • 已完结
  • 已更新16集
  • 最后更新时间: 2024-03-18 12:54:40

免费

查看详情
Locust实用教程
图文
Locust实用教程

课程分类: 性能测试 测试工具

python语言实现的非常出色性能测试工具

  • 已完结
  • 已更新9集
  • 最后更新时间: 2024-03-18 12:24:59

免费

查看详情
TDD测试驱动开发教程
图文
TDD测试驱动开发教程

课程分类: 测试框架 软件测试基础

TDD其实并不神秘

  • 已完结
  • 已更新7集
  • 最后更新时间: 2024-03-18 11:53:22

免费

查看详情
软件测试基础教程
图文
软件测试基础教程

课程分类: 软件测试基础

转码和转行必备

  • 已完结
  • 已更新9集
  • 最后更新时间: 2024-03-18 11:40:05

免费

查看详情
软件测试入门教程
图文
软件测试入门教程

课程分类: 软件测试基础

新人如何转码到软件测试

  • 已完结
  • 已更新9集
  • 最后更新时间: 2024-03-17 11:07:23

免费

查看详情