上一小節(jié) Selenium(一)——Page Object Model (POM) 已經(jīng)把新浪微博登陸頁的頁面對象編寫完成,接下來繼續(xù)編寫測試用例拓春。
1. 簡介
先安裝兩個python包:
pip install pytest
pip install pytest-html
pytest 是一個測試框架,可以輕松構(gòu)建簡單且可擴(kuò)展的測試。pytest-html
是 pytest 的一個插件,它為測試結(jié)果生成一個HTML報告邮利。pytest 中的 fixture
pytest 中的 fixture 可以類比于 unittest 中的 setUp&tearDown,但是又在此基礎(chǔ)上有所改進(jìn)垃帅,比如支持參數(shù)化等延届。pytest-html 的報告格式
為了遵守內(nèi)容安全策略(CSP),默認(rèn)情況下運用命令:pytest --html=report.html
生成的測試報告會單獨存儲CSS和圖像等多個資源贸诚。您也可以創(chuàng)建一個獨立的報告方庭,在共享結(jié)果時更方便。這可以通過以下命令完成:
pytest --html=report.html --self-contained-html
2. 編寫測試用例
運用 pytest 的參數(shù)化功能@pytest.mark.parametrize
酱固,為同一個測試方法傳入不同的測試數(shù)據(jù)械念,可以將一個測試用例拓展成多個,避免了冗余代碼运悲。
參數(shù)化過程很簡單龄减,先準(zhǔn)備好測試數(shù)據(jù),在“新浪登陸頁面測試”中測試數(shù)據(jù)是多個元組拼成的一個列表扇苞,每個元組中裝載了各不相同的帳號欺殿、密碼寄纵、檢查信息:
testdata = [('', '', '請輸入登錄名'), # 帳號鳖敷、密碼為空
('haha', '', '請輸入密碼'), # 密碼為空
('', 'hehe', '請輸入登錄名'), # 帳號為空
('haha', 'hehe', '用戶名或密碼錯誤脖苏。查看幫助'), # 帳號、密碼錯誤
('haha@163.com', 'hehe', '請?zhí)顚戲炞C碼'), # 密碼錯誤
]
然后定踱,用 pytest 提供的@pytest.mark.parametrize
裝飾器棍潘,將數(shù)據(jù)綁定到測試方法上,就可以在方法中直接取用啦:
@pytest.mark.parametrize("username, password, message", testdata)
def test_login(self, username, password, message):
test_xl_login.py 內(nèi)容如下:
1 import pytest
2 from selenium import webdriver
3 from xl_login import LoginPage
4
5
6 # 準(zhǔn)備好測試數(shù)據(jù)
7 testdata = [('', '', '請輸入登錄名'), # 帳號崖媚、密碼為空
8 ('haha', '', '請輸入密碼'), # 密碼為空
9 ('', 'hehe', '請輸入登錄名'), # 帳號為空
10 ('haha', 'hehe', '用戶名或密碼錯誤亦歉。查看幫助'), # 帳號、密碼錯誤
11 ('haha@163.com', 'hehe', '請?zhí)顚戲炞C碼'), # 密碼錯誤
12 ]
13
14 # 定義fixture畅哑,class內(nèi)有效
15 @pytest.fixture(scope="class")
16 def login_ini(request):
17 """
18 初始化瀏覽器和登陸頁面
19 完成測試后肴楷,結(jié)束driver
20 """
21 # 預(yù)打開頁面
22 base_url = 'https://weibo.com/'
23 # 頁面title
24 title = '微博-隨時隨地發(fā)現(xiàn)新鮮事'
25 # 打開Chrome瀏覽器
26 driver = webdriver.Chrome()
27 # 登陸頁面初始化
28 request.cls.login = LoginPage(driver, base_url, title)
29 # 卸載夾具,以下代碼將在最后一個測試用例結(jié)束后執(zhí)行
30 yield login_ini
31 driver.quit()
32
33 # 使用fixture
34 @pytest.mark.usefixtures("login_ini")
35 class TestLogin():
36 # 參數(shù)化
37 @pytest.mark.parametrize("username, password, message", testdata)
38 def test_login(self, username, password, message):
39 """
40 測試用例
41 """
42 login = self.login
43 login.open()
44 # 輸入用戶名
45 login.type_username(username)
46 # 輸入密碼
47 login.type_password(password)
48 # 點擊登陸
49 login.submit()
50 # 獲取提示框信息
51 ms = login.get_message()
52 # 判定檢測
53 assert ms == messagegok
現(xiàn)在荠呐,創(chuàng)建一個 conftest.py 文件赛蔫,用于配置測試報告,以免報告中的中文字符無法選擇正確的編碼:
1 from datetime import datetime
2 from py.xml import html
3 import pytest
4
5
6 @pytest.mark.hookwrapper
7 def pytest_runtest_makereport(item, call):
8 outcome = yield
9 report = outcome.get_result()
10 # 對test一列重新編碼泥张,顯示中文
11 report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
3. 運行測試用例
現(xiàn)在來試著運行一下測試用例:
>>> pytest ./test_xl_login.py --html=report.html --self-contained-html
============================================== test session starts ==============================================
platform linux -- Python 3.7.3, pytest-4.4.1, py-1.8.0, pluggy-0.10.0
rootdir: /home/user1/test/PageObject
plugins: metadata-1.8.0, html-1.20.0
collected 5 items
test_xl_login.py ..... [100%]
--------------------- generated html file: /home/user1/test/PageObject/report.html ---------------------
=========================================== 5 passed in 27.18 seconds ===========================================
看看測試報告: