#!/usr/bin/env python#-*-coding:utf-8-*-"""@desc: Test the login function of 126 mailbox 1. Use the public method public.login2. Place the test data in an XML file, using data-driven (/test_data/login.xml) 3. Here we use Xml.dom.minidom to read the XML data"""ImportUnitTestImportXml.dom.minidomImportOSImportSYS fromSeleniumImportWebdrivercur_dir=os.getcwd () sys.path.append (Cur_dir.split (R'\test_case') [0]) fromPublicImportLoginfpath= Cur_dir.split ('Test_case') [0] +'Test_data'+ Os.path.sep +'Login.xml'#open an XML documentDom =xml.dom.minidom.parse (Fpath)#Get document Element ObjectRoot =dom.documentelementclassTestlogin (unittest. TestCase):defsetUp (self): Self.driver=Webdriver. Firefox () self.driver.implicitly_wait (30) Logins= Root.getelementsbytagname ('URL') Self.base_url=Logins[0].firstchild.data self.verificationerrors= [] #use Case 1: User name, password is empty defTest_null (self): driver=self.driver driver.get (self.base_url)#reading the data in XMLlogins = Root.getelementsbytagname ('NULL') #gets the username, password property value of the null tagUsername = Logins[0].getattribute ("username") Password= Logins[0].getattribute ("Password") Prompt_info=Logins[0].firstchild.data#LoginLogin.login (self, username, password)#get assertion information to assertText = Driver.find_element_by_xpath ("//div[@class = ' Error-tt ']/p"). Text self.assertequal (text, prompt_info)#use case 2: User name is empty defTest_user_null (self): driver=self.driver driver.get (self.base_url) Logins= Root.getelementsbytagname ('User_null') #get username, Passwrod attribute values for user_null tagsUsername = Logins[0].getattribute ("username") Password= Logins[0].getattribute ("Password") Prompt_info=Logins[0].firstchild.data#LoginLogin.login (self, username, password)#get assertion information to assertText = Driver.find_element_by_xpath ("//div[@class = ' Error-tt ']/p"). Text self.assertequal (text, prompt_info)#use case 3: password is empty defTest_pwd_null (self): driver=self.driver driver.get (self.base_url) Logins= Root.getelementsbytagname ('Pwd_null') #get username, Passwrod attribute values for pwd_null tagsUsername = Logins[0].getattribute ("username") Password= Logins[0].getattribute ("Password") Prompt_info=Logins[0].firstchild.data#LoginLogin.login (self, username, password)#get assertion information to assertText = Driver.find_element_by_xpath ("//div[@class = ' Error-tt ']/p"). Text self.assertequal (text, prompt_info)#use case 4: Wrong user name and password defTest_error (self): driver=self.driver driver.get (self.base_url) Logins= Root.getelementsbytagname ('Error') #get the Username, Passwrod property value of the error tagUsername = Logins[0].getattribute ("username") Password= Logins[0].getattribute ("Password") Prompt_info=Logins[0].firstchild.data#LoginLogin.login (self, username, password)#get assertion information to assertText = Driver.find_element_by_xpath ("//div[@class = ' Error-tt ']/p"). Text self.assertequal (text, prompt_info)defTearDown (self): Self.driver.quit () self.assertequal ([], self.verificationerrors)if __name__=="__main__": Unittest.main ()
Selenium Notes 7