我可以在另一个类的Python方法中将类属性作为参数传递吗?

时间:2019-08-15 08:44:36

标签: python selenium class object methods

我目前正在进行质量检查自动化测试,我想简化代码(就像Python一样)。

这是一个定位器文件:

Class LoginPageLocators(object):       “”“登录页面定位符”“”

  USERNAME = (By.ID, 'username')
  PASSWORD = (By.ID, 'password')
  LOGIN_BTN = (By.ID, 'send')    # As you can see, it is just a cool way to access different parts of the UI

我有一个基本的page.py文件,它类似于我的页面类的“蓝图”:

页面类(对象):

  def __init__(self, driver, base_url=MY_URL)

      self.base_url = base_url
      self.driver = driver
      self.timeout = 30

  def find_element(self, *locator):
      """
      This method finds an element on the page with the appropriate locator

      :param locator:
      :return: element on page:
      """

      return self.driver.find_element(*locator)  # This is the thing I use to find elements on the page

在我的pages.py(其中包含不同的Page类)中使用了以下方法:

class LoginPage(Page):#它从基类继承了一些功能,例如find_element(如下所示)       “”“”登录页面类别“”“

  def __init__(self, driver):
      self.locator = LoginPageLocators
      super().__init__(driver)

  def click_login(self):
      """
      This function performs the login functionality with the given credentials

      :param user:
      :return: login functionality:
      """

      self.find_element(*self.locator.LOGIN_BTN).click()  # it is a cool way to access stuff,
                                                            i hope it is clear how it works

现在,我的实际问题是:

这是访问MainMenu页面类中的页面的示例方法:

       # Example Page 1
   def access_node_connector(self):
                                       #locator
      self.find_element(*self.locator.NODE_CONNECTOR).click()

      # Example Page 2
  def access_jenkins_successful_jobs(self):
                                          #locator
      self.find_element(*self.locator.SUCCESSFUL_JENKINS_JOBS).click()

现在,问题是其中有15种,而且我不认为像这样编程是pythonic。

我的想法是创建一个点击方法:

 def click(self, page):

 self.find_element(*self.locator.page).click()   # However it gives me unresolved attribute reference in Locators Class

我尝试了f弦和类似的东西,但是在这种情况下不起作用。有没有一种方法可以将*self.locator.之后的部分替换为我的函数的参数?预先感谢您<3

0 个答案:

没有答案