python webdriver 測試架構-資料驅動DDT的例子

來源:互聯網
上載者:User

標籤:data   strong   sleep   mbr   bsp   英文   expec   file   pytho   

先在cmd環境 運行 pip install ddt 安裝資料驅動ddt模組 

 

指令碼:

#encoding=utf-8

from selenium import webdriver

import unittest,time

import logging,traceback

import ddt

 

from selenium.common.exceptions import NoSuchElementException

 

#初始化日誌對象

logging.basicConfig(

    #記錄層級

    level=logging.INFO,

    #日誌格式

    #時間、代碼所在檔案名稱、程式碼號、記錄層級名稱、日誌資訊

    format=‘%(asctime)s %(filename)s[line:%(lineno)d]%(levelname)s %(message)s‘,

    #列印日誌的時間

    datefmt=‘%a, %d %b %Y %H:%M:%S‘,

    #記錄檔存放的目錄(目錄必須存在)及記錄檔名

    filename=‘d:/report.log‘,

    #開啟記錄檔的方式

    filemode=‘w‘

)

@ddt.ddt#裝飾器

class TestDemo(unittest.TestCase):

    def setUp(self):

        self.driver=webdriver.Firefox(executable_path=‘c:\\geckodriver‘)

    #資料驅動時指定的三個資料,每個資料是一個list

    @ddt.data([u"神奇動物在哪裡",u"葉茨"],

              [u"瘋狂動物城",u"古德溫"],

              [u"大話西遊之月光寶盒",u"周星馳"])

 

    @ddt.unpack#解包,將測試資料對應到testdata和expectdata,將上邊的list裡的兩個參數賦值給函數中,下邊有解包的例子

    def test_dataDrivenByObj(self,testdata,expectdata):#傳參數

        url="http://www.baidu.com"

        #訪問百度首頁

        self.driver.get(url)

        #設定隱式等待時間為10秒,個別瀏覽器版的驅動可能會有問題,待驗證

        self.driver.implicitly_wait(10)

        try:

            #找到搜尋輸入框,並輸入測試資料

            self.driver.find_element_by_id("kw").send_keys(testdata)

            #找到搜尋按鈕,並點擊

            self.driver.find_element_by_id(‘su‘).click()

            time.sleep(3)

            #斷言期望結果是否出現在頁面原始碼中

            self.assertTrue(expectdata in self.driver.page_source)

        except NoSuchElementException,e:

            logging.error("element in page not existed, abnormal stack info:"+str(traceback.format_exc()))

        except AssertionError,e:

            logging.info("search ‘%s‘,expected ‘%s‘, failed" %(testdata,expectdata))

        except Exception,e:

            logging.error("unknown error, error message:"+str(traceback.format_exc()))

        else:

            logging.info(‘search "%s", expected "%s" passed‘ %(testdata,expectdata))

    def tearDown(self):

        self.driver.quit()

 

if __name__==‘__main__‘:

    unittest.main()

 

結果:

如果日誌對象沒有問題的話,會把日誌打到檔案裡,如下(第一次運行時日誌對象中的filename錯寫成fimename了,就沒有產生report.log檔案,而是列印在了cmd裡)

亂碼是因為英文系統不支援中文導致的

D:\test_python>python task_test.py

Tue, 26 Jun 2018 12:48:12 task_test.py[line:54]INFO search "τ??σ??σè?τ??σ£?σ??Θ??", expected "σ?Φ??" passed

.Tue, 26 Jun 2018 12:48:28 task_test.py[line:54]INFO search "τ??τ?éσè?τ??σ??", expected "σ??σ╛╖μ?" passed

.Tue, 26 Jun 2018 12:48:43 task_test.py[line:54]INFO search "σ?oΦ?¥Φ?┐μ╕╕Σ?μ£êσà?σ?¥τ¢?", expected "σ??μ??Θ??" passed

.

----------------------------------------------------------------------

Ran 3 tests in 47.395s

 

OK

第二次運行時,修改了日誌對象,日誌就打在了report.log中了,控制台沒有打日誌

D:\test_python>python task_test.py

...

----------------------------------------------------------------------

Ran 3 tests in 46.251s

 

OK

 

Report.log:

內容:

Tue, 26 Jun 2018 12:49:41 task_test.py[line:54]INFO search "神奇動物在哪裡", expected "葉茨" passed

Tue, 26 Jun 2018 12:49:56 task_test.py[line:54]INFO search "瘋狂動物城", expected "古德溫" passed

Tue, 26 Jun 2018 12:50:12 task_test.py[line:54]INFO search "大話西遊之月光寶盒", expected "周星馳" passed

 

解包的例子:

>>> def add(a,b):

...     return a+b

...

>>> add(1,2)

3

>>> add((1,2))

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: add() takes exactly 2 arguments (1 given)

#直接傳元祖會報錯,但是前邊加個*就是解包的過程,把元素拆分出來,把元素分別賦值給add函數

>>> add(*(1,2))

3

python webdriver 測試架構-資料驅動DDT的例子

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.