Last active
September 29, 2020 21:28
-
-
Save boatcoder/95bbd5e55c353b86f5aa61a3cf2fdd8a to your computer and use it in GitHub Desktop.
A BasePageElement with a useful constructor and 2 derived classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BasePageElement(object): | |
"""Base page class that is initialized on every page object class.""" | |
def __init__(self, locator:Tuple[str, str]): | |
self.locator = locator | |
def __set__(self, instance, value:str): | |
""" returns the element so that derived classes can set things based upon the type of element they are dealing with """ | |
driver = instance.driver | |
WebDriverWait(driver, 10).until( | |
lambda driver: driver.find_element(*self.locator)) | |
return driver.find_element(*self.locator) | |
def __get__(self, instance, owner) -> RemoteWebElement: | |
""" Gets the element referenced by the locator """ | |
driver = instance.driver | |
WebDriverWait(driver, 10).until( | |
lambda driver: driver.find_element(*self.locator)) | |
return driver.find_element(*self.locator) | |
class PageElement(BasePageElement): | |
""" Useful for buttons and divs and other things where you don't change values """ | |
pass | |
class InputPageElement(BasePageElement): | |
""" This type of Element only really works for input elements """ | |
def __set__(self, instance, value:str): | |
""" Locate the element, clear the field and send the string as keypresses """ | |
element = super().__set__(instance, value) | |
element.clear() | |
element.send_keys(value) | |
return value | |
def __get__(self, obj, owner): | |
""" Get the value from the input element """ | |
element = super().__get__(obj, owner) | |
return element.get_attribute("value") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment