iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python单例模式之seleniumdriver实现单例
  • 597
分享到

python单例模式之seleniumdriver实现单例

2024-04-02 19:04:59 597人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、使用装饰器实现单例二、WEB自动化driver实现单例模式2.1 编写单例模式的装饰器2.2 driver 使用装饰器,实现单例模式2.3 获取driver的实例,就是单例了

一、使用装饰器实现单例

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

四、工具层 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

到此这篇关于python单例模式之selenium driver实现单例的文章就介绍到这了,更多相关Python selenium driver实现单例内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: python单例模式之seleniumdriver实现单例

本文链接: https://www.lsjlt.com/news/141022.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作