我作为TEA工作,我使用Python + Selenium Webdriver和Behave来运行我的测试。
我在上下文中定义了webdriver,如下所示
driver = webdriver.Chrome()
driver.implicitly_wait(5)
context.driver = driver
context.driver.get('https://www.ebay.com/')
这是在一个函数中定义的,但我的问题是,当我想在另一个步骤函数中使用驱动程序时,我没有得到驱动程序函数的片段。这使我浪费了很多时间来查找我要使用的函数的确切名称。
例如
@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)
,但我正在寻找的情况更具体。像上下文。驱动程序:'webdriver object'
是否有定义上下文的方法。驱动程序类型,所以我的ide将解释这是一个webdriver对象,并以这种方式获得驱动程序的方法?
我正在使用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()
你不需要定义两次type
需要的导入:
from selenium.webdriver.firefox.webdriver import WebDriver