Python 中的页面对象模式循环导入问题
Page Object Pattern Circular import problem in Python
我在页面对象模式中编写我的自动化框架,我在实现视图 classes 时遇到了这个错误。我已将此 classes 放在单独的模块中,我希望它们保持分开。问题是我希望在执行某些 UI 操作时 classes 中的实例方法到 return 另一个 class 的对象。
有没有办法在将 classes 放在单独的模块中时修复循环错误?
cart_page.py
from pages.base_page import BasePage
from utils.locators import CartLocators
from pages.main_page import MainPage
class CartPage(BasePage):
def __init__(self, driver):
self.locators = CartLocators()
super().__init__(driver, 'https://www.saucedemo.com/cart.html')
def click_continue_shopping(self):
self.find_element(*self.locators.CONTINUE_SHOPPING_BTN).click()
return MainPage(self.driver)
main_page.py
from pages.base_page import BasePage
from utils.locators import MainPageHeaderLocators, MainPageItemListLocators, InventoryItemLocators
from pages.cart_page import CartPage
class MainPage(BasePage):
def __init__(self, driver):
super().__init__(driver, "https://www.saucedemo.com/invetory.html")
self.header = MainPageHeader(self.driver)
self.item_list = MainPageItemList(self.driver)
self.inventory_item = InventoryItemPage(self.driver)
def open_cart(self):
self.header.open_cart()
return CartPage(self.driver)
E ImportError: cannot import name 'MainPage' from partially initialized module 'pages.main_page' (most likely due to a circular import) (/Users/marcin94/PycharmProjects/sauce_demo_ui_tests/pages/main_page.py)
不要使用from
。直接导入模块:
cart_page.py
from pages.base_page import BasePage
from utils.locators import CartLocators
import pages.main_page
class CartPage(BasePage):
def __init__(self, driver):
self.locators = CartLocators()
super().__init__(driver, 'https://www.saucedemo.com/cart.html')
def click_continue_shopping(self):
self.find_element(*self.locators.CONTINUE_SHOPPING_BTN).click()
return pages.main_page.MainPage(self.driver)
main_page.py
from pages.base_page import BasePage
from utils.locators import MainPageHeaderLocators, MainPageItemListLocators, InventoryItemLocators
import pages.cart_page
class MainPage(BasePage):
def __init__(self, driver):
super().__init__(driver, "https://www.saucedemo.com/invetory.html")
self.header = MainPageHeader(self.driver)
self.item_list = MainPageItemList(self.driver)
self.inventory_item = InventoryItemPage(self.driver)
def open_cart(self):
self.header.open_cart()
return pages.cart_page.CartPage(self.driver)
我在页面对象模式中编写我的自动化框架,我在实现视图 classes 时遇到了这个错误。我已将此 classes 放在单独的模块中,我希望它们保持分开。问题是我希望在执行某些 UI 操作时 classes 中的实例方法到 return 另一个 class 的对象。
有没有办法在将 classes 放在单独的模块中时修复循环错误?
cart_page.py
from pages.base_page import BasePage
from utils.locators import CartLocators
from pages.main_page import MainPage
class CartPage(BasePage):
def __init__(self, driver):
self.locators = CartLocators()
super().__init__(driver, 'https://www.saucedemo.com/cart.html')
def click_continue_shopping(self):
self.find_element(*self.locators.CONTINUE_SHOPPING_BTN).click()
return MainPage(self.driver)
main_page.py
from pages.base_page import BasePage
from utils.locators import MainPageHeaderLocators, MainPageItemListLocators, InventoryItemLocators
from pages.cart_page import CartPage
class MainPage(BasePage):
def __init__(self, driver):
super().__init__(driver, "https://www.saucedemo.com/invetory.html")
self.header = MainPageHeader(self.driver)
self.item_list = MainPageItemList(self.driver)
self.inventory_item = InventoryItemPage(self.driver)
def open_cart(self):
self.header.open_cart()
return CartPage(self.driver)
E ImportError: cannot import name 'MainPage' from partially initialized module 'pages.main_page' (most likely due to a circular import) (/Users/marcin94/PycharmProjects/sauce_demo_ui_tests/pages/main_page.py)
不要使用from
。直接导入模块:
cart_page.py
from pages.base_page import BasePage
from utils.locators import CartLocators
import pages.main_page
class CartPage(BasePage):
def __init__(self, driver):
self.locators = CartLocators()
super().__init__(driver, 'https://www.saucedemo.com/cart.html')
def click_continue_shopping(self):
self.find_element(*self.locators.CONTINUE_SHOPPING_BTN).click()
return pages.main_page.MainPage(self.driver)
main_page.py
from pages.base_page import BasePage
from utils.locators import MainPageHeaderLocators, MainPageItemListLocators, InventoryItemLocators
import pages.cart_page
class MainPage(BasePage):
def __init__(self, driver):
super().__init__(driver, "https://www.saucedemo.com/invetory.html")
self.header = MainPageHeader(self.driver)
self.item_list = MainPageItemList(self.driver)
self.inventory_item = InventoryItemPage(self.driver)
def open_cart(self):
self.header.open_cart()
return pages.cart_page.CartPage(self.driver)