11

Directory structure:

app\features\login.feature
app\features\__init__.py
app\features\steps\__init__.py
app\features\steps\login.py
app\fixtures\__init__.py
app\fixtures\fixtures.py
app\models\__init__.py
app\models\login.py

Here is what is inside of my files. The error occurs on the following file when I run behave app\features\steps\login.py on the from ...models.login import Login

from behave import *
from ...models.login import Login
from ...models.footer import Footer

@given("unauthenticated user loads the home page")
def step_impl(context):
    assert Login.is_sign_in_submit_displayed(context, Login.signinbutton)

@step("sign-in button is clicked")
def step_impl(context, signinbutton):
    Login.tap_sign_in(context, Login.signinbutton)

@when("login form is populated with valid credentials")
def step_impl(context):
    Login.enter_email(context, Login.email, DEFAULT_EMAIL)
    Login.enter_password(context, Login.password, DEFAULT_PASSWORD)

@then("login is successful")
def step_impl(context):
    assert Login.home_page.is_rewards_displayed(context, Footer.menubutton)
    assert Login.home_page.is_account_displayed(context, Footer.accountbutton

And here is the file it's failing to find in app\models\login.py:

class Login():
    """Login screen object definitions"""
    signinbutton = ".//*[@text='SIGN IN']"
    email = ".//*[contains(@class, 'EditText')]"
    password = "(.//*[@class='android.widget.EditText'])[2]"

    def is_sign_in_submit_displayed(self, signinbutton):
        """Is the 'SIGN IN' button in the 'Login Screen' displayed?"""
        self.browser.implicitly_wait(30)
        return self.browser.find_element_by_xpath(signinbutton)

    def enter_email(self, emailelement, email):
        """Enter text into 'Email' Field in the 'Login Screen'"""
        self.browser.find_element_by_xpath(emailelement).send_keys(email)

    def enter_password(self, passelement, password):
        """Enter text into 'Password' Field in the 'Login Screen"""
        self.browser.find_element_by_xpath(passelement).send_keys(password)

    def click_sign_in_submit(self, signinbutton, menubutton, accountButton):
        """Click Sign in button and await Home Screen Nav Footer showing 'ACCOUNT' Button appearing"""
        self.browser.tap(signinbutton)
        self.browser.find_element_by_xpath(menubutton)
        self.browser.find_element_by_xpath(accountButton)

I'm following the docs on this site - https://docs.python.org/2.5/whatsnew/pep-328.html

But when I run this app, it returns the error "KeyError: "'__name__' not in globals"

I searched through the SO threads and none of them seem to address this. My IDE Pycharm seems to think the import is fine, and if I change it a little bit it highlights it as an error.

Full stack trace:

MacBook-Pro:app jasonlegako$ behave
Exception KeyError: "'__name__' not in globals"
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/bin/behave", line 10, in <module>
    sys.exit(main())
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/__main__.py", line 183, in main
    return run_behave(config)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/__main__.py", line 127, in run_behave
    failed = runner.run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 804, in run
    return self.run_with_paths()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 809, in run_with_paths
    self.load_step_definitions()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 796, in load_step_definitions
    load_step_modules(step_paths)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner_util.py", line 412, in load_step_modules
    exec_file(os.path.join(path, name), step_module_globals)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner_util.py", line 386, in exec_file
    exec(code, globals_, locals_)
  File "features/steps/login.py", line 2, in <module>
    from ...app.models.login import Login
KeyError: "'__name__' not in globals"
  • I have tried: ```from ...app.models.login import Login``` and ```from ..models.login import Login``` Both of which return the same error. –  Nov 19 '19 at 16:22
  • 1
    Can you edit your question to include the **full** stacktrace? – FHTMitchell Nov 19 '19 at 16:33
  • Updated ty for looking –  Nov 19 '19 at 16:37
  • 2
    I think it's because you're trying to do relative imports via `behave` which seems to be unable to handle them. I'm guessing when it runs `exec_file`, no `__name__` is generated so relative imports fail. – FHTMitchell Nov 19 '19 at 16:42
  • 3
    Hi, did you ever solve this? I have a very similar problem – jamiet May 07 '21 at 10:04

1 Answers1

6

Reoirganize your code that it handles as

from ...models.login import Login
from ...models.footer import Footer

to

from models.login import Login
from models.footer import Footer
Alan Turing
  • 2,482
  • 17
  • 20