-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathtest_selenium.py
More file actions
75 lines (61 loc) · 2.2 KB
/
test_selenium.py
File metadata and controls
75 lines (61 loc) · 2.2 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
71
72
73
74
75
import os
import unittest
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from XTestRunner import HTMLTestRunner
from config import REPORTS_DIR
"""
1.安装selenium
> pip install selenium
2. 驱动必须定义为 `self.driver/cls.driver`, 否则无法生成截图
3. `self.images` 用于存储图片的list,可以手动添加图片
"""
class YouTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.driver = webdriver.Chrome()
cls.base_url = "https://cn.bing.com/"
@classmethod
def tearDownClass(cls) -> None:
cls.driver.quit()
def test_success(self):
"""测试bing搜索:XTestRunner """
self.driver.get(self.base_url)
sleep(2)
search = self.driver.find_element(By.ID, "sb_form_q")
search.send_keys("XTestRunner")
search.submit()
sleep(2)
def test_error(self):
"""测试bing搜索,定位失败 """
self.driver.get(self.base_url)
sleep(2)
self.driver.find_element(By.ID, "sb_form_qxxx").send_keys("python")
sleep(2)
def test_fail(self):
"""测试bing搜索,断言失败 """
self.driver.get(self.base_url)
self.driver.find_element(By.ID, "sb_form_q").send_keys("unittest")
self.assertEqual(self.driver.title, "unittest")
def test_screenshots(self):
"""测试截图"""
self.driver.get(self.base_url)
sleep(2)
# 元素截图
elem = self.driver.find_element(By.ID, "sb_form_q")
self.images.append(elem.screenshot_as_base64)
# 竖屏截图
self.images.append(self.driver.get_screenshot_as_base64())
# 最大化截图
self.driver.maximize_window()
self.images.append(self.driver.get_screenshot_as_base64())
if __name__ == '__main__':
html_report = os.path.join(REPORTS_DIR, "test_selenium.html")
with open(html_report, 'wb') as fp:
unittest.main(testRunner=HTMLTestRunner(
stream=fp,
tester="虫师",
title='Selenium自动化测试报告',
description=['类型:selenium', '操作系统:Windows', '浏览器:Chrome']
))