使用unittest测试框架来写selenium的TestCase

学了几天selenium发现,它本身没有出测试报告和执行测试用例的功能【如果有的话,请各位不吝指出】,见到一篇文章的实例,他是用的unittest,确实是个解决方案,留下来备用~!

# -*- coding: cp936 -*-

# 使用unittest框架来写TestCase
import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
    def test_search_in_python_org(self):
        self.driver = self.driver
        self.driver.get("http://www.baidu.com")
        self.assertIn("百度一下,你就知道" ,self.driver.title)
        elem = self.driver.find_element_by_id("kw1")
        time.sleep(3)
        elem.send_keys("selenium")
        elem.send_keys(Keys.RETURN)
        time.sleep(5)
        self.assertIn("百度一下,你就知道" ,self.driver.title)
    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()
    

执行结果:

>>> ================================ RESTART ================================
>>>
E
======================================================================
ERROR: test_search_in_python_org (__main__.PythonOrgSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "F:\Python_dev\python编程参考资料集\Selenium相关\python_org_search.py", line 15, in test_search_in_python_org
    self.assertIn("百度一下,你就知道" ,self.driver.title)
  File "D:\python\lib\unittest\case.py", line 802, in assertIn
    if member not in container:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 0: ordinal not in range(128)

----------------------------------------------------------------------
Ran 1 test in 4.219s

FAILED (errors=1)
>>>

一片红色的海洋,改成utf-8 也不行,目前还不知咋解决,Who can hlep me?

为了验证结果,先注释掉这两行代码。

再次执行结果:

>>> ================================ RESTART ================================
>>>
.
----------------------------------------------------------------------
Ran 1 test in 13.642s

OK
>>>

当然这个执行结果的样式是丑了点。有解决方法的。后续再说。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值