【发布时间】:2019-07-14 20:05:29
【问题描述】:
当我从步骤 python 文件导入页面对象模型时,我得到一个 KeyError:“'name' not in globals”。 我正在使用 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()
features/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:“'name' 不在全局变量中”
【问题讨论】:
标签: python python-3.x selenium cucumber python-behave