问题描述
这就是我编写 SeleniumBase/pytest-bdd 测试的方式:
ddg.feature:
Feature: browse DuckDuckGo
Going to DuckDuckGo webpage.
Scenario: I can see the title
When I go to DuckDuckGo webpage
Then Duck is present in the title
test_ddg.py:
from seleniumbase import BaseCase
from pytest_bdd import scenarios,when,then
scenarios("./ddg.feature")
class MyTestClass(BaseCase):
@when("I go to DuckDuckGo webpage")
def go_to_ddg(self):
self.open('https://duckduckgo.com/')
@then("Duck is present in the title")
def is_title_present(self):
assert 'Duck' in self.get_title()
然而,这是行不通的。 scenarios() 函数无法看到 when 和 then 描述符。
如果可能的话,知道如何进行这项工作吗?
解决方法
您需要使用 SeleniumBase 作为 pytest
固定装置,而不是直接继承 BaseCase。请参阅
pytest
fixture”部分
- https://seleniumbase.io/help_docs/syntax_formats/ 或 这个答案:
- https://gitter.im/seleniumbase/SeleniumBase?at=5dc421f9e469ef4358dc918e
所以 - 在你的例子中 - 你不需要导入 BaseCase
,你应该使用“sb”而不是“self”
另一个例子:features/homepage.feature
@homepage
Feature: Homepage
Scenario: Homepage this and that
Given the browser is at the homepage
When the user clicks this
Then that is shown
step_defs/homepage_test.py
from pytest_bdd import scenarios,given,when then
from .pom import *
# Constants
PAGE = 'https://seleniumbase.io'
# Scenarios
scenarios('../features/homepage.feature')
# Given Steps
@given('the browser is at the homepage')
def the_browser_is_at_the_homepage(sb):
"""the browser is at the homepage."""
sb.get(PAGE)
# When Steps
@when('the user clicks this')
def the_user_clicks_this(sb):
"""the user clicks this."""
sb.click(Menu.this)
# Then Steps
@then('that is shown')
def that_is_shown(sb):
"""that is shown."""
sb.assert_text('The sb pytest fixture',SyntaxPage.that)
step_defs/pom.py
class Menu():
this = "//nav//a[contains(text(),'Syntax Formats')]"
class SyntaxPage():
that = "(//h3)[4]"