时间:2022-07-26 10:03:09 | 栏目:Python代码 | 点击:次
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)
singleton.py #coding:utf-8 #单例模式函数,用来修饰类 def singleton(cls,*args,**kw): instances = {} def _singleton(): if cls not in instances: instances[cls] = cls(*args,**kw) return instances[cls] return _singleton
GetSeleniumDriver.py # -*- coding:utf-8 -*- from selenium import webdriver from singleton import singleton @singleton class GetSeleniumDriver(object): def __init__(self): self.driver = webdriver.Chrome()
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
#coding:utf-8 #单例模式函数,用来修饰类 def singleton(cls,*args,**kw): instances = {} def _singleton(): if cls not in instances: instances[cls] = cls(*args,**kw) return instances[cls] return _singleton
# -*- coding:utf-8 -*- from selenium import webdriver from Utils.singleton import singleton @singleton class GetSeleniumDriver(object): def __init__(self): self.driver = webdriver.Chrome()
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
把一步一步的动作,封装成一个业务流程
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)
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