将代码包装到Python中其他类的方法中

问题描述 投票:0回答:1

-主文件

    def test_e2e(self):
    # Home Page
    # Object created for HomePage class in home_page file
    home_page = HomePage(self.driver)

    # CheckOut Page
    # Object created for CheckOutPage class in home_page file
    checkout_page = home_page.shop_items()

    # get product names
    products = checkout_page.get_products()

    for product in products:
        product_name = product.find_element_by_css_selector("div h4").text

-checkoutpagefile

class CheckOutPage:
products = (By.CSS_SELECTOR, "div[class='card h-100']")
product_text = (By.CSS_SELECTOR, "div h4")

def __init__(self, driver):
    self.driver = driver

#  products = self.driver.find_elements_by_css_selector("div[class='card h-100']")
# find_element_by_css_selector("div h4").text

def get_products(self):
    # get the products
    return self.driver.find_elements(*CheckOutPage.products)

def getproduct_text(self):
    # get product text
    pass

我只想在主文件中删除此“” .find_element_by_css_selector(“ div h4”)。text“”部分,并以一种方法包装到checkoutpage类中,我该如何实现?

python python-3.x selenium selenium-webdriver pageobjects
1个回答
0
投票

这可能不是最好的实现,但是我将向CheckOutPage类添加以下方法。

# Product as an argument
def get_product_text(self, product):
    return product.find_element_by_css_selector("div h4").text

# Products as an argument
def get_products_text(self, products):
    return [product.find_element_by_css_selector("div h4").text for product in products]

# If the sole purpose of get_products method is to only get the products text
def get_products_text(self):
    products = self.get_products()
    return [product.find_element_by_css_selector("div h4").text for product in products]
© www.soinside.com 2019 - 2024. All rights reserved.