forked from defnngj/pyautoTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
193 lines (157 loc) · 5.3 KB
/
conftest.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import pytest
from py.xml import html
from selenium import webdriver
from selenium.webdriver import Remote
from selenium.webdriver.chrome.options import Options as CH_Options
from selenium.webdriver.firefox.options import Options as FF_Options
# 项目目录配置
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
REPORT_DIR = BASE_DIR + "/test_report/"
driver = None
############################
# 配置浏览器驱动类型(chrome/firefox/chrome-headless/firefox-headless)。
driver_type = "chrome"
# 配置运行的 URL
url = "https://www.baidu.com"
# 失败重跑次数
rerun = "3"
# 当达到最大失败数,停止执行
max_fail = "5"
# 运行测试用例的目录或文件
cases_path = "./test_dir/"
############################
# 定义基本测试环境
@pytest.fixture(scope='function')
def base_url():
global url
return url
# 设置用例描述表头
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
cells.insert(2, html.th('Description'))
cells.pop()
# 设置用例描述表格
@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
cells.insert(2, html.td(report.description))
cells.pop()
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
用于向测试用例中添加用例的开始时间、内部注释,和失败截图等.
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
report.description = description_html(item.function.__doc__)
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
case_path = report.nodeid.replace("::", "_") + ".png"
if "[" in case_path:
case_name = case_path.split("-")[0] + "].png"
else:
case_name = case_path
capture_screenshots(case_name)
img_path = "image/" + case_name.split("/")[-1]
if img_path:
html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % img_path
extra.append(pytest_html.extras.html(html))
report.extra = extra
def description_html(desc):
"""
将用例中的描述转成HTML对象
:param desc: 描述
:return:
"""
if desc is None:
return "No case description"
desc_ = ""
for i in range(len(desc)):
if i == 0:
pass
elif desc[i] == '\n':
desc_ = desc_ + ";"
else:
desc_ = desc_ + desc[i]
desc_lines = desc_.split(";")
desc_html = html.html(
html.head(
html.meta(name="Content-Type", value="text/html; charset=latin1")),
html.body(
[html.p(line) for line in desc_lines]))
return desc_html
def capture_screenshots(case_name):
"""
配置用例失败截图路径
:param case_name: 用例名
:return:
"""
global driver
file_name = case_name.split("/")[-1]
new_report_dir = new_report_time()
if new_report_dir is None:
raise RuntimeError('没有初始化测试目录')
image_dir = os.path.join(REPORT_DIR, new_report_dir, "image", file_name)
driver.save_screenshot(image_dir)
def new_report_time():
"""
获取最新报告的目录名(即运行时间,例如:2018_11_21_17_40_44)
"""
files = os.listdir(REPORT_DIR)
files.sort()
try:
return files[-1]
except IndexError:
return None
# 启动浏览器
@pytest.fixture(scope='session', autouse=True)
def browser():
"""
全局定义浏览器驱动
:return:
"""
global driver
global driver_type
if driver_type == "chrome":
# 本地chrome浏览器
driver = webdriver.Chrome()
driver.set_window_size(1920, 1080)
elif driver_type == "firefox":
# 本地firefox浏览器
driver = webdriver.Firefox()
driver.set_window_size(1920, 1080)
elif driver_type == "chrome-headless":
# chrome headless模式
chrome_options = CH_Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_options)
elif driver_type == "firefox-headless":
# firefox headless模式
firefox_options = FF_Options()
firefox_options.headless = True
driver = webdriver.Firefox(firefox_options=firefox_options)
elif driver_type == "grid":
# 通过远程节点运行
driver = Remote(command_executor='http://10.2.16.182:4444/wd/hub',
desired_capabilities={
"browserName": "chrome",
})
driver.set_window_size(1920, 1080)
else:
raise NameError("driver驱动类型定义错误!")
return driver
# 关闭浏览器
@pytest.fixture(scope="session", autouse=True)
def browser_close():
yield driver
driver.quit()
print("test end!")
if __name__ == "__main__":
capture_screenshot("test_dir/test_baidu_search.test_search_python.png")