pumapy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pumapy might be problematic. Click here for more details.
- puma/__init__.py +0 -0
- puma/apps/__init__.py +0 -0
- puma/apps/android/__init__.py +3 -0
- puma/apps/android/appium_actions.py +173 -0
- puma/apps/android/snapchat/__init__.py +0 -0
- puma/apps/android/snapchat/snapchat.py +120 -0
- puma/apps/android/telegram/__init__.py +0 -0
- puma/apps/android/telegram/telegram.py +274 -0
- puma/apps/android/whatsapp/__init__.py +0 -0
- puma/apps/android/whatsapp/whatsapp.py +632 -0
- puma/computer_vision/__init__.py +3 -0
- puma/computer_vision/ocr.py +77 -0
- puma/utils/__init__.py +38 -0
- puma/utils/video_utils.py +79 -0
- puma/version.py +1 -0
- pumapy-0.1.0.dist-info/LICENSE.txt +287 -0
- pumapy-0.1.0.dist-info/METADATA +230 -0
- pumapy-0.1.0.dist-info/RECORD +20 -0
- pumapy-0.1.0.dist-info/WHEEL +5 -0
- pumapy-0.1.0.dist-info/top_level.txt +1 -0
puma/__init__.py
ADDED
|
File without changes
|
puma/apps/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Dict
|
|
5
|
+
from uuid import uuid4
|
|
6
|
+
|
|
7
|
+
from adb_pywrapper.adb_device import AdbDevice
|
|
8
|
+
from adb_pywrapper.adb_screen_recorder import AdbScreenRecorder
|
|
9
|
+
from appium import webdriver
|
|
10
|
+
from appium.options.android import UiAutomator2Options
|
|
11
|
+
from appium.webdriver import WebElement
|
|
12
|
+
from appium.webdriver.common.appiumby import AppiumBy
|
|
13
|
+
from appium.webdriver.common.touch_action import TouchAction
|
|
14
|
+
from appium.webdriver.extensions.android.nativekey import AndroidKey
|
|
15
|
+
|
|
16
|
+
from puma.apps.android import logger
|
|
17
|
+
from puma.computer_vision import ocr
|
|
18
|
+
from puma.utils.video_utils import CACHE_FOLDER, log_error_and_raise_exception
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _get_android_default_options():
|
|
22
|
+
options = UiAutomator2Options()
|
|
23
|
+
options.no_reset = True
|
|
24
|
+
options.platform_name = 'Android'
|
|
25
|
+
options.new_command_timeout = 1200
|
|
26
|
+
return options
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def supported_version(version: str):
|
|
30
|
+
def decorator(class_or_function):
|
|
31
|
+
class_or_function.supported_version = version
|
|
32
|
+
return class_or_function
|
|
33
|
+
|
|
34
|
+
return decorator
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class AndroidAppiumActions:
|
|
38
|
+
def __init__(self,
|
|
39
|
+
udid: str,
|
|
40
|
+
app_package: str,
|
|
41
|
+
app_activity: str,
|
|
42
|
+
desired_capabilities: Dict[str, str] = None,
|
|
43
|
+
implicit_wait: int = 1,
|
|
44
|
+
appium_server: str = 'http://localhost:4723'):
|
|
45
|
+
"""
|
|
46
|
+
Class with a generic API for Appium scripting on an Android device.
|
|
47
|
+
Can be used with an emulator or real device attached to the computer.
|
|
48
|
+
:param desired_capabilities: desired capabilities as passed to the Appium webdriver
|
|
49
|
+
:param implicit_wait: how long Appium will look for an element (you can look for an element before it is
|
|
50
|
+
rendered). Default 1 second
|
|
51
|
+
:param appium_server: url of the appium server
|
|
52
|
+
"""
|
|
53
|
+
# prepare options
|
|
54
|
+
self.options = _get_android_default_options()
|
|
55
|
+
self.options.udid = udid
|
|
56
|
+
self.options.app_package = app_package
|
|
57
|
+
self.options.app_activity = app_activity
|
|
58
|
+
if desired_capabilities:
|
|
59
|
+
self.options.load_capabilities(desired_capabilities)
|
|
60
|
+
# connect to appium server
|
|
61
|
+
self.driver = webdriver.Remote(appium_server, options=self.options)
|
|
62
|
+
|
|
63
|
+
# the implicit wait time is how long appium looks for an element (you can try to find an element before it is rendered)
|
|
64
|
+
self.implicit_wait = implicit_wait
|
|
65
|
+
self.driver.implicitly_wait(implicit_wait)
|
|
66
|
+
self.udid = self.driver.capabilities.get("udid")
|
|
67
|
+
self.adb = AdbDevice(self.udid)
|
|
68
|
+
|
|
69
|
+
# screen recorder
|
|
70
|
+
self._screen_recorder = None
|
|
71
|
+
|
|
72
|
+
# start app
|
|
73
|
+
self.activate_app()
|
|
74
|
+
|
|
75
|
+
def activate_app(self):
|
|
76
|
+
self.driver.activate_app(self.options.app_package)
|
|
77
|
+
|
|
78
|
+
def __enter__(self):
|
|
79
|
+
return self
|
|
80
|
+
|
|
81
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
82
|
+
if self._screen_recorder is not None:
|
|
83
|
+
self._screen_recorder.__exit__(None, None, None)
|
|
84
|
+
self.driver.__exit__(exc_type, exc_val, exc_tb)
|
|
85
|
+
|
|
86
|
+
def back(self):
|
|
87
|
+
self.driver.press_keycode(AndroidKey.BACK)
|
|
88
|
+
|
|
89
|
+
def home(self):
|
|
90
|
+
self.driver.press_keycode(AndroidKey.HOME)
|
|
91
|
+
|
|
92
|
+
def open_notifications(self):
|
|
93
|
+
self.driver.open_notifications()
|
|
94
|
+
|
|
95
|
+
def scroll_to_find_element(self, resource_id: str = None, text_equals: str = None,
|
|
96
|
+
text_contains: str = None) -> WebElement:
|
|
97
|
+
"""
|
|
98
|
+
This code will scroll in the current view until a certain element is found, and then return that element.
|
|
99
|
+
The element can be searched for by resource-id and/or the text in that element.
|
|
100
|
+
When defining the text of the element either an exact match or a textContains match can be used. These two can
|
|
101
|
+
of course not be used at the same time.
|
|
102
|
+
The method will keep scrolling until the element is found. If you look for something that doesn't exist you
|
|
103
|
+
will have a bad time.
|
|
104
|
+
The first matching element is returned when found.
|
|
105
|
+
:param resource_id: the resource id of the element to look for.
|
|
106
|
+
:param text_equals: the exact text of the element to look for. Cannot be used in combination with text_contains.
|
|
107
|
+
:param text_contains: part of the text of the element to look for. Cannot be used in combination with
|
|
108
|
+
text_equals.
|
|
109
|
+
:return: The element when found.
|
|
110
|
+
"""
|
|
111
|
+
if text_equals is not None and text_contains is not None:
|
|
112
|
+
raise ValueError('text_equals and text_contains can not be used at the same time')
|
|
113
|
+
if resource_id is None and text_contains is None and text_equals is None:
|
|
114
|
+
raise ValueError('resource_id, text_equals and text_contains cannot all be None')
|
|
115
|
+
|
|
116
|
+
resource_id_part = '' if resource_id is None else f'.resourceIdMatches("{resource_id}")'
|
|
117
|
+
text_part = '' if text_equals is None else f'.text("{text_equals}")'
|
|
118
|
+
text_contains_part = '' if text_contains is None else f'.textContains("{text_contains}")'
|
|
119
|
+
|
|
120
|
+
java_code = f'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector(){resource_id_part}{text_part}{text_contains_part}.instance(0))'
|
|
121
|
+
return self.driver.find_element(by=AppiumBy.ANDROID_UIAUTOMATOR, value=java_code)
|
|
122
|
+
|
|
123
|
+
def is_present(self, xpath: str, implicit_wait: int = 0) -> bool:
|
|
124
|
+
self.driver.implicitly_wait(implicit_wait)
|
|
125
|
+
found = self.driver.find_elements(by=AppiumBy.XPATH, value=xpath)
|
|
126
|
+
self.driver.implicitly_wait(self.implicit_wait)
|
|
127
|
+
return len(found) > 0
|
|
128
|
+
|
|
129
|
+
def start_recording(self):
|
|
130
|
+
if self._screen_recorder is None:
|
|
131
|
+
self._screen_recorder = AdbScreenRecorder(self.adb)
|
|
132
|
+
self._screen_recorder.start_recording()
|
|
133
|
+
|
|
134
|
+
def stop_recording_and_save_video(self, output_directory: str) -> [str]:
|
|
135
|
+
if self._screen_recorder is None:
|
|
136
|
+
return None
|
|
137
|
+
video_files = self._screen_recorder.stop_recording(output_directory)
|
|
138
|
+
self._screen_recorder.__exit__(None, None, None)
|
|
139
|
+
self._screen_recorder = None
|
|
140
|
+
return video_files
|
|
141
|
+
|
|
142
|
+
def new_screenshot_name(self):
|
|
143
|
+
now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
|
144
|
+
device_name = self.options.device_name
|
|
145
|
+
return Path(CACHE_FOLDER) / f'{now}-{device_name}-{uuid4()}.png'
|
|
146
|
+
|
|
147
|
+
def find_text_ocr(self, text_to_find: str) -> [ocr.RecognizedText]:
|
|
148
|
+
path = self.new_screenshot_name()
|
|
149
|
+
screenshot_taken = False
|
|
150
|
+
try:
|
|
151
|
+
screenshot_taken = self.driver.get_screenshot_as_file(path)
|
|
152
|
+
if not screenshot_taken:
|
|
153
|
+
raise Exception(f'Screenshot could not be stored to {path}')
|
|
154
|
+
found_text = ocr.find_text(str(path), text_to_find)
|
|
155
|
+
return found_text
|
|
156
|
+
finally:
|
|
157
|
+
if screenshot_taken:
|
|
158
|
+
os.remove(path)
|
|
159
|
+
|
|
160
|
+
def click_text_ocr(self, text_to_click: str, click_first_when_multiple: bool = False):
|
|
161
|
+
found_text = self.find_text_ocr(text_to_click)
|
|
162
|
+
if len(found_text) == 0:
|
|
163
|
+
msg = f'Could not find text {text_to_click} on screen so could not click it'
|
|
164
|
+
log_error_and_raise_exception(logger, msg)
|
|
165
|
+
if len(found_text) > 1:
|
|
166
|
+
msg = f'Found multiple occurrences of text {text_to_click} on screen so could not determine what to click'
|
|
167
|
+
if not click_first_when_multiple:
|
|
168
|
+
log_error_and_raise_exception(logger, msg)
|
|
169
|
+
else:
|
|
170
|
+
logger.warning(f'Found multiple occurrences of text {text_to_click} on screen, clicking first one')
|
|
171
|
+
TouchAction(self.driver) \
|
|
172
|
+
.tap(None, found_text[0].bounding_box.middle[0], found_text[0].bounding_box.middle[1], 1) \
|
|
173
|
+
.perform()
|
|
File without changes
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
from time import sleep
|
|
2
|
+
from typing import Dict
|
|
3
|
+
|
|
4
|
+
from appium.webdriver.common.appiumby import AppiumBy
|
|
5
|
+
from puma.apps.android.appium_actions import AndroidAppiumActions, supported_version
|
|
6
|
+
|
|
7
|
+
SNAPCHAT_PACKAGE = 'com.snapchat.android'
|
|
8
|
+
SNAPCHAT_DEFAULT_ACTIVITY = '.LandingPageActivity'
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@supported_version("12.90.0.46")
|
|
12
|
+
class SnapchatActions(AndroidAppiumActions):
|
|
13
|
+
def __init__(self,
|
|
14
|
+
device_udid,
|
|
15
|
+
desired_capabilities: Dict[str, str] = None,
|
|
16
|
+
implicit_wait=1,
|
|
17
|
+
appium_server='http://localhost:4723'):
|
|
18
|
+
"""
|
|
19
|
+
Class with an API for Snapchat Android using Appium. Can be used with an emulator or real device attached to the computer.
|
|
20
|
+
"""
|
|
21
|
+
AndroidAppiumActions.__init__(self,
|
|
22
|
+
device_udid,
|
|
23
|
+
SNAPCHAT_PACKAGE,
|
|
24
|
+
SNAPCHAT_DEFAULT_ACTIVITY,
|
|
25
|
+
desired_capabilities=desired_capabilities,
|
|
26
|
+
implicit_wait=implicit_wait,
|
|
27
|
+
appium_server=appium_server)
|
|
28
|
+
|
|
29
|
+
def _currently_at_homescreen(self) -> bool:
|
|
30
|
+
return self.is_present(
|
|
31
|
+
'//android.widget.LinearLayout[@resource-id="com.snapchat.android:id/ngs_navigation_bar"]') \
|
|
32
|
+
and not self.is_present('//*[@text="View Profile"]')
|
|
33
|
+
|
|
34
|
+
def _currently_in_conversation_overview(self) -> bool:
|
|
35
|
+
return self.is_present(
|
|
36
|
+
'//android.widget.TextView[@resource-id="com.snapchat.android:id/hova_page_title" and @text="Chat"]')
|
|
37
|
+
|
|
38
|
+
def _currently_in_conversation(self) -> bool:
|
|
39
|
+
return self.is_present(
|
|
40
|
+
'//android.widget.EditText[@resource-id="com.snapchat.android:id/chat_input_text_field"]')
|
|
41
|
+
|
|
42
|
+
def go_to_main_tab(self, tab_name: str):
|
|
43
|
+
"""
|
|
44
|
+
One fo the main tabs of snapchat: Map, chat, Camera, Stories, or Spotlight
|
|
45
|
+
"""
|
|
46
|
+
if self.driver.current_package != SNAPCHAT_PACKAGE:
|
|
47
|
+
self.driver.activate_app(SNAPCHAT_PACKAGE)
|
|
48
|
+
while not self._currently_at_homescreen():
|
|
49
|
+
self.driver.back()
|
|
50
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
51
|
+
value=f'//android.widget.LinearLayout[@resource-id="com.snapchat.android:id/ngs_navigation_bar"]/android.view.ViewGroup[@content-desc="{tab_name}"]').click()
|
|
52
|
+
|
|
53
|
+
def select_chat(self, chat_subject: str):
|
|
54
|
+
"""
|
|
55
|
+
Opens a given conversation.
|
|
56
|
+
:param chat_subject: the name of the conversation to open
|
|
57
|
+
"""
|
|
58
|
+
self.go_to_main_tab('Chat')
|
|
59
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
60
|
+
value=f'//javaClass[contains(lower-case(@text), "{chat_subject.lower()}")]/..').click()
|
|
61
|
+
|
|
62
|
+
def _if_chat_go_to_chat(self, chat: str):
|
|
63
|
+
if chat is not None:
|
|
64
|
+
self.go_to_main_tab('Chat')
|
|
65
|
+
self.select_chat(chat)
|
|
66
|
+
if not self._currently_in_conversation():
|
|
67
|
+
raise Exception('Expected to be in conversation screen now, but screen contents are unknown')
|
|
68
|
+
|
|
69
|
+
def send_message(self, message: str, chat: str = None):
|
|
70
|
+
"""
|
|
71
|
+
Sends a text message, either in the current conversation, or in a given conversation.
|
|
72
|
+
:param message: the message to send
|
|
73
|
+
:param chat: optional: the conversation in which to send the message. If not used, it is assumed the
|
|
74
|
+
conversation is already opened.
|
|
75
|
+
"""
|
|
76
|
+
self._if_chat_go_to_chat(chat)
|
|
77
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
78
|
+
value='//android.widget.EditText[@resource-id="com.snapchat.android:id/chat_input_text_field"]') \
|
|
79
|
+
.send_keys(message)
|
|
80
|
+
self._press_enter()
|
|
81
|
+
|
|
82
|
+
def _press_enter(self):
|
|
83
|
+
enter_keycode = 66
|
|
84
|
+
self.driver.press_keycode(enter_keycode)
|
|
85
|
+
|
|
86
|
+
def send_snap(self, recipients: [str] = None, caption: str = None, front_camera: bool = True):
|
|
87
|
+
"""
|
|
88
|
+
Sends a snap (picture), either to one or more contacts, or posts it to `My story`
|
|
89
|
+
:param recipients: Optional: a list of recipients to send the snap to
|
|
90
|
+
:param caption: Optional: a caption to set on the snap
|
|
91
|
+
:param front_camera: Optional: whether or not to use the front camera (True by default)
|
|
92
|
+
"""
|
|
93
|
+
# go to camera and snap picture
|
|
94
|
+
self.go_to_main_tab('Camera')
|
|
95
|
+
if not front_camera:
|
|
96
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
97
|
+
value='//android.view.ViewGroup[@content-desc="Flip Camera"]').click()
|
|
98
|
+
sleep(0.5)
|
|
99
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
100
|
+
value='//android.widget.FrameLayout[@content-desc="Camera Capture"]').click()
|
|
101
|
+
# write caption if needed
|
|
102
|
+
if caption:
|
|
103
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
104
|
+
value='//android.view.View[@resource-id="com.snapchat.android:id/full_screen_surface_view"]').click()
|
|
105
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
106
|
+
value='//android.widget.EditText[@resource-id="com.snapchat.android:id/caption_edit_text_view"]').send_keys(
|
|
107
|
+
caption)
|
|
108
|
+
self.driver.back()
|
|
109
|
+
# press send
|
|
110
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.widget.ImageButton[lower-case(@content-desc)="send"]').click()
|
|
111
|
+
|
|
112
|
+
# select recipients, or post as story, and send
|
|
113
|
+
if recipients:
|
|
114
|
+
for recipient in recipients:
|
|
115
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
116
|
+
value=f'//javaClass[lower-case(@text) ="{recipient.lower()}"]').click()
|
|
117
|
+
else:
|
|
118
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
119
|
+
value='//javaClass[lower-case(@text)="my story · friends only"]/..').click()
|
|
120
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.view.View[lower-case(@content-desc)="send"]').click()
|
|
File without changes
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
from time import sleep
|
|
2
|
+
from typing import Dict
|
|
3
|
+
|
|
4
|
+
from appium.webdriver.common.appiumby import AppiumBy
|
|
5
|
+
|
|
6
|
+
from puma.apps.android.appium_actions import supported_version, AndroidAppiumActions
|
|
7
|
+
|
|
8
|
+
TELEGRAM_PACKAGE = 'org.telegram.messenger'
|
|
9
|
+
TELEGRAM_DEFAULT_ACTIVITY = '.DefaultIcon'
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@supported_version("10.13.4")
|
|
13
|
+
class TelegramActions(AndroidAppiumActions):
|
|
14
|
+
|
|
15
|
+
def __init__(self,
|
|
16
|
+
device_udid,
|
|
17
|
+
desired_capabilities: Dict[str, str] = None,
|
|
18
|
+
implicit_wait=1,
|
|
19
|
+
appium_server='http://localhost:4723'):
|
|
20
|
+
"""
|
|
21
|
+
Class with an API for Telegram Android using Appium. Can be used with an emulator or real device attached to the computer.
|
|
22
|
+
"""
|
|
23
|
+
AndroidAppiumActions.__init__(self,
|
|
24
|
+
device_udid,
|
|
25
|
+
TELEGRAM_PACKAGE,
|
|
26
|
+
TELEGRAM_DEFAULT_ACTIVITY,
|
|
27
|
+
desired_capabilities=desired_capabilities,
|
|
28
|
+
implicit_wait=implicit_wait,
|
|
29
|
+
appium_server=appium_server)
|
|
30
|
+
|
|
31
|
+
def _currently_at_homescreen(self) -> bool:
|
|
32
|
+
return self.is_present('//android.widget.FrameLayout[@content-desc="New Message"]')
|
|
33
|
+
|
|
34
|
+
def _currently_in_conversation(self) -> bool:
|
|
35
|
+
return self.is_present('//android.widget.ImageView[@content-desc="Emoji, stickers, and GIFs"]')
|
|
36
|
+
|
|
37
|
+
def _currently_in_camera(self) -> bool:
|
|
38
|
+
return self.is_present('//android.widget.Button[lower-case(@content-desc)="shutter"]')
|
|
39
|
+
|
|
40
|
+
def _currently_in_call(self) -> bool:
|
|
41
|
+
return self.is_present('//android.widget.Button[@text="End Call"]')
|
|
42
|
+
|
|
43
|
+
def _load_conversation_titles(self):
|
|
44
|
+
while True:
|
|
45
|
+
xpath = "//androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[not(@content-desc)]"
|
|
46
|
+
convos = self.driver.find_elements(by=AppiumBy.XPATH, value=xpath)
|
|
47
|
+
if len(convos) == 0:
|
|
48
|
+
break
|
|
49
|
+
print('some conversations still do not have a title loaded, clicking...')
|
|
50
|
+
convos[0].click()
|
|
51
|
+
self.driver.back()
|
|
52
|
+
print('all conversations loaded!')
|
|
53
|
+
|
|
54
|
+
def return_to_homescreen(self):
|
|
55
|
+
"""
|
|
56
|
+
Returns to the start screen of Telegram
|
|
57
|
+
"""
|
|
58
|
+
if self.driver.current_package != TELEGRAM_PACKAGE:
|
|
59
|
+
self.driver.activate_app(TELEGRAM_PACKAGE)
|
|
60
|
+
while not self._currently_at_homescreen():
|
|
61
|
+
self.driver.back()
|
|
62
|
+
self._load_conversation_titles()
|
|
63
|
+
sleep(1)
|
|
64
|
+
|
|
65
|
+
def start_new_chat(self, chat: str):
|
|
66
|
+
self.return_to_homescreen()
|
|
67
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
68
|
+
value='//android.widget.FrameLayout[@content-desc="New Message"]').click()
|
|
69
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
70
|
+
value='//android.widget.ImageButton[@content-desc="Search"]').click()
|
|
71
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
72
|
+
value='//android.widget.EditText[@text="Search"]').send_keys(chat)
|
|
73
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
74
|
+
value=f'//android.view.ViewGroup[starts-with(lower-case(@text), "{chat.lower()}")]').click()
|
|
75
|
+
|
|
76
|
+
def select_chat(self, chat: str):
|
|
77
|
+
"""
|
|
78
|
+
Opens a given conversation based on the (partial) name of a chat.
|
|
79
|
+
For groups or channels, it is advised to use :meth:`TelegramActions.select_group` or
|
|
80
|
+
:meth:`TelegramActions.select_channel`, as the matching is more explicit
|
|
81
|
+
:param chat: (part of) the conversation name to open
|
|
82
|
+
"""
|
|
83
|
+
self.return_to_homescreen()
|
|
84
|
+
xpath = f'//android.view.ViewGroup[starts-with(lower-case(@content-desc), "{chat.lower()}")]'
|
|
85
|
+
self.driver.find_element(by=AppiumBy.XPATH, value=xpath).click()
|
|
86
|
+
|
|
87
|
+
def select_group(self, group_name: str):
|
|
88
|
+
"""
|
|
89
|
+
Opens a given conversation based on the exact name of a group.
|
|
90
|
+
:param group_name: Exact group name to open
|
|
91
|
+
"""
|
|
92
|
+
self.select_chat(f'Group. {group_name}')
|
|
93
|
+
|
|
94
|
+
def select_channel(self, channel_name: str):
|
|
95
|
+
"""
|
|
96
|
+
Opens a given conversation based on the exact name of a channel.
|
|
97
|
+
:param channel_name: Exact channel name to open
|
|
98
|
+
"""
|
|
99
|
+
self.select_chat(f'Channel. {channel_name}')
|
|
100
|
+
|
|
101
|
+
def send_message(self, message: str, chat: str = None):
|
|
102
|
+
"""
|
|
103
|
+
Send a message in the current or given chat
|
|
104
|
+
:param message: The text message to send
|
|
105
|
+
:param chat: Optional: The chat conversation in which to send this message, if not currently in the desired chat
|
|
106
|
+
"""
|
|
107
|
+
self._if_chat_go_to_chat(chat)
|
|
108
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.widget.EditText[@text="Message"]').send_keys(
|
|
109
|
+
message)
|
|
110
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.view.View[@content-desc="Send"]').click()
|
|
111
|
+
|
|
112
|
+
def reply_to_message(self, message_to_reply_to: str, reply: str, chat: str = None):
|
|
113
|
+
"""
|
|
114
|
+
Send a text message as a reply to a previous message.
|
|
115
|
+
This method will scroll up until the given message text is found, select that message, and reply to it
|
|
116
|
+
:param message_to_reply_to: the message to reply to
|
|
117
|
+
:param reply: The text message that will be sent as a reply
|
|
118
|
+
:param chat: Optional: The chat conversation in which to send this message, if not currently in the desired chat
|
|
119
|
+
"""
|
|
120
|
+
try:
|
|
121
|
+
self._tap_message(message_to_reply_to, chat)
|
|
122
|
+
except:
|
|
123
|
+
raise Exception(f'Failed to scroll to message "{message_to_reply_to}", does it exist?')
|
|
124
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.widget.TextView[@text="Reply"]').click()
|
|
125
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.widget.EditText').send_keys(reply)
|
|
126
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.view.View[@content-desc="Send"]').click()
|
|
127
|
+
|
|
128
|
+
def emoji_reply_to_message(self, message_to_respond_to: str, emoji_to_respond_with: str = '👍', chat: str = None):
|
|
129
|
+
"""
|
|
130
|
+
Send emoji response to a previous message.
|
|
131
|
+
This method will scroll up until the given message text is found, select that message, and reply to it
|
|
132
|
+
:param message_to_reply_to: the message to reply to
|
|
133
|
+
:param emoji_to_respond_with: the emoji with which to respond. Optional, by default '👍'
|
|
134
|
+
:param chat: Optional: The chat conversation in which to send this message, if not currently in the desired chat
|
|
135
|
+
"""
|
|
136
|
+
self._tap_message(message_to_respond_to, chat)
|
|
137
|
+
if self.is_present(f'//android.widget.FrameLayout[@text="{emoji_to_respond_with}"]', implicit_wait=2):
|
|
138
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
139
|
+
value=f'//android.widget.FrameLayout[@text="{emoji_to_respond_with}"]').click()
|
|
140
|
+
else:
|
|
141
|
+
print(f'ERROR: emoji {emoji_to_respond_with} does not exist on screen!')
|
|
142
|
+
self.driver.back()
|
|
143
|
+
|
|
144
|
+
def _tap_message(self, message_to_tap, chat):
|
|
145
|
+
self._if_chat_go_to_chat(chat)
|
|
146
|
+
message = self.scroll_to_find_element(text_contains=message_to_tap)
|
|
147
|
+
# we want to click the middle right part of the message to open the menu
|
|
148
|
+
x = message.location['x'] + message.size['width'] - 50
|
|
149
|
+
y = message.location['y'] + message.size['height'] / 2
|
|
150
|
+
print(f'Tapping screen at ({x},{y})')
|
|
151
|
+
self.driver.tap([(x, y)])
|
|
152
|
+
|
|
153
|
+
def take_and_send_picture(self,
|
|
154
|
+
chat: str = None,
|
|
155
|
+
caption: str = None,
|
|
156
|
+
wait_time: int = 1,
|
|
157
|
+
front_camera: bool = False):
|
|
158
|
+
"""
|
|
159
|
+
Opens the embedded camera in the Telegram app, takes a picture, and sends it.
|
|
160
|
+
:param chat: Optional: the conversation in which to send the picture. If not used, we assume a conversation is opened
|
|
161
|
+
:param caption: Optional: a cpation to include with the picture
|
|
162
|
+
:param wait_time: Optional: time to wait (in seconds) after opening the camera before taking a picture. Default 1s
|
|
163
|
+
:param front_camera: Optional: whether or not to use the front camera. Default False
|
|
164
|
+
"""
|
|
165
|
+
self._if_chat_go_to_chat(chat)
|
|
166
|
+
# click the attachment icon
|
|
167
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
168
|
+
value='//android.widget.ImageView[lower-case(@content-desc)="attach media"]').click()
|
|
169
|
+
# click the camera: this might be unstable due to bad xpath expression
|
|
170
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
171
|
+
value='//android.widget.FrameLayout[@resource-id="android:id/content"]/android.widget.FrameLayout/android.widget.FrameLayout[2]').click()
|
|
172
|
+
# switch camera if need be
|
|
173
|
+
if front_camera:
|
|
174
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
175
|
+
value='//android.widget.ImageView[lower-case(@content-desc)="switch camera"]').click()
|
|
176
|
+
sleep(wait_time)
|
|
177
|
+
# take picture
|
|
178
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
179
|
+
value='//android.widget.Button[lower-case(@content-desc)="shutter"]').click()
|
|
180
|
+
sleep(1)
|
|
181
|
+
# add caption
|
|
182
|
+
if caption is not None:
|
|
183
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.widget.EditText').click()
|
|
184
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.widget.EditText').send_keys(caption)
|
|
185
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
186
|
+
value='//android.widget.EditText/../../android.widget.ImageView').click()
|
|
187
|
+
# press send
|
|
188
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
189
|
+
value='//android.widget.ImageView[lower-case(@content-desc)="send"]').click()
|
|
190
|
+
sleep(0.3) # the animation after sending a picture might throw off the script
|
|
191
|
+
|
|
192
|
+
def start_call(self, chat: str = None, video: bool = False):
|
|
193
|
+
"""
|
|
194
|
+
Makes a call and ends the call after a given number of seconds.
|
|
195
|
+
The call is made to either
|
|
196
|
+
* the current conversation we're in
|
|
197
|
+
* a given contact name
|
|
198
|
+
:param chat: Optional: name of the conversation to start a call in
|
|
199
|
+
:param video: False (default) for voice call, True for video call.
|
|
200
|
+
"""
|
|
201
|
+
self._if_chat_go_to_chat(chat)
|
|
202
|
+
if not video:
|
|
203
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
204
|
+
value='//android.widget.ImageButton[lower-case(@content-desc)="call"]').click()
|
|
205
|
+
else:
|
|
206
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
207
|
+
value='//android.widget.ImageButton[lower-case(@content-desc)="more options"]').click()
|
|
208
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
209
|
+
value='//android.widget.TextView[lower-case(@text)="video call"]').click()
|
|
210
|
+
|
|
211
|
+
def end_call(self):
|
|
212
|
+
"""
|
|
213
|
+
Ends the current call. Assumes the call screen is open.
|
|
214
|
+
"""
|
|
215
|
+
if not self._currently_in_call():
|
|
216
|
+
raise Exception('Expected to be in a call, but could not detect call screen!')
|
|
217
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
218
|
+
value='//android.widget.Button[lower-case(@text)="end call"]').click()
|
|
219
|
+
|
|
220
|
+
def answer_call(self):
|
|
221
|
+
"""
|
|
222
|
+
Answer when receiving a call via Telegram.
|
|
223
|
+
"""
|
|
224
|
+
self.driver.open_notifications()
|
|
225
|
+
if not self.is_present('//android.widget.Button[lower-case(@content-desc)="answer"]', implicit_wait=3):
|
|
226
|
+
self.driver.back()
|
|
227
|
+
raise Exception('Tried to answer call, but couldn\'t find notification to answer any calls')
|
|
228
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
229
|
+
value='//android.widget.Button[lower-case(@content-desc)="answer"]').click()
|
|
230
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
231
|
+
value='//android.widget.TextView[@resource-id="android:id/title" and @text="Ongoing Telegram call"]').click()
|
|
232
|
+
|
|
233
|
+
def decline_call(self):
|
|
234
|
+
"""
|
|
235
|
+
Declines an incoming Telegram call.
|
|
236
|
+
"""
|
|
237
|
+
self.driver.open_notifications()
|
|
238
|
+
if not self.is_present('//android.widget.Button[lower-case(@content-desc)="decline"]', implicit_wait=3):
|
|
239
|
+
self.driver.back()
|
|
240
|
+
raise Exception('Tried to answer call, but couldn\'t find notification to answer any calls')
|
|
241
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
242
|
+
value='//android.widget.Button[lower-case(@content-desc)="decline"]').click()
|
|
243
|
+
self.driver.back()
|
|
244
|
+
|
|
245
|
+
def toggle_video_in_call(self):
|
|
246
|
+
"""
|
|
247
|
+
Toggles video in an ongoing call.
|
|
248
|
+
"""
|
|
249
|
+
if not self._currently_in_call():
|
|
250
|
+
raise Exception('Expected to be in a call, but could not detect call screen!')
|
|
251
|
+
if self.is_present('//android.widget.TextView[lower-case(@text)="start video"]'):
|
|
252
|
+
# turn on video:
|
|
253
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
254
|
+
value='//android.widget.FrameLayout[@content-desc="Start Video"]').click()
|
|
255
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
256
|
+
value='//android.widget.TextView[@content-desc="Share Video"]').click()
|
|
257
|
+
else:
|
|
258
|
+
self.driver.find_element(by=AppiumBy.XPATH,
|
|
259
|
+
value='//android.widget.FrameLayout[@content-desc="Stop Video"]').click()
|
|
260
|
+
|
|
261
|
+
def flip_video_in_call(self):
|
|
262
|
+
"""
|
|
263
|
+
Switches between the front camera and rear camera while in a video call.
|
|
264
|
+
"""
|
|
265
|
+
if not self._currently_in_call():
|
|
266
|
+
raise Exception('Expected to be in a call, but could not detect call screen!')
|
|
267
|
+
self.driver.find_element(by=AppiumBy.XPATH, value='//android.widget.FrameLayout[@content-desc="Flip"]').click()
|
|
268
|
+
|
|
269
|
+
def _if_chat_go_to_chat(self, chat: str):
|
|
270
|
+
if chat is not None:
|
|
271
|
+
self.return_to_homescreen()
|
|
272
|
+
self.select_chat(chat)
|
|
273
|
+
if not self._currently_in_conversation():
|
|
274
|
+
raise Exception('Expected to be in conversation screen now, but screen contents are unknown')
|
|
File without changes
|