網(wǎng)站首頁 編程語言 正文
一、使用裝飾器實(shí)現(xiàn)單例
def Singleton(cls): ? ? _instance = {} ? ? ? def _singleton(*args, **kargs): ? ? ? ? if cls not in _instance: ? ? ? ? ? ? _instance[cls] = cls(*args, **kargs) ? ? ? ? return _instance[cls] ? ? ? return _singleton ? ? @Singleton class A(object): ? ? a = 1 ? ? ? def __init__(self, x=0): ? ? ? ? self.x = x ? ? a1 = A(2) a2 = A(3)
二、web自動(dòng)化driver實(shí)現(xiàn)單例模式
2.1 編寫單例模式的裝飾器
singleton.py #coding:utf-8 #單例模式函數(shù),用來修飾類 def singleton(cls,*args,**kw): ? ? instances = {} ? ? def _singleton(): ? ? ? ? if cls not in instances: ? ? ? ? ? ? instances[cls] = cls(*args,**kw) ? ? ? ? return instances[cls] ? ? return _singleton
2.2 driver 使用裝飾器,實(shí)現(xiàn)單例模式
GetSeleniumDriver.py # -*- coding:utf-8 -*- from selenium import webdriver from singleton import singleton @singleton class GetSeleniumDriver(object): ? ? def __init__(self): ? ? ? ? self.driver = webdriver.Chrome()
2.3 獲取driver的實(shí)例,就是單例了
class My_task(RES): ? ? def __init__(self): ? ? ? ? self.driver=GetSeleniumDriver().driver ? ? ? def Making_task_Button(self): ? ? ? ? Making_task_Button=self.driver.find_element_by_xpath(RES.Making_task_Button_xpth) ? ? ? ? return Making_task_Button ? ? ? def Audit_task_Button(self): ? ? ? ? Audit_task_Button=self.driver.find_element_by_xpath(RES.Audit_task_Button_xpth) ? ? ? ? return Audit_task_Button
三、在自動(dòng)化項(xiàng)目中具體的應(yīng)用
3.1項(xiàng)目結(jié)構(gòu)
四、工具層 Utils
4.1singleton.py 是單例裝飾器
#coding:utf-8 #單例模式函數(shù),用來修飾類 def singleton(cls,*args,**kw): ? ? instances = {} ? ? def _singleton(): ? ? ? ? if cls not in instances: ? ? ? ? ? ? instances[cls] = cls(*args,**kw) ? ? ? ? return instances[cls] ? ? return _singleton
4.2 GetSeleniumDriver.py ?driver實(shí)現(xiàn)單例
# -*- coding:utf-8 -*- from selenium import webdriver from Utils.singleton import singleton @singleton class GetSeleniumDriver(object): ? ? def __init__(self): ? ? ? ? self.driver = webdriver.Chrome()
五、頁面元素層 TsetSharelab
My_task.py # -*- coding:utf-8 -*- ? from Utils.GetSeleniumDriver import GetSeleniumDriver ? ? class My_task(): ? ? def __init__(self): ? ? ? ? self.driver=GetSeleniumDriver().driver ? ? ? def Making_task_Button(self): ? ? ? ? Making_task_Button=self.driver.find_element_by_xpath('/html/body/div[3]/div[1]/div/a[1]') ? ? ? ? return Making_task_Button ? ? ? def Audit_task_Button(self): ? ? ? ? Audit_task_Button=self.driver.find_element_by_xpath('/html/body/div[3]/div[1]/div/a[1]') ? ? ? ? return Audit_task_Button
六、流程層
把一步一步的動(dòng)作,封裝成一個(gè)業(yè)務(wù)流程
BookCity_page_process.py
# -*- coding:utf-8 -*- from Utils.GetSeleniumDriver import GetSeleniumDriver ? import time ? class BookCity_page_process(object): ? ? def __init__(self): ? ? ? ? self.driver=GetSeleniumDriver().driver ? ? ? def WeiBo_Loain_To_Share(self): ? ? ? ? ? time.sleep(3) ? ? ? ? self.driver.find_elements_by_class_name('W_input').pop(0).send_keys(123) ? ? ? ? time.sleep(1) ? ? ? ? self.driver.find_elements_by_class_name('W_input').pop(1).send_keys(456)
七、case層 ,把業(yè)務(wù)邏輯組成一條條用例
test_case.py #coding:utf-8 from time import sleep from Utils.GetSeleniumDriver import GetSeleniumDriver ? class CreativeBooks(unittest.TestCase): ? ? @classmethod ? ? def setUpClass(self): ? ? ? ? self.driver = GetSeleniumDriver().driver ? ? ? ? sleep(2) ? ? @classmethod ? ? def tearDownClass(self): ? ? ? ? pass ? ? ? def setUp(self): ? ? ? ? self.driver = GetSeleniumDriver().driver
原文鏈接:https://blog.csdn.net/qq_39208536/article/details/123214101
相關(guān)推薦
- 2024-04-06 MyBatis的一級(jí)(同SqlSession會(huì)話),二級(jí)(不同SqlSession會(huì)話)緩存使用
- 2022-11-26 Redis下載部署并加入idea應(yīng)用的小結(jié)_Redis
- 2022-04-16 python基礎(chǔ)之定義類和對(duì)象詳解_python
- 2023-01-20 React?useMemo與useCallabck有什么區(qū)別_React
- 2022-07-19 Linux cat more grep head tail cut uniq sort tr命令詳解
- 2022-03-23 .Net?Core微服務(wù)網(wǎng)關(guān)Ocelot基礎(chǔ)介紹及集成_自學(xué)過程
- 2022-09-07 python?sklearn?畫出決策樹并保存為PDF的實(shí)現(xiàn)過程_python
- 2022-04-28 WPF使用DockPanel停靠面板布局_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支