首页 > 编程语言 >【Appium】python利用Template生成对象模板_appium_元素定位/操作

【Appium】python利用Template生成对象模板_appium_元素定位/操作

时间:2023-02-06 18:45:50浏览次数:44  
标签:Appium code name python self appium file class template

UI自动化中用PageObject设计模式就会发现page元素定位代码基本重复,复制黏贴,修改,所以就想到运用模板方式,批量生成page,同理也能批量生成handle。

有模板,利用配置文件ini获取类名,方法名,模板套用,生成文件。

1、编写模板文件:

文件名:template_handle_head.tmpl,template_page_body.tmpl

2、利用配置文件:

LocalElement.ini

3、用configparser 读取配置文件,替换class_name、fun_name以及option,批量生成多个page文件:

from string import Template
import configparser


# 用ini里的文件获取元素生成的类--->后面再升级直接读取excel生成page跟handle下的页面
class GeneratePage:
    def __init__(self):
        # 元素文件
        self.config_file = '..\\config\\LocalElement.ini'
        # 类模板文件,模板文件最下面要空2行
        self.template_path_head = '../config/template_page_head.tmpl'
        self.template_path_body = '../config/template_page_body.tmpl'
        # 生成文件后的地址
        self.package_path = '../page/'

    # 自动生成类文件_page_package下的元素定位
    def generate_class_file(self, class_name, list_func_name):
        my_code = []
        template_file_head = open(self.template_path_head, 'r')
        template_file_body = open(self.template_path_body, 'r')
        tmpl_head = Template(template_file_head.read())
        tmpl_body = Template(template_file_body.read())
        my_code.append(tmpl_head.substitute(class_name=class_name))
        for func_name in list_func_name:
            my_code.append(tmpl_body.substitute(func_name=func_name, option="\'" + func_name + "\'"))
        return my_code

    # 生成文件
    def write_to_file(self, file_name, code):
        file_path = self.package_path + file_name + '_page.py'
        class_file = open(file_path, 'w')
        class_file.writelines(code)
        class_file.close()

    # 用configparser 读取配置文件,替换class_name、fun_name以及option
    def loop_generate_class_file(self):
        config = configparser.ConfigParser()
        config.read(self.config_file, encoding='utf-8')
        list_section = config.sections()
        for section in list_section:
            # 文件名
            file_name = section[0:section.rfind('_')]
            # 类名
            list_file = file_name.split('_')
            class_name = "".join([name.capitalize() for name in list_file])
            # 方法名集合里就是func_name以及option_name
            list_func_name = config.options(section)
            code = self.generate_class_file(class_name, list_func_name)
            self.write_to_file(file_name, code)
        print('ok')


if __name__ == '__main__':
    generate_code = GeneratePage()
    generate_code.loop_generate_class_file()

4.查看效果图:

5、其他:handel格式也是类似,可以同样自动生成,配置文件也可以用excel等。

 

标签:Appium,code,name,python,self,appium,file,class,template
From: https://www.cnblogs.com/ninarming/p/17096398.html

相关文章

  • mac系统通过python批量转换doc为docx文件
    准备需要安装libreoffice这个软件下载地址:https://www.libreoffice.org/download/download/代码importosimportsubprocesssource="doc文件夹路径"dest="要转......
  • Python requests.Session 协程 下载文件
    Pythonrequests.Session协程下载文件 #coding:utf-8fromgeventimportmonkeymonkey.patch_all()fromgevent.poolimportPoolimportgeventimportrequ......
  • python3如何构建文本向量
    importreimportcollectionswords='''钟声响起归家的讯号在他生命里仿佛带点唏嘘黑色肌肤给他的意义是一生奉献肤色斗争中年月把拥有变做失去......
  • python同时替换多个字符串方法
    importrewords='''钟声响起归家的讯号在他生命里仿佛带点唏嘘黑色肌肤给他的意义是一生奉献肤色斗争中年月把拥有变做失去疲倦的双眼带着......
  • python中某个字符的替换escape
    re包中的escape就是对特殊字符进行转义:......
  • python3中zip详解
    描述zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象......
  • Python3 ImportError: No module named '_tkinter'
    Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"/usr/local/lib/python3.5/tkinter/__init__.py",line35,in<module>import_tkinter#......
  • python怎么获取向量中非零元素的行号
    假设A为向量 :行向量的时候,需要转化为列向量  A.TA为列向量的时候,不需要转化了解下numpy中的nonzero()函数np.nonzero(arrayormmatrix)返回非零的行标,和列标......
  • python2.7 + MySQL 拼接SQL语句的技巧 (处理unicode,时间)
    背景在Python2.7中,可以使用单引号,双引号,三引号表示字符串,当字符串的值为中文时,则会默认转换成unicode。但是在MYSQL中,使用SQL语句时,直接用unicode作为列的查询条件(例如......
  • 快速上手python的简单web框架flask
    目录简介web框架的重要组成部分快速上手flaskflask的第一个应用flask中的路由不同的http方法静态文件使用模板总结简介python可以做很多事情,虽然它的强项在于进行向量运......