integration with Behave or Pytest-bdd #13
-
Hi, i want to know if screenpy can integrate with behave or pytest-bdd to use .feature files in gherkin language Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, great question! I believe it can! With BDD-style frameworks like // cart.feature
Scenario: Add an Item to Cart
Given I am on the homepage
When I click on the "daily deals" link
And I add 1x"Strawberry Gushers" to my cart
Then I should see 1 item is in my cart and your step definitions could use ScreenPy (maybe with ScreenPy Selenium). Let's use // maybe several step definition files
from pytest_bdd import given, when, then, parsers
from screenpy import AnActor
from screenpy.actions import Eventually
from screenpy_selenium.abilities import BrowseTheWeb
from screenpy_selenium.actions import Click
from ui.home_page import URL
@pytest.fixture(scope="session") # not sure if this is right, i haven't used pytest-bdd!
def Xander():
"""Our example actor, Xander!"""
the_actor = AnActor.named("Xander").who_can(BrowseTheWeb.with_firefox())
yield the_actor
the_actor.exit()
@given("I am on the homepage")
def start_on_homepage(Xander):
Xander.attempts_to(Open.their_browser_on(URL))
@when(parsers.re('I click on the "(?P<link_text>[^"]+)" link'))
def click_on_link_by_link_text(Xander, link_text): # also not sure if this parameter order is correct
link = Target.the(f'"{link_text}" link').located_by(
f'//a[contains(text(), "{link_text}")]'
)
Xander.attempts_to(Eventually(Click.on_the(link)))
@when(parsers.re('I add (?P<num>\d+)x"(?P<item>[^"]+)" to my cart'))
def add_num_item_to_cart(Xander, num, item):
item_quantity_field = Target.the(f"{item}'s quantity field").located_by(
f'input[name="{item}"]'
)
add_to_cart_button = Target.the(f"{item}'s add to cart button").located_by(
f'button[value="{item}"]'
)
Xander.attempts_to(
Eventually(Enter.the_text(num).into_the(item_quantity_field)),
Click.on_the(add_to_cart_button),
)
... I made a lot of assumptions because i haven't actually done this before, but hopefully that gives you the idea. When i have written BDD suites, the difficult part for me was always how to organize those step files. I think using ScreenPy's Actors as fixtures will help you pass state around in a way that is easy to follow, and i also think it would fit nicely with the user-focused nature of BDD suites. If you do wind up whipping up a proof-of-concept or adopting ScreenPy for your suite, i would love to hear how it turns out (and see it, if it's OK to post publicly)! Does this answer your question? |
Beta Was this translation helpful? Give feedback.
-
Hi. Thank you very much for the answer, I will try with the example you give and then I will see how to integrate it with the project. |
Beta Was this translation helpful? Give feedback.
Hi, great question! I believe it can!
With BDD-style frameworks like
behave
andpytest-bdd
, you'll have to organize your step definitions. I believe you can use ScreenPy as the way you interact with your application under test, to organize your step definitions more closely. For example, an "add to cart" scenario might look like this:and your step definitions could use ScreenPy (maybe with ScreenPy Selenium). Let's use
pytest-bdd
for the example: