Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pytest-bdd integration with selenium base #1200

Answered by mdmintz
taruntechno asked this question in Q&A
Discussion options

Hello Team,
I need your help in knowing how to integrate pytest-bdd level with selenium base.
My feature file is
`@speedtest
Feature: Test Internet Speed
As a user I want to test the internet speed

Background:
Given Bell Speed test page shows up

Scenario: Speed Test
When User clicks on check speed button
Then Speed details are saved
`

Step Defs is
`from seleniumbase import BaseCase
import time
import pytest
from pytest_bdd import scenarios, given, when, then, parsers

scenarios('../features/speedtest.feature')

class SpeedTest(BaseCase):
@given('Bell Speed test page shows up')
def open_bell_speed_test(self):
self.open("https://support.bell.ca/internet/internet-speed-test")

@when('User clicks on check speed button')
def start_speed_test(self):
self.click('button[id="skst-enh-init-test-btn"]')

@then('Speed details are saved')
def test_bell_internet(self):
time.sleep(120)
self.save_screenshot("speedtest.png", folder="./downloaded_files")
self.assert_downloaded_file("speedtest.png")
`

When I run pytest it opens the browser but does nothing . If I run the code without pytest-bdd using following it works
class SpeedTest(BaseCase): def open_bell_speed_test(self): self.open("https://support.bell.ca/internet/internet-speed-test") self.click('button[id="skst-enh-init-test-btn"]') self.save_screenshot("speedtest.png", folder="./downloaded_files") self.assert_downloaded_file("speedtest.png")

Folder structure
CleanShot 2022年02月04日 at 18 57 04@2x

You must be logged in to vote

Hi @taruntechno, when using https://github.com/pytest-dev/pytest-bdd with SeleniumBase, you may need to use the SeleniumBase sb fixture format for compatibility. (Numbers 3 and 4 of syntax_formats)

Here's what the sb fixture format looks like, shown below: (pytest-bdd can be added onto that separately)

# "sb" pytest fixture test in a method with no class
def test_sb_fixture_with_no_class(sb):
 sb.open("https://google.com/ncr")
 sb.type('input[title="Search"]', "SeleniumBase GitHub\n")
 sb.click('a[href*="github.com/seleniumbase/SeleniumBase"]')
 sb.click('a[title="seleniumbase"]')
# "sb" pytest fixture test in a method inside a class
class Test_SB_Fixture:
 def test_sb_fix...

Replies: 2 comments 9 replies

Comment options

Hi @taruntechno, when using https://github.com/pytest-dev/pytest-bdd with SeleniumBase, you may need to use the SeleniumBase sb fixture format for compatibility. (Numbers 3 and 4 of syntax_formats)

Here's what the sb fixture format looks like, shown below: (pytest-bdd can be added onto that separately)

# "sb" pytest fixture test in a method with no class
def test_sb_fixture_with_no_class(sb):
 sb.open("https://google.com/ncr")
 sb.type('input[title="Search"]', "SeleniumBase GitHub\n")
 sb.click('a[href*="github.com/seleniumbase/SeleniumBase"]')
 sb.click('a[title="seleniumbase"]')
# "sb" pytest fixture test in a method inside a class
class Test_SB_Fixture:
 def test_sb_fixture_inside_class(self, sb):
 sb.open("https://google.com/ncr")
 sb.type('input[title="Search"]', "SeleniumBase GitHub\n")
 sb.click('a[href*="github.com/seleniumbase/SeleniumBase"]')
 sb.click('a[title="examples"]')
You must be logged in to vote
7 replies
Comment options

Even after matching the version both are on 98 same behaviour. I am on macos btw. @mdmintz

Comment options

@taruntechno Did you make any local changes to seleniumbase/core/browser_launcher.py? If there's an issue, you'd be able to track it down there via breakpoints. Likely a Chrome option wasn't valid, so it reverted to no special options (and then you would see a message such as "this browser is being controlled by automated test software", etc. Did that message appear when your test finally ran on Chrome in the second browser?

Comment options

@mdmintz No local changes were made to this . I am not seeing this message "this browser is being controlled by automated test software" I am attaching the screenrecording of this

chromebrowserissue.mov

.

Comment options

@taruntechno It appears you ran a very short test that first time.
Run with pytest -v to see if you can see the name.

Comment options

@mdmintz it seems to be running two tests and i dont think its respecting bdd
As first test method it ran is part of "then" statement which should run in the end.

collected 2 items

tests/step_defs/test_speed.py::test_bell_internet PASSED [ 50%]
tests/step_defs/test_speed.py::test_speed_test PASSED [100%]

Answer selected by mdmintz
Comment options

@mdmintz it seems to be running two tests and i dont think its respecting bdd
As first test method it ran is part of "then" statement which should run in the end.

collected 2 items

tests/step_defs/test_speed.py::test_bell_internet PASSED [ 50%]
tests/step_defs/test_speed.py::test_speed_test PASSED [100%]

You must be logged in to vote
2 replies
Comment options

@mdmintz Was able to fix it finally as i had my step_Defs methods have test in there name which was confusing pytest. After removing test from the name it was able to run properly. Thanks a lot for all your help really appreciated.

Comment options

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /