-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathweb_ut.py
More file actions
70 lines (54 loc) · 2.26 KB
/
web_ut.py
File metadata and controls
70 lines (54 loc) · 2.26 KB
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
import allure
import configparser
import os
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
@allure.feature('Test Baidu WebUI')
class ISelenium(unittest.TestCase):
# 读入配置文件
def get_config(self):
config = configparser.ConfigParser()
config.read(os.path.join(os.environ['HOMEPATH'], 'iselenium.ini'))
return config
def tearDown(self):
self.driver.quit()
def setUp(self):
config = self.get_config()
# 控制是否采用无界面形式运行自动化测试
try:
using_headless = os.environ["using_headless"]
except KeyError:
using_headless = None
print('没有配置环境变量 using_headless, 按照有界面方式运行自动化测试')
chrome_options = Options()
if using_headless is not None and using_headless.lower() == 'true':
print('使用无界面方式运行')
chrome_options.add_argument("--headless")
self.driver = webdriver.Chrome(options=chrome_options)
@allure.story('Test key word 今日头条')
def test_webui_1(self):
""" 测试用例1,验证'今日头条'关键词在百度上的搜索结果
"""
self._test_baidu('今日头条', 'test_webui_1')
@allure.story('Test key word 王者荣耀')
def test_webui_2(self):
""" 测试用例2, 验证'王者荣耀'关键词在百度上的搜索结果
"""
self._test_baidu('王者荣耀', 'test_webui_2')
def _test_baidu(self, search_keyword, testcase_name):
""" 测试百度搜索子函数
:param search_keyword: 搜索关键词 (str)
:param testcase_name: 测试用例名 (str)
"""
self.driver.get("https://www.baidu.com")
print('打开浏览器,访问 www.baidu.com')
time.sleep(5)
assert f'百度一下' in self.driver.title
elem = self.driver.find_element_by_name("wd")
elem.send_keys(f'{search_keyword}{Keys.RETURN}')
print(f'搜索关键词~{search_keyword}')
time.sleep(5)
self.assertTrue(f'{search_keyword}' in self.driver.title, msg=f'{testcase_name}校验点 pass')