Selenium學習第三天--模組化設計用例

weixin_34185364發表於2018-09-03

公共方法:

from  selenium import webdriver
import time

def login(self):
    self.driver.find_element_by_xpath('/html/body/div/div/form/div[1]/select').click()
    time.sleep(2)
    self.driver.find_element_by_xpath('/html/body/div/div/form/div[1]/select/option[2]').click()
    time.sleep(1)
    self.driver.find_element_by_xpath('/html/body/div/div/form/div[2]/input').send_keys('142630170611011230')
    time.sleep(1)
    self.driver.find_element_by_xpath('/html/body/div/div/form/div[3]/input').send_keys('123456')
    time.sleep(2)
    self.driver.find_element_by_xpath('/html/body/div/div/form/button').click()
def  logout(self):
    self.driver.find_element_by_link_text('退出').click()

登入首頁測試用例:

from  selenium import webdriver
import unittest
import time
from  SELENIUM.sx_public import login ,logout

class Testlogin(unittest.TestCase):
    #初始化方法,用例執行時先執行這個方法
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(2)
        self.url = '測試地址(url)'
        self.verificationErrors = []
    def testlogin(self):
        driver = self.driver
        self.driver.get(self.url)
     #呼叫登入方法
        login(self)
        text = self.driver.find_element_by_class_name('text-muted').text
        self.assertEqual(text, '智慧填報系統')
    #呼叫退出方法
       logout(self)
   #善後工作,關閉瀏覽器,並驗證有沒有錯誤資訊
    def tearDown(self):
        driver = self.driver
       self.driver.quit()
        self.assertEqual([],self.verificationErrors)

if __name__ == '__main__':
    unittest.main()

相關文章