【问题标题】:Python Selenium Webdriver add method to Webelements/ custom 'assert' statementPython Selenium Webdriver 将方法添加到 Webelements/自定义“断言”语句
【发布时间】:2018-08-06 13:51:59
【问题描述】:

我正在尝试将一些methods 添加到我经常使用的WebElements。我想出了如何让它工作,但现在我的断言语句失败了。这就是我所拥有的。如何让我的assert 工作?

def is_below(self, above_element):
    below = self.location['y']
    above = above_element.location['y']

    self.assertLess(above, below)

WebElement.is_below = WebElement_is_below

实际上,这就是我想要弄清楚的所有逻辑:

class ModifiedTestCase(TestCase):
    def is_below(self, above_element):
            below = self.location['y']
            above = above_element.location['y']

        self.assertLess(above, below)

        WebElement.is_below = WebElement_is_below


class SeleniumTest(ModifiedTestCase):

    def test_web_page(self):

        above_element = self.find_element()
        below_element = self.find_element()

        below_element.is_below(above_element)

我得到的错误是“WebElement 没有属性 assertIn”。我知道我可以给它传递一个driver 参数,但这会破坏一些简单性。

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver webdriver


    【解决方案1】:

    所以我想出了一个解决方案。我使用了assert 声明。

    class ModifiedTestCase(TestCase):
        def WebElement_is_below(self, above_element):
            below = self.location['y']
            above = above_element.location['y']
    
            assert above < below, f'"{below}"  > "{above}"'
    
        WebElement.is_below = WebElement_is_below
    
    
    class SeleniumTest(ModifiedTestCase):
    
        def test_web_page(self):
    
            above_element = self.find_element()
            below_element = self.find_element()
    
            below_element.is_below(above_element)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-14
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      相关资源
      最近更新 更多