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
posted @ 2020-11-03 13:25  天天眠眠  阅读(164)  评论(0编辑  收藏  举报