从步骤python文件导入页面对象模型时,出现KeyError:“'名称'不在全局变量中”。 我正在使用python 3。
功能/步骤/tutorial.feature
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from behave import *
from ..lib.pages.login_page import LoginPage
use_step_matcher("re")
@given("user is lead to the login page")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
context.browser.get("https://www.phptravels.net/login")
@when("I log in")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
page = LoginPage(context)
page.login()
功能/lib/pages/login_page.py
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
class LoginPage:
locator_dictionary = {
"email_field": (By.NAME, 'username'),
"password_field": (By.ID, 'password'),
"login_button": (By.CSS_SELECTOR, '.btn.btn-action.btn-lg.btn-block.loginbtn'),
"accept_cookies": (By.ID, 'cookyGotItBtn')
}
def __init__(self, context):
self.browser = context.browser
def login(self, username="xxxxx", passwd="xxxxx"):
b = self.browser
WebDriverWait(b, 10).until(
ec.visibility_of_element_located((By.ID, "cookyGotItBtn"))).click()
b.find_element_by_name('username').send_keys(username)
b.find_element_by_id('password').send_keys(passwd)
b.find_element_by_css_selector('.btn.btn-action.btn-lg.btn-block.loginbtn').click()
features / tutorial.feature
Feature: showing off behave
Background: user is lead to the login page
Given user is lead to the login page
Scenario: login invalid
When I log in
我的目录结构是:
E:features\tutorial.feature
E:features\__init__.py
E:features\lib\__init__.py
E:features\lib\pages\__init__.py
E:features\lib\pages\login_page.py
E:features\steps\__init__.py
E:features\steps\tutorial.py
文件“ steps / tutorial.py”,第8行,在 从..lib.pages.login_page导入LoginPage KeyError:“'名称'不在全局变量中”
答案 0 :(得分:0)
您很可能需要在导入路径中放入包名称:
from features.lib.pages.login_page import LoginPage
答案 1 :(得分:0)
您能确认您没有相对进口吗?
作为缓解措施,您可以尝试在使用LoginPage之前将其导入。
@when("I log in")
def step_impl(context):
"""
:type context: behave.runner.Context
"""
from ..lib.pages.login_page import LoginPage
page = LoginPage(context)
page.login()