Python Selenium Webdriver add method to Webelements/ custom 'assert' statement

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python Selenium Webdriver add method to Webelements/ custom 'assert' statement



I’m trying to add a few methods to WebElements that I use frequently. I figured out how to make it work, but now my assert statements fail. Here is what I have. How do I get my assert to work?


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



In reality, this is all the logic I am trying to figure out:


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)



The error I get is “WebElement has no attribute assertIn”. I know I can pass it a driver argument, but that defeats some of the simplicity.


driver




1 Answer
1



So I figured out a solution. I used an assert statement.


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)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard