Skip to content

Commit f55a2f0

Browse files
author
Mykola Mokhnach
authored
Add clipboard handlers (#209)
* Add clipboard handlers * Fix documentation * fix options notation
1 parent 46d5728 commit f55a2f0

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
class ClipboardContentType(object):
17+
PLAINTEXT = 'plaintext'
18+
IMAGE = 'image'
19+
URL = 'url'

appium/webdriver/mobilecommand.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,5 @@ class MobileCommand(object):
6868
CLEAR = 'clear'
6969
START_RECORDING_SCREEN = 'startRecordingScreen'
7070
STOP_RECORDING_SCREEN = 'stopRecordingScreen'
71+
SET_CLIPBOARD = 'setClipboard'
72+
GET_CLIPBOARD = 'getClipboard'

appium/webdriver/webdriver.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .switch_to import MobileSwitchTo
2020
from .webelement import WebElement as MobileWebElement
2121

22+
from appium.webdriver.clipboard_content_type import ClipboardContentType
2223
from appium.webdriver.common.mobileby import MobileBy
2324
from appium.webdriver.common.touch_action import TouchAction
2425
from appium.webdriver.common.multi_action import MultiAction
@@ -28,6 +29,8 @@
2829
from selenium.common.exceptions import TimeoutException, InvalidArgumentException
2930

3031
from selenium.webdriver.remote.command import Command as RemoteCommand
32+
33+
import base64
3134
import copy
3235

3336
# From remote/webdriver.py
@@ -1064,6 +1067,9 @@ def start_recording_screen(self, **options):
10641067
- forcedRestart: Whether to ignore the result of previous capture and start a new recording
10651068
immediately (`True` value). By default (`False`) the endpoint will try to catch and return the result of
10661069
the previous capture if it's still available.
1070+
- bugReport: Makes the recorder to display an additional information on the video overlay,
1071+
such as a timestamp, that is helpful in videos captured to illustrate bugs.
1072+
This option is only supported since API level 27 (Android P).
10671073
10681074
iOS Specific:
10691075
- videoQuality: The video encoding quality: 'low', 'medium', 'high', 'photo'. Defaults to 'medium'.
@@ -1118,6 +1124,54 @@ def stop_recording_screen(self, **options):
11181124
del options['password']
11191125
return self.execute(Command.STOP_RECORDING_SCREEN, options)['value']
11201126

1127+
def set_clipboard(self, content, content_type=ClipboardContentType.PLAINTEXT, label=None):
1128+
"""
1129+
Set the content of the system clipboard
1130+
1131+
:param content: The content to be set as bytearray string
1132+
:param content_type: One of ClipboardContentType items. Only ClipboardContentType.PLAINTEXT
1133+
is supported on Android
1134+
:param label: Optional label argument, which only works for Android
1135+
"""
1136+
options = {
1137+
'content': base64.b64encode(content),
1138+
'contentType': content_type,
1139+
}
1140+
if label:
1141+
options['label'] = label
1142+
self.execute(Command.SET_CLIPBOARD, options)
1143+
1144+
def set_clipboard_text(self, text, label=None):
1145+
"""
1146+
Copies the given text to the system clipboard
1147+
1148+
:param text: The text to be set
1149+
:param label: Optional label argument, which only works for Android
1150+
"""
1151+
self.set_clipboard(bytes(text.encode('UTF-8')), ClipboardContentType.PLAINTEXT, label)
1152+
1153+
def get_clipboard(self, content_type=ClipboardContentType.PLAINTEXT):
1154+
"""
1155+
Receives the content of the system clipboard
1156+
1157+
:param content_type: One of ClipboardContentType items. Only ClipboardContentType.PLAINTEXT
1158+
is supported on Android
1159+
:return: Clipboard content as base64-encoded string or an empty string
1160+
if the clipboard is empty
1161+
"""
1162+
base64_str = self.execute(Command.GET_CLIPBOARD, {
1163+
'contentType': content_type
1164+
})['value']
1165+
return base64.b64decode(base64_str)
1166+
1167+
def get_clipboard_text(self):
1168+
"""
1169+
Receives the text of the system clipboard
1170+
1171+
:return: The actual clipboard text or an empty string if the clipboard is empty
1172+
"""
1173+
return self.get_clipboard(ClipboardContentType.PLAINTEXT).decode('UTF-8')
1174+
11211175
@property
11221176
def device_time(self):
11231177
"""Returns the date and time from the device
@@ -1230,3 +1284,7 @@ def _addCommands(self):
12301284
('POST', '/session/$sessionId/appium/start_recording_screen')
12311285
self.command_executor._commands[Command.STOP_RECORDING_SCREEN] = \
12321286
('POST', '/session/$sessionId/appium/stop_recording_screen')
1287+
self.command_executor._commands[Command.SET_CLIPBOARD] = \
1288+
('POST', '/session/$sessionId/appium/device/set_clipboard')
1289+
self.command_executor._commands[Command.GET_CLIPBOARD] = \
1290+
('POST', '/session/$sessionId/appium/device/get_clipboard')

0 commit comments

Comments
 (0)