【问题标题】:Wrapping the code into a method in a different class in Python将代码包装到 Python 中不同类的方法中
【发布时间】:2020-04-05 13:22:06
【问题描述】:

--主文件

    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

-- 结帐页面文件

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】:

    这可能不是最好的实现,但我会在 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]
    

    【讨论】:

      猜你喜欢
      • 2011-09-10
      • 2016-06-29
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多