使用python参数化测试

思路

1)使用parameterized工具进行参数化(将测试数据传递给参数)
2)使用selenium框架操作浏览器
3)使用unittest框架进行验证

设计python脚本

# -*- coding: utf-8 -*-


from selenium.webdriver import *
from time import *
import unittest
from parameterized import parameterized
import sys

reload(sys)
sys.setdefaultencoding('utf-8')


class SearchTest(unittest.TestCase):
    """
    搜索测试类
    """
    driver = None

    @parameterized.expand([
        ("https://cn.bing.com", "sb_form_q", "sb_form_go", u"赵丽颖", u"冯绍峰"),
        ("https://www.sogou.com/", "query", "stb", u"冯绍峰", u"赵丽颖")
    ])
    def test_search(self, url, input_box_id, search_btn_id, keyword, expected):
        """
        测试用例
        :param url:
        :param input_box_id:
        :param search_btn_id:
        :param keyword:
        :param expected:
        :return:
        """
        # 最大化窗口
        self.driver.maximize_window()
        # 设置默认的等待时长
        self.driver.implicitly_wait(15)
        # 打开网页
        self.driver.get(url)
        # 点击搜索框
        self.driver.find_element_by_id(input_box_id).click()
        # 输入关键字
        self.driver.find_element_by_id(input_box_id).send_keys(keyword)
        # 点击搜索按钮
        self.driver.find_element_by_id(search_btn_id).click()
        sleep(2)
        self.assertIn(expected, self.driver.page_source)

    @classmethod
    def setUpClass(cls):
        # 创建驱动工具
        cls.driver = Firefox()

    @classmethod
    def tearDownClass(cls):
        # 关闭浏览器
        cls.driver.close()


if __name__ == "__main__":
    # verbosity表示测试日志的详细级别
    unittest.main(verbosity=2)

运行python脚本

selenium190117.gif

参考资料

[1] unittest 官方教程
https://docs.python.org/3.7/library/unittest.html
[2] Python必会的单元测试框架unittest
https://www.jianshu.com/p/38948d0d73f5
[3] python unittest详解
https://www.jianshu.com/p/8e22c1213260
[4] UnicodeDecodeError: 'ascii' codec can't decode解决
http://www.cnblogs.com/angellst/p/8783190.html
[5] python中time模块的源码在哪里丫??
https://segmentfault.com/q/1010000012132002/a-1020000012133135

微信扫一扫关注该公众号【测试开发者部落】

image.png

点击链接加入群聊【软件测试学习交流群】
https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容