Skip to content

Commit

Permalink
[app] updated elements.py and added the OpenedChats nultielement to i…
Browse files Browse the repository at this point in the history
…nteract with all opened chats available in whatsapp.
  • Loading branch information
Fantaso committed Jul 7, 2020
1 parent 61ede4f commit 8410830
Showing 1 changed file with 96 additions and 4 deletions.
100 changes: 96 additions & 4 deletions simon/elements.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,101 @@
import time

from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.support.ui import WebDriverWait

from .locators import PaneLocators


class ReadOnlyBaseElement(object):
def __init__(self, element, locators):
self.locators = locators
self.element = element

def __get__(self, instance, owner):
return self.element

def __set__(self, instance, value):
raise Exception("Can not set a value to a read-only element")

def __repr__(self):
return f"<ReadOnlyBaseElement: {self.element}>"


class ReadOnlyBaseElements(object):
def __get__(self, instance, owner):
"""Gets the current state of the element."""
driver = instance.driver
WebDriverWait(driver, 100).until(
lambda driver: driver.find_elements(*self.parent_locator))
elements = driver.find_elements(*self.parent_locator)
return [self.child_class(ele, self.child_locators) for ele in elements]

def __set__(self, instance, values):
raise Exception("Can not set a value to a read-only elements")


def element_not_found(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except NoSuchElementException:
return ""
except StaleElementReferenceException:
return ""

return wrapper


class Chat(ReadOnlyBaseElement):
@property
@element_not_found
def name(self):
return self.element.find_element(*self.locators['name']).text

@property
@element_not_found
def last_message_time(self):
return self.element.find_element(*self.locators['last_message_time']).text

@property
@element_not_found
def last_message(self):
return self.element.find_element(*self.locators['last_message']).text

@property
@element_not_found
def icon(self):
return self.element.find_element(*self.locators['icon']).get_attribute('src')

@property
@element_not_found
def notifications(self):
return int(self.element.find_element(*self.locators['notifications']).text)

@element_not_found
def has_notifications(self):
notifications = int(self.element.find_element(*self.locators['notifications']).text)
if notifications > 0:
return True

@element_not_found
def click(self):
self.element.click()
time.sleep(3)


class OpenedChats(ReadOnlyBaseElements):
parent_locator = PaneLocators.OPENED_CHATS
child_class = Chat
child_locators = {
'name': PaneLocators.NAME,
'last_message_time': PaneLocators.LAST_MESSAGE_TIME,
'last_message': PaneLocators.LAST_MESSAGE,
'icon': PaneLocators.ICON,
'notifications': PaneLocators.NOTIFICATION,
}



class CheckBoxException(Exception):
pass
Expand Down Expand Up @@ -38,7 +134,3 @@ def _set_checkbox(checkbox, value):
checkbox.click()
if value is False and checkbox.is_selected():
checkbox.click()


class LoginRememberMeCheckBox(RememberMeCheckBox):
locator = "rememberMe"

0 comments on commit 8410830

Please sign in to comment.