使用伟大的Behave框架,但遇到我缺乏OOP技能的麻烦。将常见属性添加到Behave方法
行为具有内置的上下文命名空间,其中可以在测试执行步骤之间共享对象。在初始化我的WebDriver会话之后,我一直使用这个context
来保存所有内容。功能很好,但正如你可以在下面看到的,它是干的。
如何/在哪里可以将这些属性添加到step_impl()
或context
一次?
environment.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
steps.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/")
就是如何避免一切从'拆包的问题每个'step_impl'函数中的上下文吗?我想说,你可以通过跳过稍后不打算使用的项目(例如,上个版本中的'xenv','wait'和'expected_conditions')来删除一堆。除此之外,您可以跳过一些拆包,并直接使用上下文的属性,例如'context.driver.get(无论)'。我对Behave一无所知,所以我不确定这是否是一个答案。 – Blckknght
谢谢。是的,为了避免所有的拆包*和*每次都通过调用'context.attribute.something'来避免重复,这两者都没有感觉到Pythonic –