pytest全局變量的使用詳解
這里重新闡述下PageObject設(shè)計(jì)模式:
PageObject設(shè)計(jì)模式是selenium自動(dòng)化最成熟,最受歡迎的一種模式,這里用pytest同樣適用
這里直接提供代碼:

全局變量
conftest.py
"""
conftest.py 全局變量,主要實(shí)現(xiàn)以下功能:
1、添加命令行參數(shù)broswer, 用于切換不用瀏覽器
2、全局參數(shù)driver調(diào)用
"""
import pytest
from selenium import webdriver
def pytest_addoption(parser):
'''
添加命令行參數(shù) --browser
'''
parser.addoption(
"--browser", action="store", default="firefox", help="browser option: firefox or chrome"
)
@pytest.fixture(scope='session') # 以實(shí)現(xiàn)多個(gè).py跨文件使用一個(gè)session來(lái)完成多個(gè)用例
def driver(request):
'''定義全局driver參數(shù)'''
name = request.config.getoption("--browser")
if name == "firefox":
driver = webdriver.Firefox()
elif name == "chrome":
driver = webdriver.Chrome()
else:
driver = webdriver.Chrome()
print("正在啟動(dòng)瀏覽器名稱(chēng): %s" % name)
# 需要登陸就調(diào)用登陸函數(shù)
def fn():
print("當(dāng)全部用例執(zhí)行完之后: teardown driver!")
driver.quit()
request.addfinalizer(fn)
return driverloginpage.py
'''
作者:Caric_lee
日期:2018
'''
import time
from autoTest.pytest_selenium.common.basepage import BasePage
from autoTest.pytest_selenium.common.logging import Log
from autoTest.pytest_selenium.page.mysql_python import Mysql
# 獲取數(shù)據(jù)庫(kù)數(shù)據(jù)
mysql_test = Mysql('localhost','3306','root','123456','test')
dataAll = mysql_test.query('select * from auto_test')
username = dataAll[0]['username']
password = dataAll[0]['password']
url = dataAll[0]['url']
print("查詢(xún)數(shù)據(jù)庫(kù)信息 賬號(hào): %s, 密碼: %s, url: %s" % (username, password, url))
class Login(BasePage):
log = Log()
# 斷言登錄頁(yè),提示文本
hint_text_element = ('xpath', "http://*[@class='crm-login1-header']/h1")
hint_text = '登錄銷(xiāo)售易'
# 斷言忘記密碼
forget_paw_elemet = ('xpath', "http://*[text()='忘記密碼?']")
forget_paw = '忘記密碼'
# 斷言免費(fèi)注冊(cè)
Free_registration_element = ('xpath', "http://*[text()='免費(fèi)注冊(cè)']")
Free_registration_text = '免費(fèi)'
# 斷言'歡迎登錄銷(xiāo)售易'
tenant_interface = ('xpath', "http://*[text()='歡迎登錄銷(xiāo)售易']")
tenant_interface_text = '歡迎登錄'
# 元素定位
input_username_element = ('xpath', "http://*[@name='loginName']")
input_paw_element = ('xpath', "http://*[@placeholder='請(qǐng)輸入密碼']")
click_enter_element = ('xpath', "http://*[text()='登 錄']")
clikc_tenant_element = ('xpath', "http://span[text()='自動(dòng)化測(cè)試_0202_1109_正式' and @class='crm-company-name']")
def assert_title(self):
result = self.is_text_in_element(self.hint_text_element, self.hint_text)
self.log.info("assert: 斷言登錄頁(yè),提示文本: %s" % result)
def assert_forget_paw(self):
result = self.is_text_in_element(self.forget_paw_elemet, self.forget_paw)
self.log.info("assert: 斷言忘記密碼: %s" % result)
def assert_Free_registration(self):
result = self.is_text_in_element(self.Free_registration_element, self.Free_registration_text)
self.log.info("assert: 斷言免費(fèi)注冊(cè): %s" % result)
def input_username(self, username):
self.send_keys(self.input_username_element, username)
def input_paw(self, paw):
self.send_keys(self.input_paw_element, paw)
def click_enter(self):
self.click(self.click_enter_element)
def assert_tenant_interface(self):
result = self.is_text_in_element(self.tenant_interface, self.tenant_interface_text)
self.log.info("assert: 歡迎登錄銷(xiāo)售易: %s" % result)
def move_scroll_end(self):
time.sleep(3)
self.js_focus_element(self.clikc_tenant_element) # 這里已經(jīng)可以實(shí)現(xiàn)滾動(dòng)了
self.log.info("聚焦?jié)L動(dòng)結(jié)束!")
def click_tenant(self): # 選擇租戶(hù)
self.click(self.clikc_tenant_element)
self.log.info("選擇租戶(hù)成功!")
def login(self, username=username, paw=password):
'''登錄流程'''
self.assert_title() # 斷言登錄頁(yè),提示文本
self.assert_forget_paw() # 斷言忘記密碼
self.assert_Free_registration() # 斷言免費(fèi)注冊(cè)
self.input_username(username)
self.input_paw(paw)
self.click_enter()
time.sleep(3)
self.assert_tenant_interface() # 斷言'歡迎登錄銷(xiāo)售易'
print("---------------------->>>>>>>>>>>>>")
self.move_scroll_end()
self.click_tenant()
if __name__=='__main__':
from selenium import webdriver
driver = webdriver.Chrome()
base = Login(driver)
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(10)
base.login()
# 學(xué)習(xí)備注!
# 調(diào)試某個(gè)功能的時(shí)候,就只寫(xiě)這個(gè)功能點(diǎn)的代碼去調(diào)
# 加載轉(zhuǎn)圈是js報(bào)錯(cuò)了,前端的問(wèn)題,是bug (滾動(dòng)條)test_login.py
'''
作者:Caric_lee
日期:2018
'''
from autoTest.pytest_selenium.page import loginpage
from autoTest.pytest_selenium.page.loginpage import Login
import pytest, time
class Test_login():
url = loginpage.url
username_data = loginpage.username
paw_data = loginpage.password
print("調(diào)用信息 賬號(hào): %s, 密碼: %s, url: %s" % (username_data, paw_data, url))
@pytest.fixture(scope="function", autouse=True) # function 默認(rèn)參數(shù)傳遞,autouse=True 自動(dòng)調(diào)用fixture功能
def test_01(self, driver):
driver.get(self.url)
driver.maximize_window()
driver.implicitly_wait(10)
self.login = Login(driver)
def test_02(self):
'''登錄'''
# 1、斷言登錄頁(yè),提示文本
self.login.assert_title()
# 2、斷言忘記密碼
self.login.assert_forget_paw()
# 3、斷言免費(fèi)注冊(cè)
self.login.assert_Free_registration()
# 4、輸入賬號(hào)
self.login.input_username(self.username_data)
# 5、輸入密碼
self.login.input_paw(self.paw_data)
# 6、點(diǎn)擊登錄
self.login.click_enter()
time.sleep(3)
# 7、斷言'歡迎登錄銷(xiāo)售易'
self.login.assert_tenant_interface()
# 8、滾動(dòng)到底部
self.login.move_scroll_end()
# 9、點(diǎn)擊租戶(hù)
self.login.click_tenant()
time.sleep(5)
def test_03(self, driver):
time.sleep(5)
driver.quit()
if __name__ == '__main__':
# 選擇測(cè)試瀏覽器
pytest.main(["-s", "--browser=chrome", "test_login.py"])這里直接指向test_login.py文件就OK了,
還可以在優(yōu)化,把loginpage中的讀取數(shù)據(jù)庫(kù)信息,單獨(dú)寫(xiě)個(gè)方法,放在page里面,直接讀取。
數(shù)據(jù)結(jié)構(gòu)還需要在調(diào)整
到此這篇關(guān)于pytest全局變量的使用詳解的文章就介紹到這了,更多相關(guān)pytest全局變量 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python字符串操作實(shí)戰(zhàn)之如何提取子字符串
這篇文章主要給大家介紹了關(guān)于Python字符串操作實(shí)戰(zhàn)之如何提取子字符串的相關(guān)資料,字符串是Python中最常用的數(shù)據(jù)類(lèi)型,大家應(yīng)該都不陌生,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
Python數(shù)據(jù)可視化之簡(jiǎn)單折線圖的繪制
這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)可視化之繪制簡(jiǎn)單折線圖的相關(guān)資料,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以了解一下2022-10-10
Python restful框架接口開(kāi)發(fā)實(shí)現(xiàn)
這篇文章主要介紹了Python restful框架接口開(kāi)發(fā)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Python Pingouin數(shù)據(jù)統(tǒng)計(jì)分析技術(shù)探索
Pingouin庫(kù)基于pandas、scipy和statsmodels,為用戶(hù)提供了執(zhí)行常見(jiàn)統(tǒng)計(jì)分析的功能,它支持各種統(tǒng)計(jì)方法和假設(shè)檢驗(yàn),例如 t-tests、ANOVA、correlation analysis 等,本文通過(guò)一些示例代碼,以更全面地了解如何使用Pingouin庫(kù)進(jìn)行統(tǒng)計(jì)分析,2024-01-01
pytorch?tensor按廣播賦值scatter_函數(shù)的用法
這篇文章主要介紹了pytorch?tensor按廣播賦值scatter_函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Python處理和解析CLIXML數(shù)據(jù)的方法
在使用Windows的Windows Remote Management (WinRM)服務(wù)與PowerShell交互時(shí),經(jīng)常會(huì)遇到CLIXML(即CLI XML)格式的數(shù)據(jù),本文將介紹如何在Python中處理和解析CLIXML數(shù)據(jù),并提供一種方法來(lái)從數(shù)據(jù)中提取有效信息,需要的朋友可以參考下2024-04-04
python+selenium 腳本實(shí)現(xiàn)每天自動(dòng)登記的思路詳解
這篇文章主要介紹了python+selenium 腳本實(shí)現(xiàn)每天自動(dòng)登記,本文你給大家分享基本的思路,通過(guò)實(shí)例代碼截圖的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
兩行代碼解決Jupyter Notebook中文不能顯示的問(wèn)題
這篇文章主要介紹了兩行代碼解決Jupyter Notebook中文不能顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
Python?中?key?參數(shù)的含義及用法小結(jié)
我們?cè)谑褂?sorted()?或?map()?函數(shù)的時(shí)候,都會(huì)看到里面有一個(gè)?key?參數(shù),其實(shí)這個(gè)?key?參數(shù)也存在于其他內(nèi)置函數(shù)中(例如?min()、max()?等),那么我們今天就來(lái)了解一下?key?參數(shù)的含義以及用途吧,需要的朋友可以參考下2023-12-12

