Is it possible to use pytest-bdd with SeleniumBase?

496 Views Asked by At

This is how I would write a SeleniumBase/pytest-bdd test:

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()

However, this is not working. scenarios() function cannot see the when and then descriptors.

Any idea how to make this work, if it is possible ?

1

There are 1 best solutions below

0
On

You need to use SeleniumBase as a pytest fixture, rather than inheriting BaseCase directly. See "The sb pytest fixture" section at

So - in your example - you do not need to import BaseCase, and you should use "sb" instead of "self"

another example: 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]"