lsekfe 发表于 2022-3-18 10:49:52

【转】python单例模式&selenium driver实现单例

一、使用装饰器实现单例
def Singleton(cls):
    _instance = {}

    def _singleton(*args, **kargs):
      if cls not in _instance:
            _instance = cls(*args, **kargs)
      return _instance

    return _singleton


@Singleton
class A(object):
    a = 1

    def __init__(self, x=0):
      self.x = x


a1 = A(2)
a2 = A(3)二、web自动化driver实现单例模式
2.1 编写单例模式的装饰器

singleton.py
#coding:utf-8
#单例模式函数,用来修饰类
def singleton(cls,*args,**kw):
    instances = {}
    def _singleton():
      if cls not in instances:
            instances = cls(*args,**kw)
      return instances
    return _singleton2.2 driver 使用装饰器,实现单例模式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的实例,就是单例了
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三、在自动化项目中具体的应用
3.1项目结构

一、工具层 Utils1singleton.py 是单例装饰器#coding:utf-8
#单例模式函数,用来修饰类
def singleton(cls,*args,**kw):
    instances = {}
    def _singleton():
      if cls not in instances:
            instances = cls(*args,**kw)
      return instances
    return _singleton2 GetSeleniumDriver.pydriver实现单例# -*- coding:utf-8 -*-
from selenium import webdriver
from Utils.singleton import singleton
@singleton
class GetSeleniumDriver(object):
    def __init__(self):
      self.driver = webdriver.Chrome()二、页面元素层 TsetSharelabMy_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/div/div/a')
      return Making_task_Button

    def Audit_task_Button(self):
      Audit_task_Button=self.driver.find_element_by_xpath('/html/body/div/div/div/a')
      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)四、case层 ,把业务逻辑组成一条条用例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
      self.driver.get("http://t.com/")
      self.driver.maximize_window()
      sleep(1)
      Creative_books().Creative_books_Button().click()

    def tearDown(self):
      self.driver.delete_all_cookies()
    def testa(self):

      #新建图书
      Creative_books().CreatBook_Button().click()
      #输入书名
      Creative_books().Input_book_name().send_keys(u"A1一")
      #保存书名

    def testb(self):
      first = self.driver.current_window_handle
      Creative_books().Help_center_Button().click()
原文连接:点击此处>>>

页: [1]
查看完整版本: 【转】python单例模式&selenium driver实现单例