The modularity of the test case: Preparing for po model.
1, the common method of extraction.
2, extracts the data.
3, fetch logic.
# @Author:lsj # @version V1.0 # -*- coding:UTF-8 -*- import unittest from selenium import webdriver from time import sleep from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import HTMLTestRunner def openBroswer(): driver = webdriver.Chrome() return driver def openUrl(driver, url): driver.get(url) driver.maximize_window() # 元素是否存在 def ele_is_presence(driver,tuple_arg): return WebDriverWait(driver, 10).until(EC.presence_of_element_located(tuple_arg)) #点击操作 def ele_click(driver,tuple_arg): ele_is_presence(driver,tuple_arg).click() # 发送文本 def ele_sendKeys(driver,tuple_arg,keyword): ele_is_presence(driver,tuple_arg).send_keys(keyword) # 检查登录结果 def checkResult(driver, loginfo): try: ele_is_presence(driver, loginfo) Print ( " login status verified! " ) the except : Print ( " Login Failed! " ) class Test_Login (of unittest.TestCase): DEF testm1 (Self): # with an embodiment mode URL = ' http://daxue.qysxy.com .cn / ADMIN / static / Front / HTML / the login.html ' Account = [ ' fuguang ' ] pwd = [ ' 123456 ' ] C = [ ' 1111 ' ] username= (By.NAME, " username " ) # user name input box password = (By.NAME, " password " ) # password input box code = (By.ID, " code " ) # codes input box loginbtn = (By .ID, ' loginBtn ' ) # login button # instantiated Driver Driver = openBroswer () # open URL OpenURL (Driver, URL) # enter a username ele_sendKeys (Driver, username, Account) # password ele_sendKeys (Driver, password, pwd) # verification code ele_sendKeys (Driver, code, C) # click the login button ele_click (Driver, loginbtn) Print ( " mode 1 execution is complete !! " ) DEF testm2 (Self): # use-case patterns two URL = " http://daxue.qysxy.com.cn/admin/static/front/html/login.html " username = (By.NAME, ' username ' ) # user name input box password = (By.NAME , ' password ' ) # password input box = code (By.ID, " code " ) # codes input box loginbtn = (By.ID, ' loginBtn ' ) # login button Driver = openBroswer () OpenURL (Driver, URL) SLEEP ( . 3 ) # enter a username ele_sendKeys (Driver, username, ' fuguang ' ) # password ele_sendKeys (Driver, password, ' 123456 ' ) # verification code ele_sendKeys (Driver, code, ' 1111' ) # Click the login button ele_click (Driver, loginbtn) Print ( " mode two execution is complete !! " ) SLEEP ( 3 ) driver.quit () IF __name__ == ' __main__ ' : # Test_Login () # test report testCase = unittest .TestLoader (). loadTestsFromTestCase (Test_Login) # plurality of test class is loaded into the test set Suite = unittest.TestSuite ([the testCase]) # original version F = Open ( " report.html " ," WB " ) Runner = HTMLTestRunner.HTMLTestRunner (F = Stream, title = " test report " , Description = " Test Report Description " ) runner.run (Suite)
The test report