是否可以将 pytest-bdd 与 SeleniumBase 一起使用?

问题描述

这就是我编写 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() 函数无法看到 whenthen 描述符。

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

解决方法

您需要使用 SeleniumBase 作为 pytest 固定装置,而不是直接继承 BaseCase。请参阅

上的“The sb pytest fixture”部分

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...