学了几天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
>>>
当然这个执行结果的样式是丑了点。有解决方法的。后续再说。