第一章 Selenium+WebDriver环境搭建
第二章 Selenium定位方式
第三章 元素常用属性
第四章 自动化中的三种等待
第五章 自动化浏览器设置及句柄、窗口切换操作
第六章 鼠标、键盘操作
第七章 javascript在自动化中的应用
第八章 unittest&断言
第九章 ddt数据驱动
第十章 测试框架搭建过程Python+Selenium+unittest+BeautifulReport
1、假设有两条测试用例,如下:
用例编号 | 用例描述 | 预期结果 | 实际结果 | 运行结果 |
---|---|---|---|---|
test_001 | 打卡百度首页,输入搜索词“内卷”,点击搜索,校验跳转页面title包含“百度” | 跳转页面title包含“百度” | 跳转页面title包含“百度” | 成功 |
test_002 | 打卡百度首页,输入搜索词“躺平”,点击搜索,校验跳转页面title不包含“百度” | 跳转页面title不包含“百度” | 跳转页面title包含“百度” | 失败 |
1、首先,我们用最简单的方法,在一个文件中写出两条测试用例。
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait as Wait
# 测试用例一,搜索"内卷"
driver = webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.baidu.com")
search_ele = (By.ID, "kw")
click_ele = (By.ID, "su")
# 定位搜索栏,输入搜索词
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).send_keys("内卷")
# 定位搜索按钮,并点击
Wait(driver, 5).until(EC.presence_of_element_located(click_ele)).click()
time.sleep(3)
driver.quit()
# 测试用例二,搜索"躺平"
driver = webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.baidu.com")
search_ele = (By.ID, "kw")
click_ele = (By.ID, "su")
# 定位搜索栏,输入搜索词
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).send_keys("躺平")
# 定位搜索按钮,并点击
Wait(driver, 5).until(EC.presence_of_element_located(click_ele)).click()
time.sleep(3)
driver.quit()
2、然后,我们可以将两条用例分别定义成函数。
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait as Wait
# 测试用例一,搜索"内卷"
def test_001():
driver = webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.baidu.com")
search_ele = (By.ID, "kw")
click_ele = (By.ID, "su")
# 定位搜索栏,输入搜索词
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).send_keys("内卷")
# 定位搜索按钮,并点击
Wait(driver, 5).until(EC.presence_of_element_located(click_ele)).click()
time.sleep(3)
driver.quit()
# 测试用例二,搜索"躺平"
def test_002():
driver = webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.baidu.com")
search_ele = (By.ID, "kw")
click_ele = (By.ID, "su")
# 定位搜索栏,输入搜索词
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).send_keys("躺平")
# 定位搜索按钮,并点击
Wait(driver, 5).until(EC.presence_of_element_located(click_ele)).click()
time.sleep(3)
driver.quit()
if __name__ == '__main__':
test_001()
test_002()
3、接下来,我们就要加上单元测试方法unittest,同时可以将重复的步骤放在前置步骤和后置步骤中执行。
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait as Wait
import unittest
class Test_search(unittest.TestCase):
@classmethod
def setUpClass(cls):
# 将driver定义成全局变量,可以供下面函数使用
global driver
driver = webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.baidu.com")
@classmethod
def tearDownClass(cls):
time.sleep(3)
driver.quit()
# 测试用例一,搜索"内卷"
def test_001(self):
search_ele = (By.ID, "kw")
click_ele = (By.ID, "su")
# 因为setUpClass方法浏览器只打开一次,所以我们需要清空搜索栏
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).clear()
# 定位搜索栏,输入搜索词
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).send_keys("内卷")
# 定位搜索按钮,并点击
Wait(driver, 5).until(EC.presence_of_element_located(click_ele)).click()
time.sleep(3)
# 测试用例二,搜索"躺平"
def test_002(self):
search_ele = (By.ID, "kw")
click_ele = (By.ID, "su")
# 因为setUpClass方法浏览器只打开一次,所以我们需要清空搜索栏
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).clear()
# 定位搜索栏,输入搜索词
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).send_keys("躺平")
# 定位搜索按钮,并点击
Wait(driver, 5).until(EC.presence_of_element_located(click_ele)).click()
time.sleep(3)
if __name__ == '__main__':
# 创建测试套件
suite = unittest.TestCase()
# 添加测试用例类中的所有用例
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(Test_search))
# 执行测试套件,输出文本报告
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
这时,我们就可以通过文本报告看见测试用例的执行情况,但同时存在问题,第二次搜索是直接在第一次结果上进行的搜索。测试案例一和二除了搜索词,其他完全一样,我们可以再将搜索词定义出来。
4、下一步,我们可以将搜索词移出去,新建一个Datas文件夹存放,搜索词等放入yaml文件中便后期管理。同时,导包ddt,用来加载测试数据。
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait as Wait
import unittest
import ddt
@ddt.ddt
class Test_search(unittest.TestCase):
@classmethod
def setUpClass(cls):
# 将driver定义成全局变量,可以供下面函数使用
global driver
driver = webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.baidu.com")
@classmethod
def tearDownClass(cls):
time.sleep(3)
driver.quit()
# 测试用例一、二,搜索"内卷"、"躺平"
@ddt.file_data("./Datas/search_words.yaml")
def test_search(self, search_word):
search_ele = (By.ID, "kw")
click_ele = (By.ID, "su")
# 因为setUpClass方法浏览器只打开一次,所以我们需要清空搜索栏
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).clear()
# 定位搜索栏,输入搜索词
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).send_keys(search_word)
# 定位搜索按钮,并点击
Wait(driver, 5).until(EC.presence_of_element_located(click_ele)).click()
time.sleep(3)
if __name__ == '__main__':
# 创建测试套件
suite = unittest.TestCase()
# 添加测试用例类中的所有用例
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(Test_search))
# 执行测试套件,输出文本报告
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
这里可以看出,在yaml文件中的所有搜索词都会作为一条用例去单独执行。
5、最后我们加入断言,去判断预期结果与实际结果是否一致。安装BeautifulReport库,可以让测试报告更加美观。再将执行测试用例方法单独写一个执行文件。最终结构目录,如下图:
search_words.yaml文件:
- 内卷
- 躺平
test_search.py文件:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait as Wait
import unittest
import ddt
@ddt.ddt
class Test_search(unittest.TestCase):
@classmethod
def setUpClass(cls):
# 将driver定义成全局变量,可以供下面函数使用
global driver
driver = webdriver.Chrome()
driver.implicitly_wait(3)
driver.get("https://www.baidu.com")
@classmethod
def tearDownClass(cls):
time.sleep(3)
driver.quit()
# 测试用例一、二,搜索"内卷"、"躺平"
@ddt.file_data("../Datas/search_words.yaml")
def test_search(self, search_word):
search_ele = (By.ID, "kw")
click_ele = (By.ID, "su")
# 因为setUpClass方法浏览器只打开一次,所以我们需要清空搜索栏
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).clear()
# 定位搜索栏,输入搜索词
Wait(driver, 5).until(EC.presence_of_element_located(search_ele)).send_keys(search_word)
# 定位搜索按钮,并点击
Wait(driver, 5).until(EC.presence_of_element_located(click_ele)).click()
time.sleep(3)
#获取当前页面title
title = driver.title
assert title == f"{search_word}_百度搜索", f"预期结果是‘{search_word}_百度搜索’, 实际结果是‘{title}’"
run.py文件
from BeautifulReport import BeautifulReport
import unittest
if __name__ == '__main__':
suite = unittest.TestLoader().discover("./testCases/", pattern="test_*.py")
result = BeautifulReport(suite)
result.verbosity = 2
result.report(filename="report.html", description="拒绝内卷的小测试_测试报告", report_dir="./reports")
执行run.py文件就可以得到最终的测试报告。
项目打包已上传资源,点击下载。或者直接github自取,点击下载,谢谢支持!
随手点赞一次,运气增加一份。