Python+selenium in the UI Automation test project, Common Tips 2: Read the configuration file (Configparser,.ini file)

Source: Internet
Author: User
Tags appium

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.