Previous blogs have introduced the basics of using Python's Configparser module to read and write configuration files, and this blog post describes how to instantiate it for easy invocation as a public class.
There are many benefits of instantiation, which are convenient to call, reduce the maintenance cost of the script, and improve the readability of the code ...
1. Configuration files
The Configparser module supports reading . ConF and . ini types of files, creating a new . ini file in the folder, writing some information, such as the following example:
Config.ini
[Driver]chromedriver = E:\automation\ui\testcase\browser\chromedriver.exe[homepage]url = https://www.sougou.com/[ Signup]name = E:\automation\ui\testdata\signup-data.csv[login]name = E:\automation\ui\testdata\login-data.csv[sign _user]name = E:\automation\ui\testdata\sign_user.csv[sign_mobile]name = e:\automation\ui\testdata\sign_mobile.csv[ Sign_pwd]name = E:\automation\ui\testdata\sign_pwd.csv[vip_user]name = e:\automation\ui\testdata\vip_user.csv[ Serach]name = E:\automation\ui\testdata\search.csv[database]host = 10.0.0.1username = Testpassword = Testdbname = Test[ Headers]contenttype = Application/x-www-form-urlencoded[http]url = Https://baidu.comlogin = https://github.com
2. Configparser read configuration information and instantiate
You can write to the configuration file such as the URL address to be accessed, the Read data file path, the database information, and then use the Configparser module to read and instantiate the sample code as follows:
readconfig.py
#Coding=utf-8ImportOS fromConfigparserImportConfigparser#Get absolute pathBase_dir = Os.path.dirname (__file__) Config_file_path= Os.path.join (Base_dir,"Config.ini")#configuration file nameclassMyconfig:def __init__(self): config=Configparser () config.read (config_file_path) self.browerdriver= Config.get ("Driver","Chromedriver")#Browser DriverSelf.homedourl = Config.get ("Homepage","URL")#Sogou URLSelf.signupdata = Config.get ("Signup","name")#User Registration test DataSelf.logindata = Config.get ("Login","name")#User Login test DataSelf.signuser = Config.get ("Sign_user","name")#Registering user DataSelf.signmobile = Config.get ("Sign_mobile","name")#Registered user Mobile numberSelf.signpwd = Config.get ("sign_pwd","name")#Registered user PasswordSelf.vipuser = Config.get ("Vip_user","name")#Production Whitelist test accountSelf.search = Config.get ("Serach","name")#Search DataSelf.contenttype = Config.get ("HEADERS","ContentType")#Request Header Connection typeSelf. URL = Config.get ("HTTP","URL")#Baidu URLSelf.loginurl =config.get ("HTTP","Login")#GitHub's URLMyconfig= Myconfig ()
3. Invoking the instantiated configuration information
The information we need to instantiate the package, when needed, the direct call, the sample code (test phone verification code) is as follows:
test_mobcode.py
#Coding=utf-8 fromSeleniumImportWebdriver fromReadconfigImportMyconfig fromSelenium.webdriver.common.byImport byImportUnittest,timeclassmobcodetest (unittest. TestCase):defsetUp (self): Self.driver= Webdriver. Chrome (Myconfig.browerdriver)#invoking a browser driver after instantiationSelf.driver.maximize_window () Self.url= Myconfig.homedourl#invoking the requested URL after instantiation defTest_mobcode (self): driver=self.driver driver.get (self.url) driver.implicitly_wait (3) Driver.find_element_by_link_text ("Free Registration"). Click () driver.find_element_by_id ("Txtmobilecode"). Send_keys ("1234") Driver.find_element_by_class_name ("login-btn"). Click ()#Locate the error alert element and assert it, print it outError = Driver.find_element_by_xpath ("//*[@id =\ "loginform\"]/div/dl[2]/dd/p") Print(Error.text) self.assertisnotnone (error.text)defTearDown (self): Self.driver.close () Suite=UnitTest. TestSuite () suite.addtest (Mobcodetest ("Test_mobcode")) Runner=UnitTest. Texttestrunner () Runner.run (suite)
It can be imagined that if you do not write the URL, drive path, etc. to the configuration file, but write directly to the script, the cost of maintenance of the script itself will be very large, and the readability is not good.
This will be a disaster for automated testing of hundreds of dozens of of test cases.
The above content is for reference only, some sensitive information has been desensitization, hope to understand ...
Python: Instantiating the Configparser module read-write configuration file