0

这就是我编写 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()

但是,这是行不通的。场景()函数看不到whenthen描述符。

如果可能的话,知道如何进行这项工作吗?

4

1 回答 1

1

您需要使用 SeleniumBase 作为pytest夹具,而不是直接继承 BaseCase。请参阅“某人pytest夹具”部分

所以 - 在你的例子中 - 你不需要 import 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]"

于 2021-03-23T09:01:20.710 回答