簡體   English   中英

屬性 Python 的 function 中的參數類型定義

[英]Parameter type definition in function of attribute Python

我是一名 TEA,我使用 Python + Selenium Webdriver and Behave 運行我的測試。

為此,我在上下文中定義 webdriver 如下

    driver = webdriver.Chrome()
    driver.implicitly_wait(5)
    context.driver = driver
    context.driver.get('https://www.ebay.com/')

這是在一個 function 中定義的,但我的問題是,當我想在另一個步驟 function 中使用驅動程序時,我沒有得到驅動程序功能的片段。 這讓我浪費了很多時間來尋找我正在嘗試使用的 function 的確切名稱。

例如

@then('Click the "Search" button')
def step_impl(context: behave.runner.Context):
    """
    :type context: behave.runner.Context
    """
    search_btn = context.driver.find_element_by_xpath('//*[@id="gh-btn"]')
    search_btn.click()

我知道我可以定義直接接收的參數的類型,例如

var_name: str (def step_impl(context: behave.runner.Context) ,但我正在尋找的情況更具體。比如 context.driver: 'webdriver object'

有沒有辦法定義 context.driver 類型,所以我的 ide 會解釋這是一個 webdriver object 並以這種方式獲取驅動程序的方法?

我正在使用 PyCharm Professional

PyCharm 應該能夠使用這個:

@then('Click the "Search" button')
def step_impl(context: behave.runner.Context):
    """
    :type context: behave.runner.Context
    """
    driver: webdriver.Chrome = context.driver
    search_btn = driver.find_element_by_xpath('//*[@id="gh-btn"]')
    search_btn.click()
def step_impl(context: WebDriver):
    search_btn = context.driver.find_element_by_xpath('//*[@id="gh-btn"]')
    search_btn.click()

您不必定義類型兩次。

您需要的導入:

from selenium.webdriver.firefox.webdriver import WebDriver

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM