zoukankan      html  css  js  c++  java
  • python+selenium+unittest 搭建web自动化测试框架(五)

    下面咱们着重来封装两个类,用于配置跟定位元素

    首先,我们封装一个类,来读配置元素的配置文件 LocalElement.ini

    read_ini.py

    #coding=utf-8
    import os
    import configparser
    
    class ReadIni:
        def __init__(self,filename=None,node=None):
            if filename == None:
               self.filename = os.getcwd()+'/config/LocalElement.ini'
            else:
               self.filename = filename
            if node == None:
                self.node = 'RegisterElement'
            else:
                self.node = node
            self.load_ini()
    
        def load_ini(self):
            self.cf = configparser.ConfigParser()
            self.cf.read(self.filename)
    
        def get_value(self,key):
            return self.cf.get(self.node,key)
    
    if __name__ == '__main__':
        print(ReadIni().get_value('register_button'))
            

    有了这个读配置文件的类,咱们在封装定位元素的类   find_element.py

    #coding=utf-8
    
    from util.read_ini import ReadIni
    
    class FindElement:
        def __init__(self,driver):
            self.driver = driver
        
        def get_element(self,key):
            readini = ReadIni()
            data = readini.get_value(key)
            by = data.split('>')[0]
            value = data.split('>')[1]
            try:
                if by == 'id':
                    return self.driver.find_element_by_id(value)
                elif by == 'name':
                    return self.driver.find_element_by_name(value)
                elif by == 'classname':
                    return self.driver.find_element_by_class_name(value)
                else:
                    return self.driver.find_element_by_xpath(value)
            except:
                return None

     -------------------------------------- 这个是LocalElement.ini 的内容 --------------------------------

    [RegisterElement]
    user_email=id>register_email
    user_email_error=id>register_email-error
    user_name=id>register_nickname
    user_name_error=id>register_nickname-error
    password=id>register_password
    password_error=id>register_password-error
    code_image=id>getcode_num
    code_text=id>captcha_code
    code_text_error=id>captcha_code-error
    register_button=id>register-btn
  • 相关阅读:
    Linux 命令大全
    MySQL 存储 utf8mb4
    PHP房贷计算器代码,等额本息,等额本金
    laravel 原生 sql
    include_once 问题
    laravel count distinct
    微信小程序显示cms里的html文章
    PHP文件上传
    Ajax做无刷新分页
    PHP封装返回Ajax字符串和JSON数组
  • 原文地址:https://www.cnblogs.com/huaniaoyuchong/p/13919744.html
Copyright © 2011-2022 走看看