python單例模式之selenium driver實(shí)現(xiàn)單例
作者:傲嬌的喵醬
這篇文章主要介紹了python單例模式之selenium driver實(shí)現(xiàn)單例,使用裝飾器實(shí)現(xiàn)單例,文章基于python的相關(guān)資料實(shí)現(xiàn)主題,具有一的的參考價(jià)值,需要的朋友可以參考一下
一、使用裝飾器實(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 _singleton2.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 _singleton4.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
到此這篇關(guān)于python單例模式之selenium driver實(shí)現(xiàn)單例的文章就介紹到這了,更多相關(guān)python selenium driver實(shí)現(xiàn)單例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!