zoukankan      html  css  js  c++  java
  • Python自动化测试工具Splinter简介和使用实例

    Splinter 快速介绍

    官方网站:http://splinter.cobrateam.info/

    官方介绍:

    Splinter is an open source tool for testingweb applications using Python. It lets you automate browser actions, such asvisiting URLs and interacting with their items

    特性:

    1、可以模拟浏览器行为,访问指定的URL,并且可以指定不同的浏览器类型。比如firefox或者chrome等。不同的浏览器只要在本地安装对应的驱动,就可以在代码中通过名称指定来访问。
    2、支持cookie操作,可以很方便的添加和删除cookie;
    3、支持模拟鼠标的动作,比如滑动到某个按钮上,焦点离开某个按钮等等,对于带有动态提示的页面,如搜索引擎的关键字输入框的动态提示,可以非常方便的测试。
    4、支持模拟键盘的输入操作,对input等控件的输入可以模拟用户的type过程。
    5、支持直接运行js或者调用页面的js。
    6、支持模拟上传文件。
    7、对radio和checkbox有专门的api支持,非常方便;
    8、支持快速的获取页面的元素或者判断是否存在文本,用于开发判断页面提示信息是否准确非常方便。
    9、最重要的,splinter的API非常简单,配合官方的文档学习成本几乎是0,当然你得懂一些python语法。如果你比较了解js和css,你可能会像喜欢jquery一样喜欢它;

    功能:

    Splinter执行的时候会自动打开你指定的浏览器,访问指定的URL。
    然后你所开发的模拟的任何行为,都会自动完成,你只需要坐在电脑面前,像看电影一样看着屏幕上各种动作自动完成然后收集结果即可。


    举个例子,我们要回归登录功能,首先要开发如下模拟登录行为的脚本:

     1 #!/usr/bin/py2
     2 # -*- coding: utf-8 -*-
     3 #encoding=utf-8
     4 import sys, re
     5 from splinter.browser import Browser 
     6 CLOASE_AFTER_TEST = False
     7 reload(sys)
     8 sys.setdefaultencoding('utf8')
     9 encoding = lambda x:x.encode('gbk') 
    10 def testLogin(desc, username, password, result):
    11     output(desc)      
    12     browser.fill('TPL_username',username.decode('utf8'))
    13     browser.fill('TPL_password',password.decode('utf8'))
    14     browser.find_by_value('登录').first.click()
    15     checkresult(result) 
    16 def output(x):
    17     print encoding(x) 
    18 def resultMsg(x):
    19     if x == True:
    20         print 'pass'
    21     else:
    22         print '[X]not pass'
    23 def checkresult(x):
    24     """  check result message, x : the error message u want  """
    25     resultMsg(browser.is_text_present(x)) 
    26 __testUrl = 'http://waptest.taobao.com/login/login.htm?tpl_redirect_url=http%3A%2F%2Fm.taobao.com%2F' 
    27 # chrome driver : http://code.google.com/p/selenium/wiki/ChromeDriver
    28 browser = Browser()  # already support firefox
    29 browser.visit(__testUrl)
    30 output("测试页面:"+browser.title) 
    31 try:
    32     # test login
    33     testLogin('测试未输入用户名','','','请输入会员名')
    34     testLogin('测试未输入密码','qd_test_001','','请输入密码')
    35     testLogin('测试帐户不存在','这是一个不存在的名字哦','xxxxxxx','该账户名不存在')
    36     testLogin('测试成功登录','qd_test_001','taobao1234','继续登录前操作') 
    37     # test find password
    38     output("测试[找回密码]链接")
    39     browser.visit(__testUrl)
    40     backPasswordLink = browser.find_link_by_text('取回密码')
    41     if 1 == len(backPasswordLink):
    42         backPasswordLink.first.click()
    43         ru = re.findall(re.compile(".*(reg/gp.htm).*", re.IGNORECASE), browser.url)
    44         if ru is not None:
    45             checkresult('找回密码')
    46         else:
    47             output("测试找回密码链接失败") 
    48 except Exception,x:
    49     print x 
    50 if CLOASE_AFTER_TEST:
    51     browser.quit()
  • 相关阅读:
    BUAA2020个人博客作业小结
    BUAA2020软工热身作业小结
    个人博客作业----总结
    个人阅读作业7
    超链接按钮点击变色,原来的链接恢复原色
    setInterval和setTimeout的区别以及setInterval越来越快问题的解决方法
    自定义网站404页面
    jQuery实现的上下滚动公告栏详细讲解
    K先生的博客
    Bootstrap4响应式布局之栅格系统
  • 原文地址:https://www.cnblogs.com/liu-ke/p/5026379.html
Copyright © 2011-2022 走看看