使用装饰器重构以减少代码量

Refactoring using decorators to reduce code volume

我最近切换到一个新项目,我们所有的 selenium 测试都是用 Python 编写的。我想知道我是否可以通过使用装饰器来减少代码量

我们现在得到的是:

class BasePage(object):
    view_button = ".//a[text()='View']"
    create_button = ".//a[text()='Create']"
    #some code here

class BaseTestCase(unittest.TestCase):
    setUpclass(cls):
    #code here

    def find(cls,xpath):
        return cls.driver.find_element_by_xpath(xpath)


class SomeTest(BaseTestCase):
    def test_00_something(self):
        self.find(self.view_button).click()

我在想有没有办法将整个self.find(self.view_button).click()最小化为click.view_button

我听说可以使用装饰器来完成,但作为一个 Java 人,我在这方面收效甚微。

我不确定你会怎么做,所以你可以做到 click.view_button。但是,您可以为元素提供自己的 class。这样你就可以达到类似 view_button.click() 的效果。在我的项目中,我对所有元素都这样做。

class BaseElement:
    def __init__(self, driver, locator):
        self.driver = driver
        self.locator = locator

    def get(self):
        self.driver.find_element(locator)

    def click():
        self.get().click()


class ViewButton(BaseElement):
    def __init__(self, driver):
       locator = (By.XPATH, ".//a[text()='View']")
       super(ViewButton, self).__init__(driver, locator)


class BasePage(object):
    def __init__(self, driver):
        self.view_button = ViewButton(driver)


class BaseTestCase(unittest.TestCase):
    setUpclass(cls):
        # driver stuff
        self.page = BasePage(driver)


class SomeTest(BaseTestCase):
    def test_00_something(self):
        self.page.view_button.click()

感谢来自 RemcoW 的 post 我找到了一个解决方案,见下文

class ViewButton():

    def __init__(self,driver):
        self.driver = driver
        self.locator = (".//a[text()='View']")

    @property
    def click(self):
        return self.driver.find_element_by_xpath(self.locator).click()

class BaseTestCase(unittest.TestCase):
    #some code here

    @property
    def view(self):
        view = ViewButton(self.driver)
        return view

在测试用例中它是这样工作的

class TestThign(BaseTestCase):

    def test_00_something(self):
        self.view.click

您还可以查看以下解决方案;使用 :

创建新模块 - navigation.py
class Button():

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

    @property
    def click(self):
        return self.driver.find_element_by_xpath(self.locator).click()

class Navigation():

    """NAVIGATION COMMANDS """
    def goTo(self):
        #somethign

    def previousPage(self):
        #something

    """ BUTTONS """
    @property
    def view_button(self):
        xpath = ".//a[text()='View']"
        view = Button(self.driver,xpath)
        return view

   @property
   def create_button(self):
       xpath = ".//a[text()='Create']"
       create = Button(self.driver,xpath)
       return create

在basetestcase.py中:

class BaseTestCase(unittest.TestCase, Navigation)

      setUpClass(cls):
      #somethign here

您的测试用例将如下所示:

class TestSomething(BaseTestCase):

     def test_99_somethign(self):
         #finds .//a[text()='View'] and clicks
         self.view.click

         #create button
         self.create_button.click

这样,您就可以在测试中使用导航 class。另外,您将所有导航元素都放在一个地方