2

Feature file is as below

Feature: Nopcommerce Login

Scenario: login to nopcommerce website

Given nopcommerce page is displayed
When user enters username as admin@yourstore.com
When user enters password as admin
Then user is able to login to nocpmmerce website

step definition python file is as below

from pytest_bdd import scenarios, given, when, then 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pytest

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

@pytest.fixture()
def browser():
    driver = webdriver.Safari()
    yield driver
    driver.quit()

@given("nopcommerce page is displayed")
def webpage(browser):
    browser.get("http://admin-demo.nopcommerce.com")

@when("user enters username as admin@yourstore.com")
def enter_uname(browser):
    browser.find_element_by_id("Email").send_keys("admin@yourstore.com")

@when("user enters password as admin")
def enter_pwd(browser):
    browser.find_element_by_id("Password").send_keys("admin")
    browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div/div[2]/div[1]/div/form/div[3]/input").click()

@then("user is able to login to nocpmmerce website")
def loginsuccess(browser):
    assert browser.current_url == "https://admin-demo.nopcommerce.com/admin/"

when the step_def file is run, the following error message is displayed

Traceback (most recent call last):

File "~/tests/step_defs/test_NopcommerceLogin.py", line 6, in scenarios('../features/NopcommerceLogin.feature')

File "~/venv/lib/python3.8/site-packages/pytest_bdd/scenario.py", line 343, in scenarios features_base_dir = get_features_base_dir(module)

File "~/venv/lib/python3.8/site-packages/pytest_bdd/scenario.py", line 295, in get_features_base_dir return get_from_ini('bdd_features_base_dir', default_base_dir)

File "~/venv/lib/python3.8/site-packages/pytest_bdd/scenario.py", line 303, in get_from_ini config = CONFIG_STACK[-1]

IndexError: list index out of range

Lucky1234
  • 129
  • 1
  • 12
  • From that error message, the list named `CONFIG_STACK` is empty. – PM 2Ring Feb 16 '20 at 20:29
  • @PM 2Ring What does that mean? What changes do I have to do in the code to correct that? – Lucky1234 Feb 16 '20 at 20:34
  • Sorry, I don't know pytest-bdd, and I can't guess from the code you posted what could be the cause of your problem. I *assume* that list is supposed to have some kind of configuration data in it, but instead it's an empty list. – PM 2Ring Feb 16 '20 at 20:41

6 Answers6

3

It's not entirely the solution, and I have the same issue in PycharmIDE, but I suggest using terminal and start tests like:

pytest <test_fily.py>

for IDEA solution, still working on it

Vova
  • 3,117
  • 2
  • 15
  • 23
1

Change your configurations to use pytest configuration: screenshot

or try running using terminal : pipenv run python -m pytest

Bando
  • 1,223
  • 1
  • 12
  • 31
0

I guess the problem is that the pytest is not identifying any executable pytest method's in your step definition file. Please try changing the "scenarios" to "scenario" and add a pytest identifiable method below the same

@scenario('../features/NopcommerceLogin.feature')
def test_login():
      pass

This approach always works for me and is based on Pytest-BDD doc.

Arun Sasi
  • 316
  • 5
  • 15
  • @Lucky1234, could you please check and confirm if the issue is solved. :) – Arun Sasi Feb 26 '20 at 05:19
  • Thanks @Arun Sasi . I have tried both ways . Both times error occurs if i run it through “right click” and “run” option . But if i run it via terminal it executes . Not sure why it behaves like that – Lucky1234 Feb 26 '20 at 13:01
  • @Lucky1234 May I know the IDE which is being used? May be the issue is with the run configurations – Arun Sasi Feb 26 '20 at 15:14
  • Pycharm @Arun Sasi – Lucky1234 Feb 27 '20 at 16:07
  • @Lucky1234, please ensure the following: 1. In the run configurations, please ensure that script path and working directory is set 2. Interpreter is set as Python 3. Add content and source roots to PYTHONPATH is set. – Arun Sasi Feb 28 '20 at 08:18
  • In run configurations everything is set correctly.But still it behaves the same way – Lucky1234 Feb 29 '20 at 00:36
0

Make sure that pytest configured in pycham Enable Pytest for your project

Open the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework. In the Default test runner field select pytest. Click OK to save the settings

0

@Vova I found that in the run configuration, the Working directory was incorrectly set to the directory where the steps python file was, instead of the project root directory. Fixing that made the test run successfully in PyCharm.

0

I had this issue - it was simply alignment in the feature file, I had a space on the Scenario definition after 'Scenario' and before the colon. When I removed - the error no longer occured.