python+selenium+unittest 實現自動化測試

示例代碼:
baidu.pypython

import csv #導入csv模塊
from itertools import islice #從itertools導入islice,後邊讓其默認跳過第一行使用
from time import sleep
from selenium import webdriver
from module import baidumodule
driver = webdriver.Chrome()
search = baidumodule(driver) #將driver傳給aidumodule這個類
with open("file.csv","r") as name:
    lines = name.readlines()#以行讀取整個文件
for i in islice(lines,1,None):  #每次從第二行讀取:
    search.login(i) #調用aidumodule下的login函數
    sleep(2)

start.login_out()

module.pyweb

class baidumodule():
    def __init__(self,driver,):
        self.dr = driver #不能在類中再次導入webdriver 兩邊的driver等於兩個窗口,直接讓調用方傳入driver便可
    def login(self,values):
        login_dr = self.dr
        login_dr.get("https://www.baidu.com/")
        login_dr.find_element_by_xpath("//*[@id='kw']").send_keys(values)
        login_dr.find_element_by_xpath("//*[@id='su']").click()

    def login_out(self):
        self.dr.quit()

file.csvruby

selenium
selenium2
selenium3
webdriver

能夠看到咱們上面寫的代碼,雖然是自動幫咱們搜索多個值,但並不能算測試用例,頂多算線性測試,既然是測試那咱們就須要加入用例計算及結果斷言,咱們這裏介紹使用unittest框架進行自動化測試組建。
unittest簡單介紹:框架

import unittest #導入unittest
class baidu(unittest.TestCase):#TestCase測試用例集

    def setUp(self):#setUp在unittest固定寫法setUp首先執行
        pass
    def tearDown(self):#tearDown在unittest固定寫法setUp會最後執行
        pass
    def test_baidu(self):#測試用例1,可定義多個必須以test開頭
        pass

注:用例名稱不是以test開頭不會被執行,多條用例名稱一致只會執行一條函數

開始改寫咱們上面的baidu.py內容:測試

# _*_ coding:utf-8 _*_
import csv,unittest #導入unittest 模塊
from time import sleep
from selenium import webdriver
from module import baidumodule
class baidu(unittest.TestCase):#建立測試用例集
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()#最大化窗口
        self.driver.implicitly_wait(10)#隱式等待
        self.search = baidumodule(self.driver) #將driver傳給aidumodule這個類
        with open("file.csv","r") as name:
            self.lines = name.readlines()#以行讀取整個文件
    def tearDown(self):
        self.search.login_out()#調用登出函數

    def test_search(self):#測試用例一
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[0])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium_百度搜索')#判斷當前title是否等於selenium_百度搜索
        sleep(2)
    def test_search1(self):#測試用例二
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[1])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium2_百度搜索')
        sleep(2)
    def test_search2(self):#測試用例三
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[2])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium3_百度搜索')
        sleep(2)
    def test_search3(self):#測試用例四
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[3])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'webdriver_百度搜索')
        sleep(2)

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

執行結果:ui

.... ----------------------------------------------------------------------
Ran 4 tests in 51.427s

OK

能夠看到執行結果中成功返回咱們的測試用例數,併成功執行退出(項目中善用sleep,這裏只是作演示,sleep太多執行時間太長)
若是執行失敗spa

self.assertEqual(title,'selenium_百度搜索2')
AssertionError: 'selenium_百度搜索' != 'selenium_百度搜索2'
Ran 4 tests in 23.518s

FAILED (failures=1)
相關文章
相關標籤/搜索