在自动化测试项目中,可能会碰到一些经常使用的但 很少变化的配置信息,下面就来介绍使用configparser来读取配置信息config.ini

读取的信息(config.ini)如下:

[config]
platformName=Android
appPackage=com.sheinside
appActivity=.module.GuideActivity
baseUrl=http://0.0.0.0:4723/wd/hub
findElementTimes=10
[cmd]
openAppium=node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js
stopAppium=pkill node
startServer=abd statr-server
closeServer=abb kill-server
checkPhone=adb get-state
viewPhone=adb devices
viewAndroid=adb shell grep ro.build.version.release /system/build.prop
openPhone=adb shell input keyevent 26
installSoftware=adb install
uninstallSoftware=adb uninstall com.sheinside

###############################################

读取配置文件的代码(readconfig.py):

 import os
import configparser
import codecs
global configfile_path prjDir = os.path.split(os.path.realpath(__file__))[0]
configfile_path = os.path.join(prjDir, "config.ini")# 配置文件的路径 class ReadConfig:
def __init__(self): fd = open(configfile_path)
data = fd.read()
# remove BOM
if data[:3] == codecs.BOM_UTF8:
data = data[3:]
file = codecs.open(configfile_path, "w")
file.write(data)
file.close()
fd.close() self.cf = configparser.ConfigParser()
self.cf.read(configfile_path)
  # 读取config下的信息
def getConfigValue(self, name):
value = self.cf.get("config", name)
return value
  # 读取cmd下的信息
def getcmdValue(self, name):
value = self.cf.get("cmd", name)
return value

调用执行:

if __name__ == '__main__':
rf = ReadConfig()
print rf.getConfigValue('platformName')
print rf.getcmdValue('openAppium')

结果为:

Android
node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js
[Finished in 0.2s]

Python+Selenium进行UI自动化测试项目中,常用的小技巧2:读取配置文件(configparser,.ini文件)的更多相关文章

  1. Python+Selenium进行UI自动化测试项目中,常用的小技巧1:读取excel表,转化成字典(dict)输出

    从今天开始我将会把在项目中遇到的问题,以及常用的一些技巧来分享出来,以此来促进自己的学习和提升自己:更加方便我以后的查阅. 现在要说的是:用Python来读取excel表的数据,返回字典(dict), ...

  2. Python+Selenium进行UI自动化测试项目中,常用的小技巧4:日志打印,longging模块(控制台和文件同时输出)

    在前段时间,为了给项目中加入日志功能,就想到了 logging 模块,百度logging一大推,都是各种复制的,并没有找到自己想要的结果:我的目的很简单,就是:在把日志写入文件的同时在控制台输出,更加 ...

  3. Python+Selenium进行UI自动化测试项目中,常用的小技巧3:写入excel表(python,xlsxwriter)

    我们在项目中可能用到excel表生成,下面的代码就是对excel表的操作: import xlsxwriter import datetime class write_excel(): def __i ...

  4. Extjs 项目中常用的小技巧,也许你用得着(2)

    接着来,也是刚刚遇到的 panel怎么进行收缩 collapsible: true, 这会panel就会出现这个 点这个就可以收缩了 panel怎么随便拉伸,也就是让那个小黑三角出现 split: t ...

  5. Extjs 项目中常用的小技巧,也许你用得着(1)

    我在项目中遇到的一些知识点: 1.在GridPanel中显示图片,效果 对应的代码实现 { text: '是否启用', width: 80, // xtype: 'checkcolumn', data ...

  6. Extjs 项目中常用的小技巧,也许你用得着(5)--设置 Ext.data.Store 传参的请求方式

    1.extjs 给怎么给panel设背景色 设置bodyStyle:'background:#ffc;padding:10px;', var resultsPanel = Ext.create('Ex ...

  7. Extjs 项目中常用的小技巧,也许你用得着(4)---Extjs 中的cookie设置

    1.ExtJs设置cookie两种方式 其一:设置cookie如下 saveacct=isForm.getForm().findField('itemselector').getValue(); Ex ...

  8. Extjs 项目中常用的小技巧,也许你用得着(3)

    几天没写了,接着继续, 1.怎么获取表单是否验证通过: form.isValid()//通过验证为true 2.怎样隐藏列,并可勾选: hidden: true, 如果是动态隐藏的话: grid.ge ...

  9. ES6中常用的小技巧,用了事半功倍哦

    ES6中常用的小技巧,如果能在实际项目中能使用到,必定事半功倍: 1. 强制要求参数 ES6提供了默认参数值机制,允许你为参数设置默认值,防止在函数被调用时没有传入这些参数. 在下面的例子中,我们写了 ...

随机推荐

  1. Windows 系统下设置Nodejs NPM全局路径

    Windows下的Nodejs npm路径是appdata,很不爽,想改回来,但是在cmd下执行以下命令也无效 npm config set cache "D:\nodejs\node_ca ...

  2. 【linux】vim编辑器vim+taglist+ctags的配置

    很多linux软件开发实际上并不实在X window的情况下进行的,这时我们不可能启动基于X window的图形化窗口,在这一情况下我们所能使用的主要的编辑器是vim和emacs.(ps:emacs和 ...

  3. Ext JS 6 新特性和工具

    Ext JS 6 新特性和工具 Ext JS 6 带来很多新特性.工具和改进.以下是一些亮点: • 合并了 Ext JS & Sencha Touch - 在 Ext 6, 你可以访问 Ext ...

  4. 解决企业In-House安装APP需HTTPS支持的问题(转载)

    同事写的一篇文章,感觉不错,转过来. 解决企业In-House安装APP需HTTPS支持的问题 问题背景: 能否通过应用服务器发布企业应用: 解决iOS7.1后,发布地址必须为HTTPS服务器. 写作 ...

  5. [转]LINQ To SQL 语法及实例大全

    转载自:http://blog.csdn.net/pan_junbiao/article/details/7015633 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过 ...

  6. [原创]Andorid DexClassLoader的创建过程解析(基于5.0)

    做Android插件框架时,经常会用到dex的动态加载,就要直接或间接的使用DexClassLoader,在new DexClassLoader的时候Android系统做了很多工作,下面我们详细分析一 ...

  7. WebDriver测试EXT控件(基于C#)

    WebDriver测试EXT控件(基于C#)http://www.docin.com/p-748096409.html

  8. Binary Tree Postorder Traversal--leetcode难题讲解系列

    https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return the posto ...

  9. Til the Cows Come Home(最短路)

    Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  10. Ruby on Rails 和 J2EE:两者能否共存?

    http://www.ibm.com/developerworks/cn/java/wa-rubyonrails/