使用出色的Behave框架,但因缺乏 OOP 技能而遇到麻烦。
Behave 有一个内置的上下文命名空间,可以在其中在测试执行步骤之间共享对象。初始化我的 WebDriver 会话后,我一直在我的步骤之间传递它,使用它context
来保存所有内容。功能很好,但正如您在下面看到的,它绝不是 DRY。
我如何/在哪里可以将这些属性添加到step_impl()
orcontext
一次?
环境.py
from selenium import webdriver
def before_feature(context, scenario):
"""Initialize WebDriver instance"""
driver = webdriver.PhantomJS(service_args=service_args, desired_capabilities=dcap)
"""
Do my login thing..
"""
context.driver = driver
context.wait = wait
context.expected_conditions = expected_conditions
context.xenv = env_data
步骤.py
@given('that I have opened the blah page')
def step_impl(context):
driver = context.driver
wait = context.wait
expected_conditions = context.expected_conditions
xenv = context.xenv
driver.get("http://domain.com")
driver.find_element_by_link_text("blah").click()
wait.until(expected_conditions.title_contains("Blah page"))
@given(u'am on the yada subpage')
def step_impl(context):
driver = context.driver
wait = context.wait
expected_conditions = context.expected_conditions
xenv = context.xenv
if driver.title is not "MySubPage/":
driver.get("http://domain.MySubPage/")
wait.until(expected_conditions.title_contains("Blah | SubPage"))
@given(u'that I have gone to another page')
def step_impl(context):
driver = context.driver
wait = context.wait
expected_conditions = context.expected_conditions
xenv = context.xenv
driver.get("http://domain.com/MyOtherPahge/")