The public directory has some common modules that are called by use cases. login.py content is as follows:
#Coding=utf-8Import Time#LogindefLogin (Driver): Driver.find_element_by_class_name ("Ui-dialog-close"). Click ()#Close the pop -up windowDriver.find_element_by_xpath ("//*[@id = ' Topbar_nav ']/li[1]/a[1]"). Click ()#Click the login buttonDRIVER.FIND_ELEMENT_BY_ID ("username"). Clear () driver.find_element_by_id ("username"). Send_keys ("18055352262") driver.find_element_by_id ("Password"). Clear () driver.find_element_by_id ("Password"). Send_keys ("hj123456") Driver.find_element_by_xpath ("//input[@class = ' btn ')"). Click ()#Click the Confirm login button#LogoutdefLogout (Driver): Time.sleep (2) driver.find_element_by_link_text (U"Exit"). Click ()
Next, the Login_lizi_public file references the function defined in login.py, with the following code:
#Coding=utf-8 fromSeleniumImportWebdriver fromPublicImportLoginImportUnitTestclasslogintest (unittest. TestCase):defsetUp (self): Self.driver=Webdriver. Chrome () Self.base_url="http://www-test.lizi.com"self.driver.implicitly_wait (5) defTest_lizi (self): driver=self.driver driver.get (self.base_url)#calling the login functionLogin.login (Driver) text= Driver.find_element_by_css_selector ('. Name'). TextPrinttext self.assertequal (Text,u"A summer blown by the wind", msg="Error") #Call Exit Functionlogin.logout (Driver)defTearDown (self): Self.driver.quit ()if __name__=="__main__": Unittest.main ()
Python+selenium Automation Implementation Example-modular invocation