天天看點

Python 裝飾器填坑指南 | 最常見的報錯資訊、原因和解決方案

Python 裝飾器填坑指南 | 最常見的報錯資訊、原因和解決方案
本文為霍格沃茲測試學院學員學習筆記。

Python 裝飾器簡介

裝飾器(Decorator)是 Python 非常實用的一個文法糖功能。裝飾器本質是一種傳回值也是函數的函數,可以稱之為“函數的函數”。其目的是在不對現有函數進行修改的情況下,實作額外的功能。

在 Python 中,裝飾器屬于純粹的“文法糖”,不使用也沒關系,但是使用的話能夠大大簡化代碼,使代碼更加簡潔易讀。

最近在霍格沃茲測試學院的《Python 測試開發實戰進階》課程中學習了 App 自動化測試架構的異常處理,存在一定重複代碼,正好可以當作題材,拿來練習一下裝飾器。

裝飾器學習資料,推薦參考 RealPython

https://realpython.com/primer-on-python-decorators/

本文主要彙總記錄 Python 裝飾器的常見踩坑經驗,列舉報錯資訊、原因和解決方案,供大家參考。

裝飾器避坑指南

坑 1:Hint: make sure your test modules/packages have valid Python names.

報錯資訊

test_market.py:None (test_market.py)
ImportError while importing test module 'D:\project\Hogwarts_11\test_appium\testcase\test_market.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_market.py:9: in <module>
    from test_appium.page.app import App
..\page\app.py:12: in <module>
    from test_appium.page.base_page import BasePage
..\page\base_page.py:16: in <module>
    from test_appium.utils.exception import exception_handle
..\utils\exception.py:11: in <module>
    from test_appium.page.base_page import BasePage
E   ImportError: cannot import name 'BasePage' from 'test_appium.page.base_page' (D:\project\Hogwarts_11\test_appium\page\base_page.py)           

原因

exception.py 檔案和 base_page.py 檔案之間存在互相調用關系。

解決方案

把循環調用的包引入資訊放在函數内。隻要一方的引用資訊放在函數裡即可,不必兩邊都放。

我隻在 exception.py 檔案裡改了,base_page.py 保持不變。

exception.py

def exception_handle(func):
    def magic(*args, **kwargs):
        # 防止循環調用報錯
        from test_appium.page.base_page import BasePage
        # 擷取BasePage執行個體對象的參數self,這樣可以複用driver
        _self: BasePage = args[0]           
坑 2:IndexError: tuple index out of range
test_search.py:None (test_search.py)
test_search.py:11: in <module>
    from test_appium.page.app import App
..\page\app.py:12: in <module>
    from test_appium.page.base_page import BasePage
..\page\base_page.py:52: in <module>
    class BasePage:
..\page\base_page.py:74: in BasePage
    def find(self, locator, key=None):
..\page\base_page.py:50: in exception_handle
    return magic()
..\page\base_page.py:24: in magic
    _self: BasePage = args[0]
E   IndexError: tuple index out of range           

第一次寫裝飾器真的很容易犯這個錯,一起來看下哪裡寫錯了。

def decorator(func):
    def magic(*args, **kwargs):
        _self: BasePage = args[0]
        ...
        return magic(*args, **kwargs)
    # 這裡的問題!!!不應該傳回函數調用,要傳回函數名稱!!!
    return magic()             

為什麼傳回函數調用會報這個錯呢?

因為調用 magic() 函數的時候,沒有傳參進去,但是 magic() 裡面引用了入參,這時 args 沒有值,自然就取不到 args[0] 了。

去掉括弧就好了。

def decorator(func):
    def magic(*args, **kwargs):
        _self: BasePage = args[0]
        ...
        return magic(*args, **kwargs)
    # 傳回函數名,即函數本身
    return magic           
坑 3:異常處理隻執行了1次,自動化無法繼續

主要是定位元素過程中出現的各種異常,

NoSuchElementException

TimeoutException

等常見問題。

異常處理後,遞歸邏輯寫得不對。

return func()

執行了

func()

,跳出了異常處理邏輯,是以異常處理隻執行一次。

正确的寫法是

return magic()

感覺又是裝飾器小白容易犯的錯誤 …emmm…. :no_mouth:

為了直覺,已過濾不重要代碼,異常處理邏輯代碼會在文末放出。

def exception_handle(func):
    def magic(*args, **kwargs):
        _self: BasePage = args[0]
        try:
            return func(*args, **kwargs)
        # 彈窗等異常處理邏輯
        except Exception as e:
            for element in _self._black_list:
                elements = _self._driver.find_elements(*element)
                if len(elements) > 0:
                    elements[0].click()
                    # 異常處理結束,遞歸繼續查找元素 
                    # 這裡之前寫成了return func(*args, **kwargs),是以異常隻執行一次!!!!!
                    return magic(*args, **kwargs)
            raise e
    return magic           
坑 4:如何複用 driver?

問題

自己剛開始嘗試寫裝飾器的時候,發現一個問題。

裝飾器内需要用到 find_elements,這時候 driver 哪裡來?還有 BasePage 的私有變量 error_max 和 error_count 怎麼擷取到呢?建立一個 BasePage 對象?然後通過 func 函數來傳遞 driver ?

func 的 driver 是私有的,不能外部調用(事實證明可以emmm…)。

我嘗試把異常相關的變量做成公共的,沒用,還是無法解決 find_elements 的調用問題。

思寒老師的做法是,在裝飾器裡面建立一個

self

變量,取

args[0]

,即函數 func 的第一個入參

self

_self: BasePage = args[0]

這一簡單的語句成功解答了我所有的疑問。

類函數定義裡面 self 代表類自身,是以可以擷取

._driver

屬性,進而調用 find_elements。

坑 5:AttributeError

找到元素後,準備點選的時候報錯

EINFO:root:('id', 'tv_search')
INFO:root:None
INFO:root:('id', 'image_cancel')
INFO:root:('id', 'tv_agree')
INFO:root:('id', 'tv_search')
INFO:root:None

test setup failed
self = <test_appium.testcase.test_search.TestSearch object at 0x0000018946B70940>

    def setup(self):
>       self.page = App().start().main().goto_search()

test_search.py:16: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <test_appium.page.main.MainPage object at 0x0000018946B70780>

    def goto_search(self):
>       self.find(self._search_locator).click()
E       AttributeError: 'NoneType' object has no attribute 'click'

..\page\main.py:20: AttributeError           

看了下 find 函數,找到元素後,有傳回元素本身。

@exception_handle
def find(self, locator, key=None):
    logging.info(locator)
    logging.info(key)
    # 定位符支援元組格式和兩個參數格式
    locator = locator if isinstance(locator, tuple) else (locator, key)
    WebDriverWait(self._driver, 10).until(expected_conditions.visibility_of_element_located(locator))
    element = self._driver.find_element(*locator)
    return element           

那就是裝飾器寫得不對了:

def exception_handle(func):
    def magic(*args, **kwargs):
        _self: BasePage = args[0]
        try:
            # 這裡隻是執行了函數,但是沒有return
            func(*args, **kwargs)
        # 彈窗等異常處理邏輯
        except Exception as e:
            raise e
    return magic           

要在裝飾器裡面傳回函數調用,要不然函數本身的傳回會被裝飾器吃掉。

def exception_handle(func):
    def magic(*args, **kwargs):
        _self: BasePage = args[0]
        try:
            # return函數執行結果
            return func(*args, **kwargs)
        # 彈窗等異常處理邏輯
        except Exception as e:
            raise e
    return magic           

思考:

寫裝飾器的時候,各種return看着有點頭暈。每個函數裡面都可以return,分别代表什麼含義呢???

def exception_handle(func):
    def magic(*args, **kwargs):
        _self: BasePage = args[0]
        try:
            # 第1處 return:傳遞func()函數的傳回值。如果不寫,原有return則失效
            return func(*args, **kwargs)
        # 彈窗等異常處理邏輯
        except Exception as e:
            for element in _self._black_list:
                elements = _self._driver.find_elements(*element)
                if len(elements) > 0:
                    elements[0].click()
                    # 異常處理結束,遞歸繼續查找元素 
                    # 第2處 return:遞歸調用裝飾後的函數。magic()表示新函數,func()表示原函數,不可混淆
                    return magic(*args, **kwargs)
            raise e
    # 第3處 return:傳回裝飾後的函數,裝飾器文法。不能傳回函數調用magic()
    return magic           

裝飾器完整實作

exception.py

import logging

logging.basicConfig(level=logging.INFO)


def exception_handle(func):
    def magic(*args, **kwargs):
        # 防止循環調用報錯
        from test_appium.page.base_page import BasePage
        # 擷取BasePage執行個體對象的參數self,這樣可以複用driver
        _self: BasePage = args[0]
        try:
            # logging.info('error count is %s' % _self._error_count)
            result = func(*args, **kwargs)
            _self._error_count = 0
            # 傳回調用函數的執行結果,要不然傳回值會被裝飾器吃掉
            return result
        # 彈窗等異常處理邏輯
        except Exception as e:
            # 如果超過最大異常處理次數,則抛出異常
            if _self._error_count > _self._error_max:
                raise e
            _self._error_count += 1
            for element in _self._black_list:
                # 用find_elements,就算找不到元素也不會報錯
                elements = _self._driver.find_elements(*element)
                logging.info(element)
                # 是否找到彈窗
                if len(elements) > 0:
                    # 出現彈窗,點選掉
                    elements[0].click()
                    # 彈窗點掉後,重新查找目标元素
                    return magic(*args, **kwargs)
            # 彈窗也沒有出現,則抛出異常
            logging.warning("no error is found")
            raise e
    return magic           

一點學習心得

“紙上得來終覺淺,絕知此事要躬行”。遇到問題後嘗試自主解決,這樣踩過的坑才印象深刻。

是以,建議大家最好先根據自己的了解寫一遍裝飾器,遇到問題實在沒有頭緒了,再參考思寒老師的解法,那時會有一種豁然開朗的感覺,這樣學習的效果最好。

以上,Python 裝飾器踩到的這些坑,如有遺漏,歡迎補充~

更多技術文章分享及軟體測試資料