python数据分离

python数据分离——读取配置文件

版本:
python 3.6

进行数据/配置和代码分离时,python3.6 可以使用configparser进行配置信息读取或创建。可以实现程序和数据的分离,便于后期维护程序,也能在一定程序上满足不会编码的人进行自动化测试(只需改配置文件。)

以创建/读取ini格式配置文件为例:

创建配置文件:

每个ini文件都是有n个sections组成(可以理解为组成部分或者是段落。。我是这么理解的),每个sections包含若干个键值对(keys:values),所以某种程度上,你可以把每个section看成是个字典(dictionary),虽然这俩意义上完全无关。
python3.6创建ini配置文件代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import configparser

cf = configparser.ConfigParser()
cf['DEFAULT'] = {"browser" : "Chrome",
"version" : "63.0.3239.132",
"driver" : "chromedriver.exe",
}

cf['tokyle.com'] = {}
cf['tokyle.com']['Author'] = 'kyle'

cf['baidu.com'] = {}
baiduSearch = cf['baidu.com']
baiduSearch['searchBox'] = 'id > kw'
baiduSearch['searchButton'] = 'id > su'

cf['DEFAULT']['noRetry'] = 'yes'

with open('test.ini', 'w') as configfile:
cf.write(configfile)

上述代码执行完成之后会在该.py文件所在目录下创建一个test.ini的配置文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[DEFAULT]
browser = Chrome
version = 63.0.3239.132
driver = chromedriver.exe
noretry = yes

[tokyle.com]
author = kyle

[baidu.com]
searchbox = id > kw
searchbutton = id > su


每个ini文件可以包含一个['DEFAULT'] section,也可以没有,['DEFAULT']部分定义了默认设置,例如默认使用的浏览器,浏览器版本等。

读取配置信息

1
2
3
4
import configparser

cf = configparser.ConfigParser()
cf.read('test.ini')

获取所有sections使用

1
cf.sections()

即可,需要注意的是,这样,是默认不会获取都[‘DEFAULT’]的,可以实验一下:

1
2
section = cf.sections()
print(section)

打印出的结果:

1
['tokyle.com', 'baidu.com']

可以看见默认不包含[‘DEFAULT’]。

其实读取配置信息就和读取字典里的数据做法一样,使用for循环就可以打印出全部信息,例如进行自动化测试时想获取[‘baidu.com’]里存的百度搜索输入框以及百度一下按键,可以:

1
2
3
baidu_msg = cf['baidu.com']
for key,value in baidu_msg.items():
print(key + ' : ' + value)

打印出的信息:

1
2
3
4
5
6
searchbox : id > kw
searchbutton : id > su
browser : Chrome
version : 63.0.3239.132
driver : chromedriver.exe
noretry : yes

可以看见,这样不仅会把[‘baidu.com’]模块的信息全部打印,[‘DEFAULT’]模块的信息也会被打印出来。

使用配置文件实例(简单的数据分离):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import configparser
import traceback,time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from nose.tools import assert_true

class ElementId():

def __init__(self):
#读取配置文件
self.cf = configparser.ConfigParser()
self.cf.read('test.ini')
self.cf.sections()

def get_elementId(self, driver, webSiteName, webelement):
try:
#获取配置文件中的定位方法以及定位元素
webElemnt = self.cf.get(webSiteName, webelement).split('>')
webelement_method = webElemnt[0].strip()
webelement_expression = webElemnt[1].strip()
element = WebDriverWait(driver, 10).until\
(lambda x: x.find_element(webelement_method, webelement_expression))
except Exception as e:
print(traceback.print_exc(), e)
else:
return element

if __name__ == '__main__':
"""测试百度搜索"""
#打开百度首页
browser = webdriver.Chrome()
browser.get("https://www.baidu.com")
#获取定位元素
elementid = ElementId()
searchBox = elementid.get_elementId(browser, 'baidu.com', 'searchbox')
searchBox.send_keys('selenium')
searchButton = elementid.get_elementId(browser, 'baidu.com', 'searchbutton')
searchButton.click()
time.sleep(3)
#断言完成百度搜索"selenium"
assert_true(u"Selenium - Web Browser Automation" in browser.page_source)
browser.quit()

(我在执行上述示例之前,把test.ini中的[‘DEFAULT’]注释掉了,此例不需要[‘DEFAULT’]中的信息)

文章目录
  1. 创建配置文件:
  2. 读取配置信息
  3. 使用配置文件实例(简单的数据分离):
|