In an automated test project, you may encounter some frequently used but rarely changing configuration information, which is described using Configparser to read configuration information config.ini
The information read (Config.ini) is as follows:
[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
###############################################
Read the code for the configuration file (readconfig.py):
1 ImportOS2 ImportConfigparser3 ImportCodecs4 GlobalConfigfile_path5 6Prjdir = Os.path.split (Os.path.realpath (__file__)) [0]7Configfile_path = Os.path.join (Prjdir,"Config.ini") # Path to the configuration file8 9 Ten classReadconfig: One def __init__(self): A -FD =Open (Configfile_path) -data =Fd.read () the #Remove BOM - ifData[:3] = =codecs. Bom_utf8: -data = Data[3:] -File = Codecs.open (Configfile_path,"W") + file.write (data) - file.close () + fd.close () A atSELF.CF =Configparser. Configparser () - Self.cf.read (Configfile_path) -# read the information under Config - defGetconfigvalue (self, name): -Value = Self.cf.get ("Config", name) - returnvalue in# read the information under CMD - defGetcmdvalue (self, name): toValue = Self.cf.get ("cmd", name) + returnValue
Call Execution:
if __name__ = = ' __main__ ':
RF = Readconfig ()
Print Rf.getconfigvalue (' PlatformName ')
Print Rf.getcmdvalue (' Openappium ')
The result is:
Android
Node/applications/appium.app/contents/resources/node_modules/appium/bin/appium.js
[Finished in 0.2s]
Python+selenium in the UI Automation test project, Common Tips 2: Read the configuration file (Configparser,.ini file)