我正在用頁面物件模式撰寫我的自動化框架,并且在實作視圖類時遇到了這個錯誤。我已經將這些類放在單獨的模塊中,我希望它們保持分開。問題是我希望我的兩個類中的實體方法在執行某些 UI 操作時回傳另一個類的物件。
在單獨的模塊中使用此類時,有沒有辦法修復回圈錯誤?
購物車頁面.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)
uj5u.com熱心網友回復:
不要使用from. 直接匯入模塊:
購物車頁面.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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391992.html
