读取 ini 配置文件、UI 对象库

读取ini配置文件

配置项

读取API

写入API

实战:UI 对象库

 

 

 

读取ini配置文件

配置项

在每个 ini 配置文件中,配置数据会被分组(比如下述配置文件中的“config”和“cmd”),每个分组中又可以指定对应的变量值。

示例:test.ini

# 定义config分组
[config]
platformName=Android
appPackage=com.romwe
appActivity=com.romwe.SplashActivity

# 定义cmd分组
[cmd]
viewPhone=adb devices
startServer=adb start-server
stopServer=adb kill-server

# 定义log分组
[log]
log_error=true

  

读取API

  • read(filename):直接读取文件内容
  • sections():得到所有的 section,并以列表的形式返回
  • options(section):得到该 section 的所有 option
  • items(section):得到该 section 的所有键值对
  • get(section,option):得到 section 中 option 的值,返回为 string 类型
  • getint(section, option):得到 section 中 option 的值,返回为 int 类型
  • getboolean()
  • getfloat()
>>> import configparser
>>>
>>> cf = configparser.ConfigParser()
>>> cf.read("e:\\db.ini", encoding="utf-8")  # 读取配置文件
['e:\\db.ini']
>>>
>>> cf.sections()  # 获取各分组
['config', 'cmd', 'log']
>>>
>>> cf.options("config")  # 获取指定分组的所有键
['platformname', 'apppackage', 'appactivity']
>>>
>>> cf.items("config")  # 获取指定分组的所有键值对
[('platformname', 'Android'), ('apppackage', 'com.romwe'), ('appactivity', 'com.romwe.SplashActivity')]
>>>
>>> cf.get("log", "log_error")  # 获取指定分组及指定键的值
'true'

配置文件中的键是不区分大小写的,例如下述两种方式是等价的:

cf.get("config", "appActivity")
cf.get("config", "APPACTIVITY")

在解析时,getboolean()方法查找任何可行的值,例如以下几个都是等价的:

[log]
log_error=true
log_error=TRUE
log_error=1
log_error=yes

 

写入API

  • write(fp):将config对象写入至某个 .init 格式的文件
  • add_section(section):添加一个新的 section
  • set(section, option, value):对 section 中的 option 进行设置,需要调用 write 将内容写入配置文件
  • remove_section(section):删除某个 section
  • remove_option(section, option):删除某个 sction 的某个 key
 1 >>> import configparser
 2 >>> 
 3 >>> cf = configparser.ConfigParser()
 4 >>> 
 5 >>> cf.add_section("section1")  # 添加分组
 6 >>> cf.set("section1", "key1", "value1")  # 添加键值对
 7 >>> cf.set("section1", "key2", "value2")
 8 >>> 
 9 >>> cf.add_section("section2")
10 >>> cf.set("section2", "key3", "value3")
11 >>>
12 >>> cf.add_section("section3")
13 >>> cf.set("section3", "key4", "value4")
14 >>> cf.set("section3", "key5", "value5")
15 >>>
16 >>> cf.remove_section("section2")  # 删除指定分组
17 True
18 >>> cf.remove_option("section3", "key4")  # 删除指定分组的指定键值对
19 True
20 >>>
21 >>> cf.sections()
22 ['section1', 'section3']
23 >>>
24 >>> f = open("config.ini", "w")
25 >>> cf.write(f)  # 保存配置文件
26 >>> f.flush()  # 刷新或关闭文件,数据才会真正写入文件
27 >>> f.close()

 

实战:UI 对象库

用配置文件存储页面元素的定位方式和定位表达式,做到定位数据和程序的分离。

UiObjectMap.ini

[sogou]
searchBox = id>query
searchButton = id>stb

ObjectMap.py

ObjectMap 工具类文件,供测试程序调用。

 1 from selenium.webdriver.support.ui import WebDriverWait
 2 import configparser
 3 import os
 4 from selenium import webdriver
 5 
 6 
 7 class ObjectMap:
 8    
 9     def __init__(self):
10         # 获取存放页面元素定位方式及定位表达式配置文件路径
11         # os.path.abspath(__file__):获取当前文件所在路径目录
12         self.uiObjectMapPath = os.path.dirname(os.path.abspath(__file__)) + "\\UiObjectMap.ini"
13         print(self.uiObjectMapPath)  # E:\eclipse-workspace\AutoTest\UI\UiObjectMap.ini
14        
15     def getElementObject(self, driver, webSiteName, elementName):
16         try:
17             # 创建一个读取配置文件的实例
18             cf = configparser.ConfigParser()
19             # 将配置文件加载到内存
20             cf.read(self.uiObjectMapPath)
21             # 根据section和option获取配置文件中页面元素的定位方式及定位表达式组成的字符串,
22             # 并使用">"分隔
23             locators = cf.get(webSiteName, elementName).split(">")
24             # 得到定位方式
25             locatorMethod = locators[0]
26             # 得到定位表达式
27             locatorExpression = locators[1]
28             print(locatorMethod, locatorExpression)
29             # 显式等待获取页面元素
30             element = WebDriverWait(driver, 10).until(lambda x: x.find_element(locatorMethod, locatorExpression))
31         except Exception as e:
32             print(e)
33         else:
34             # 找到页面元素,返回给调用者
35             return element

SoGou.py

调用 ObjectMap 工具类实现测试逻辑。

 1 from selenium import webdriver
 2 import unittest
 3 import time, traceback
 4 from UI.ObjectMap import ObjectMap
 5 
 6 
 7 class TestSoGouObjectMap(unittest.TestCase):
 8    
 9     def setUp(self):
10         self.obj = ObjectMap()
11         self.driver = webdriver.Firefox()
12        
13     def tearDown(self):
14         self.driver.quit()
15        
16     def testSoGouSearch(self):
17         url = "http://www.sogou.com";
18         self.driver.get(url)
19         try:
20             # 查找页面的搜索输入框
21             searchBox = self.obj.getElementObject(self.driver, "sogou", "searchBox")
22             searchBox.send_keys("WebDriver")
23             # 查找搜索按钮
24             searchButton = self.obj.getElementObject(self.driver, "sogou", "searchButton")
25             searchButton.click()
26             time.sleep(2)
27             self.assertTrue("Selenium" in self.driver.page_source, "assert error!")
28         except Exception:
29             print(traceback.print_exc())
30        
31 
32 if __name__ == "__main__":
33     unittest.main()

 

posted @ 2021-01-04 22:53  Juno3550  阅读(218)  评论(0编辑  收藏  举报