15
15
import base64
16
16
from typing import TYPE_CHECKING , Optional , cast
17
17
18
+ from selenium .common .exceptions import UnknownMethodException
19
+
18
20
from appium .protocols .webdriver .can_execute_commands import CanExecuteCommands
21
+ from appium .protocols .webdriver .can_execute_scripts import CanExecuteScripts
22
+ from appium .protocols .webdriver .can_remember_extension_presence import CanRememberExtensionPresence
19
23
from appium .webdriver .clipboard_content_type import ClipboardContentType
20
24
21
25
from ..mobilecommand import MobileCommand as Command
24
28
from appium .webdriver .webdriver import WebDriver
25
29
26
30
27
- class Clipboard (CanExecuteCommands ):
31
+ class Clipboard (CanExecuteCommands , CanRememberExtensionPresence , CanExecuteScripts ):
28
32
def set_clipboard (
29
33
self , content : bytes , content_type : str = ClipboardContentType .PLAINTEXT , label : Optional [str ] = None
30
34
) -> 'WebDriver' :
@@ -39,13 +43,18 @@ def set_clipboard(
39
43
Returns:
40
44
Union['WebDriver', 'Clipboard']: Self instance
41
45
"""
46
+ ext_name = 'mobile: setClipboard'
42
47
options = {
43
48
'content' : base64 .b64encode (content ).decode ('UTF-8' ),
44
49
'contentType' : content_type ,
45
50
}
46
51
if label :
47
52
options ['label' ] = label
48
- self .execute (Command .SET_CLIPBOARD , options )
53
+ try :
54
+ self .assert_extension_exists (ext_name ).execute_script (ext_name , options )
55
+ except UnknownMethodException :
56
+ # TODO: Remove the fallback
57
+ self .mark_extension_absence (ext_name ).execute (Command .SET_CLIPBOARD , options )
49
58
return cast ('WebDriver' , self )
50
59
51
60
def set_clipboard_text (self , text : str , label : Optional [str ] = None ) -> 'WebDriver' :
@@ -68,9 +77,15 @@ def get_clipboard(self, content_type: str = ClipboardContentType.PLAINTEXT) -> b
68
77
is supported on Android
69
78
70
79
Returns:
71
- base64-encoded string: Clipboard content. Or return an empty string if the clipboard is empty
80
+ Clipboard content as bytearray . Or empty bytes if the clipboard is empty
72
81
"""
73
- base64_str = self .execute (Command .GET_CLIPBOARD , {'contentType' : content_type })['value' ]
82
+ ext_name = 'mobile: getClipboard'
83
+ options = {'contentType' : content_type }
84
+ try :
85
+ base64_str = self .assert_extension_exists (ext_name ).execute_script (ext_name , options )
86
+ except UnknownMethodException :
87
+ # TODO: Remove the fallback
88
+ base64_str = self .mark_extension_absence (ext_name ).execute (Command .GET_CLIPBOARD , options )['value' ]
74
89
return base64 .b64decode (base64_str )
75
90
76
91
def get_clipboard_text (self ) -> str :
0 commit comments