|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from appium.webdriver import WebElement |
1 | 4 | from appium.webdriver.common.appiumby import AppiumBy |
2 | 5 |
|
3 | 6 | from puma.apps.android.teleguard.xpaths import * |
4 | 7 | from puma.state_graph.action import action |
5 | | -from puma.state_graph.puma_driver import PumaDriver, supported_version |
| 8 | +from puma.state_graph.puma_driver import PumaDriver, supported_version, PumaClickException |
6 | 9 | from puma.state_graph.state import ContextualState, SimpleState, compose_clicks |
7 | 10 | from puma.state_graph.state_graph import StateGraph |
8 | 11 |
|
@@ -111,14 +114,61 @@ def add_contact(self, teleguard_id: str): |
111 | 114 | self.driver.click(CONVERSATION_STATE_INVITE) |
112 | 115 |
|
113 | 116 | @action(conversations_state) |
114 | | - def accept_invite(self): |
| 117 | + def invite_received(self, scroll: bool = False, max_swipes: int = 10) -> bool: |
| 118 | + """ |
| 119 | + Checks whether an invite has been received. By default this method does not scroll to look for an invite. |
115 | 120 | """ |
116 | | - Accepts an invite from another user. |
| 121 | + if scroll: |
| 122 | + try: |
| 123 | + self.driver.swipe_to_find_element(CONVERSATION_STATE_YOU_HAVE_BEEN_INVITED, max_swipes=max_swipes) |
| 124 | + return True |
| 125 | + except PumaClickException: |
| 126 | + return False |
| 127 | + return self.driver.is_present(CONVERSATION_STATE_YOU_HAVE_BEEN_INVITED) |
| 128 | + |
| 129 | + @action(conversations_state) |
| 130 | + def accept_invite(self) -> str: |
| 131 | + """ |
| 132 | + Accepts an invite from another user and returns the name of the accepted invite. |
117 | 133 |
|
118 | 134 | If there are multiple invites, only the topmost invite in the UI will be accepted. |
| 135 | +
|
| 136 | + This method returns the name of the user that sent an invite. If no invite was sent, an exception is raised. |
119 | 137 | """ |
120 | 138 | self.driver.swipe_to_click_element(CONVERSATION_STATE_YOU_HAVE_BEEN_INVITED) |
121 | | - self.driver.click(CONVERSATION_STATE_ACCEPT_INVITE) |
| 139 | + name = self.driver.get_element(CONVERSATION_STATE_INVITATION_NAME).get_attribute('content-desc') |
| 140 | + self.driver.click(CONVERSATION_STATE_INVITATION_ACCEPT) |
| 141 | + return name |
| 142 | + |
| 143 | + def _extract_name(self, conversation_row: WebElement) -> str: |
| 144 | + """ |
| 145 | + Given a conversation row, extracts the name of the conversation. |
| 146 | + For users with an avatar, the first line of the content description is the conversation name. |
| 147 | + For users without an avatar, there is an extra first line with just the first letter of the name. |
| 148 | + We try to detect this pattern and return the first or second line based on this. |
| 149 | + """ |
| 150 | + content_description = conversation_row.get_attribute('content-desc') |
| 151 | + lines = content_description.split('\n')[:2] |
| 152 | + if len(lines[0])>1: |
| 153 | + return lines[0] |
| 154 | + elif lines[1][0] == lines[0]: |
| 155 | + return lines[1] |
| 156 | + else: |
| 157 | + return lines[0] |
| 158 | + |
| 159 | + @action(conversations_state) |
| 160 | + def conversations_with_unread_messages(self) -> List[str]: |
| 161 | + """ |
| 162 | + Returns a list of names of conversations that have unread messages. |
| 163 | + """ |
| 164 | + try: |
| 165 | + elements = self.driver.get_elements(CONVERSATION_STATE_UNREAD_MESSAGES) |
| 166 | + names = [self._extract_name(e) for e in elements] |
| 167 | + return names |
| 168 | + except PumaClickException: |
| 169 | + return [] |
| 170 | + |
| 171 | + |
122 | 172 |
|
123 | 173 | @action(chat_state) |
124 | 174 | def send_message(self, message: str, conversation: str = None): |
|
0 commit comments