python单例模式之selenium driver实现单例

作者:傲娇的喵酱 时间:2021-09-30 14:31:03 

一、使用装饰器实现单例

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自动化driver实现单例模式

2.1 编写单例模式的装饰器

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

2.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项目结构

python单例模式之selenium driver实现单例

四、工具层 Utils

4.1singleton.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

4.2 GetSeleniumDriver.py  driver实现单例

# -*- 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

六、流程层

把一步一步的动作,封装成一个业务流程

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

来源:https://blog.csdn.net/qq_39208536/article/details/123214101

标签:python,selenium,driver,单例
0
投稿

猜你喜欢

  • Go语言空结构体详解

    2024-04-30 10:07:44
  • Pytorch实现WGAN用于动漫头像生成

    2023-07-24 22:31:26
  • Go语言使用sort包对任意类型元素的集合进行排序的方法

    2023-09-02 03:55:18
  • 像聪明女孩穿衣服那样设计网页文字

    2007-11-06 16:45:00
  • sqlserver2005 xml字段的读写操作

    2024-01-16 23:00:37
  • 详解VScode自动补全CSS3前缀插件以及配置无效的解决办法

    2023-01-05 06:49:40
  • 推荐一篇不错的新手asp编程的基本法则

    2011-04-15 11:08:00
  • Yii2中使用asset压缩js,css文件的方法

    2024-05-02 17:16:13
  • Python面向对象编程repr方法示例详解

    2021-10-02 23:38:09
  • jQuery 1.3.3 新功能[译]

    2009-06-04 12:23:00
  • 详解Python中的type和object

    2021-03-25 13:00:58
  • python人工智能tensorflow函数tensorboard使用方法

    2021-04-21 14:52:46
  • js 计算月/周的第一天和最后一天代码

    2024-05-03 15:07:32
  • python函数map()和partial()的知识点总结

    2023-10-04 14:58:11
  • Python列表排序 list.sort方法和内置函数sorted用法

    2022-01-18 01:01:47
  • Python相互导入的问题解决

    2022-12-04 16:59:56
  • Python提高运行速度工具之Pandarallel的使用教程

    2021-07-16 20:14:09
  • Python科学画图代码分享

    2023-08-19 07:06:25
  • Django 项目布局方法(值得推荐)

    2022-08-22 12:44:22
  • 五个Python迷你版小程序附代码

    2023-09-28 02:55:26
  • asp之家 网络编程 m.aspxhome.com