zoukankan      html  css  js  c++  java
  • selenium的PO设计模式02

    一、代码和测试数据分离

    1)数据封装

    封装前:
    self.username_inputbox = {'element_name':'用户名输入框', 'locator_type':'xpath', 'locator_value':'//input[@name="account"]', 'timeout': 5 }

      

    封装后:
     def __init__(self,driver):
            super().__init__(driver)
            element = ElementdataUtils('login_page').get_data()
            self.username_inputbox = element['username_inputbox']
    

      

    创建元素基础类(ElementdataUtils):
    import os
    import xlrd
    
    current_path = os.path.dirname(__name__)
    excel_path = os.path.join( current_path,'../element_info_datas/element_data.xlsx' )
    class  ElementdataUtils():
        def __init__(self,page_name,element_path =excel_path):   #将页面名称定义
            self.element_path=element_path
            self.workbook = xlrd.open_workbook(excel_path)
            self.sheet = self.workbook.sheet_by_name(page_name)
            # value = sheet.cell_value(1,0)
            self.row_count = self.sheet.nrows
    
        def get_data(self):
            element_infos = {}
            for i in range(1, self.row_count):
                element_info = {}
                element_info['element_name'] = self.sheet.cell_value(i, 1)
                element_info['locator_type'] = self.sheet.cell_value(i, 2)
                element_info['locator_value'] = self.sheet.cell_value(i, 3)
                element_info['timeout'] = self.sheet.cell_value(i, 4)
                # element_info['current_page'] = self.sheet.cell_value(i, 5)
                element_infos[self.sheet.cell_value(i, 0)] = element_info
            return element_infos
    

    2)驱动和url封装

    封装前:
     current_path = os.path.dirname(__file__)
        driver_path = os.path.join(current_path, '../webdriver/chromedriver')
        driver = webdriver.Chrome(executable_path=driver_path)
        login_page = LoginPage(driver)
        login_page.open_url('http://***/zentao/www/index.php?m=user&f=login')
        login_page.input_username('**')
        login_page.input_password('***')
    

      

    封装后:
    driver =Browser().get_chrome_path()
    login_page = LoginPage(driver)
    login_page.open_url(local_config.url)
    login_page.input_username(local_config.username)
    login_page.input_password(local_config.password)
    login_page.click_login_button()
    

      

    创建配置工具类:
    import os
    import configparser
    
    current_dir = os.path.abspath(os.path.dirname(__file__))  #==os.path.dirname(__file__)  abspath表示当前文件的绝对路径,dirname当前文件相对路径
    config_path = os.path.join(current_dir, '..','config','config.ini')
    
    class ConfigUtils(object):
        def  __init__(self,path =config_path):
            self.cfg=configparser.ConfigParser()    #创建读取配置的对象
            self.cfg.read(path)
    
        @property                #装饰器就是负责把一个方法变成属性调用.
        def  url(self):
            self.url_value=self.cfg.get('default','url')
            return self.url_value
    
        @property
        def driver_path(self):
             self.driver_path_value=self.cfg.get('default','driver_path')
             return self.driver_path_value
    
        @property
        def  username(self):
            self.username_value=self.cfg.get('account','username')
            return self.username_value
    
        @property
        def password(self):
            self.password_value=self.cfg.get('account','password')
            return self.password_value
    
    
    local_config =ConfigUtils()
    
    if __name__=='__main__':
        # config =ConfigUtils()
        print(local_config.url)
    

      

    创建驱动类:
    import os
    from selenium import webdriver
    from common.config_utils import local_config
    from selenium.webdriver.chrome.options import Options
    
    
    current_path = os.path.dirname(__file__)
    webdriver_path = os.path.join(current_path, '..',local_config.driver_path)
    #引用config_utils类中的driver_path方法,因driver_path方法中有修饰器,所以当属性在调用
    
    
    class Browser(object):
        def __init__(self,driver_path =webdriver_path):
            self.driver_path =driver_path
    
        def get_chrome_path(self):
            chrome_driver_path = os.path.join(self.driver_path,'chromedriver')
            driver = webdriver.Chrome(executable_path=chrome_driver_path)
            return driver
    
    if __name__=='__main__':
        Browser().get_chrome_path()
    

      

  • 相关阅读:
    VB.NET程序设计笔记
    ASP.NET动态的网页增删查改
    一般处理程序加简单三层实现增删查改(2)
    一般处理程序加简单三层实现增删查改(1)
    设计模式六大原则(1):单一职责原则
    Android Https相关完全解析 当OkHttp遇到Https
    Androrid应用打包release版时关闭log日志输出
    Fragment结合ViewPager之懒加载
    多线程基础
    Android Studio wifi调试
  • 原文地址:https://www.cnblogs.com/miaoxiaochao/p/12822289.html
Copyright © 2011-2022 走看看