forked from tungwaiyip/HTMLTestRunner
-
Notifications
You must be signed in to change notification settings - Fork 155
/
test_screenshot_selenium.py
150 lines (121 loc) · 4.1 KB
/
test_screenshot_selenium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
# @Time : 2017/9/6 11:26
# @File : aaa.py
# @Author : 守望@天空~
"""HTMLTestRunner 截图版示例 selenium 版"""
from selenium import webdriver
import unittest
import time
from HTMLTestRunner_cn import HTMLTestRunner
import sys
class case_01(unittest.TestCase):
"""
def setUp(cls):
cls.driver = webdriver.Chrome()
def tearDown(cls):
cls.driver.quit()
"""
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
@classmethod
def tearDownClass(cls):
cls.driver.quit()
def add_img(self):
# self.imgs.append(self.driver.get_screenshot_as_base64())
return True
def setUp(self):
# 在python3.x 中,如果在这里初始化driver ,因为3.x版本 unittest 运行机制不同,会导致用例失败后截图失败
self.imgs = []
self.addCleanup(self.cleanup)
def cleanup(self):
pass
def test_case1(self):
""" 百度搜索
呵呵呵呵
"""
print("本次校验没过?")
print ("超级长"*66)
self.driver.get("https://www.baidu.com")
self.add_img()
self.driver.find_element_by_id('kw').send_keys(u'百度一下')
self.add_img()
self.driver.find_element_by_id('su').click()
time.sleep(1)
self.add_img()
def test_case2(self):
"""搜狗首页"""
self.driver.get("http://www.sogou.com")
print("本次校验没过?")
self.assertTrue(False,"这是相当的睿智了")
def test_case3(self):
""" QQ邮箱"""
self.driver.get("https://mail.qq.com")
# self.imgs.append(self.driver.get_screenshot_as_base64())
print("没法打印?")
self.assertIn(u"中文", u'中华','小当家?')
def test_case4(self):
u""" 淘宝"""
self.driver.get("http://www.taobao.com/")
raise Exception
self.add_img()
self.assertTrue(True)
class case_02(unittest.TestCase):
"""
def setUp(cls):
cls.driver = webdriver.Chrome()
def tearDown(cls):
cls.driver.quit()
"""
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
@classmethod
def tearDownClass(cls):
cls.driver.quit()
def add_img(self):
# self.imgs.append(self.driver.get_screenshot_as_base64())
return True
def setUp(self):
# 在是python3.x 中,如果在这里初始化driver ,因为3.x版本 unittest 运行机制不同,会导致用力失败时截图失败
self.imgs = []
self.addCleanup(self.cleanup)
def cleanup(self):
pass
def test_case1(self):
""" 百度搜索
呵呵呵呵
"""
print("校验了一下")
self.driver.get("https://www.baidu.com")
self.add_img()
self.driver.find_element_by_id('kw').send_keys(u'百度一下')
self.add_img()
self.driver.find_element_by_id('su').click()
time.sleep(1)
self.add_img()
@unittest.skip('跳过')
def test_case2(self):
"""搜狗首页"""
self.driver.get("http://www.sogou.com")
print("本次校验没过?")
self.assertTrue(False,"这是相当的睿智了")
def test_case3(self):
""" QQ邮箱"""
self.driver.get("https://mail.qq.com")
# self.imgs.append(self.driver.get_screenshot_as_base64())
print("没法打印?")
self.assertIn(u"中文", u'中文')
def test_case4(self):
u""" 淘宝"""
self.driver.get("http://www.taobao.com/")
self.add_img()
self.assertTrue(True)
if __name__ == "__main__":
from unittest import TestResult
suite1 = unittest.TestLoader().loadTestsFromTestCase(case_01)
suite2 = unittest.TestLoader().loadTestsFromTestCase(case_02)
suites = unittest.TestSuite()
suites.addTests([suite1,suite2])
runer = HTMLTestRunner(title="带截图的测试报告", description="小试牛刀", stream=open("sample_test_report.html", "wb"), verbosity=2, retry=2, save_last_try=True)
runer.run(suites)