Python PO設(shè)計(jì)模式的具體使用
無規(guī)矩不成方圓。編寫代碼也是,如果沒有大概的框架,管理代碼將會(huì)是一件很頭疼的事。
先看看筆者以前寫的python腳本:
如果只有一個(gè)用例,這樣看著好像挺整潔的。但是當(dāng)用例越來越多后,如果元素定位發(fā)生了改變,那你將要在多個(gè)類、多個(gè)方法中,去尋找那個(gè)元素,然后一個(gè)一個(gè)修改,這將耗費(fèi)很多時(shí)間。
引入PO設(shè)計(jì)模式后,管理代碼將會(huì)很輕松。
什么是PO設(shè)計(jì)模式?
PO設(shè)計(jì)模式是一種業(yè)務(wù)流程與頁面元素操作分離的模式;這意味著,當(dāng)UI發(fā)生變化,元素定位發(fā)生變化時(shí),只需要在一個(gè)地方修改即可。
下面是代碼目錄:
頁面元素的定位、封裝寫到pages模塊中;業(yè)務(wù)流程的操作寫到test_case模塊中;run_main是執(zhí)行所有用例。
其它如讀取配置文件等,可另外寫到models模塊中。
模塊下的文件如下:
models模塊
configfile.cfg: #存儲(chǔ)后臺(tái)登錄賬號(hào)密碼 [login] username=admin password=admin #平臺(tái)地址 [platform] url = http://ip/admin
讀取文件(readconfig.py):
import ConfigParser import os conf = ConfigParser.RawConfigParser() #讀取文件 conf.read(os.path.join(os.getcwd(),'models\\configfile.cfg')) #賬號(hào)、密碼 username = conf.get("login", "username") password = conf.get("login", "password") #url url = conf.get("platform", "url")
Myunit.py:
from selenium import webdriver import unittest from models import readconfig class MyTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.get(readconfig.url) self.driver.maximize_window() time.sleep(4) #self.driver.implicitly_wait(2) def tearDown(self): self.driver.quit()
pages模塊
pages下的登錄頁面loginpage.py
from selenium.webdriver.common.by import By from pages.page import BasePage class LoginPage(BasePage): ''' 用戶登錄頁面 ''' #元素集 #用戶名 username = (By.NAME,"userName") #密碼 passwd = (By.NAME,"password") #登錄 按鈕 loginbtn = (By.XPATH,"http://form[@id='frmLogin']/div[2]/footer/button") def input_username(self,text): print u"輸入用戶名:",text self.input_text(self.username, text) def input_passwd(self,text): print u"輸入密碼:",text self.input_text(self.passwd, text) def click_loginbtn(self): print u"點(diǎn)擊 登錄 按鈕" self.click(self.loginbtn)
test_case模塊
test_case下的登錄操作logintest.py
from models.myunit import MyTest from models import readconfig from pages.loginpage import LoginPage import unittest import os #所有test類都繼承MyTest類;#每次執(zhí)行xx_test方法時(shí),會(huì)先執(zhí)行MyTest中的setUp(),結(jié)束操作后再執(zhí)行tearDown() class LoginTest(MyTest): '''登錄測(cè)試''' def test_login1(self): '''用戶名和密碼都為空''' try: loginpage = LoginPage(self.driver) loginpage.login_sys("", "") self.assertEqual(loginpage.none_user_hint(), u"請(qǐng)輸入用戶名") self.assertEqual(loginpage.none_passwd_hint(), u"請(qǐng)輸入密碼") except Exception as msg: print(u"異常原因:%s"%msg) self.driver.get_screenshot_as_file(os.path.join(readconfig.screen_path,'login1.png')) raise Exception("false")
run_main
run_main.py:
import sys reload(sys) sys.setdefaultencoding('utf-8') import unittestimport HTMLTestRunner from models import readconfig report_path = readconfig.report_path if __name__ == '__main__': #suite = unittest.TestSuite() #suite.addTest(TestAll('test_a')) now = time.strftime("%Y%m%d%H%M%S",time.localtime(time.time())) filename = report_path+now+'result.html' fp = open(filename,'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title='REPORT',description='RESULT') discover = unittest.defaultTestLoader.discover("test_case",pattern="*test.py",top_level_dir=None) runner.run(discover) fp.close()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pytorch - TORCH.NN.INIT 參數(shù)初始化的操作
這篇文章主要介紹了Pytorch - TORCH.NN.INIT 參數(shù)初始化的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Python在Matplotlib圖中顯示中文字體的操作方法
這篇文章主要介紹了Python在Matplotlib圖中顯示中文字體的方法,本篇主要針對(duì)在Ubuntu系統(tǒng)中,matplotlib顯示不了中文的問題,尤其是在無法安裝系統(tǒng)字體的情況下,解決Python繪圖時(shí)中文顯示的問題。需要的朋友可以參考下2019-07-07tensorflow生成多個(gè)tfrecord文件實(shí)例
今天小編就為大家分享一篇tensorflow生成多個(gè)tfrecord文件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python日期時(shí)間模塊datetime詳解與Python 日期時(shí)間的比較,計(jì)算實(shí)例代碼
python中的datetime模塊提供了操作日期和時(shí)間功能,本文為大家講解了datetime模塊的使用方法及與其相關(guān)的日期比較,計(jì)算實(shí)例2018-09-09多個(gè)python文件調(diào)用logging模塊報(bào)錯(cuò)誤
這篇文章主要介紹了多個(gè)python文件調(diào)用logging模塊產(chǎn)生錯(cuò)誤,需要的朋友可以參考下2020-02-02Python中bytes和str的區(qū)別與聯(lián)系詳解
Python3最重要的新特性之一是對(duì)字符串和二進(jìn)制數(shù)據(jù)流做了明確的區(qū),下面這篇文章主要給大家介紹了關(guān)于Python中bytes和str區(qū)別與聯(lián)系的相關(guān)資料,需要的朋友可以參考下2022-05-05