1515import base64
1616from typing import TYPE_CHECKING , Optional , cast
1717
18+ from selenium .common .exceptions import UnknownMethodException
19+
1820from 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
1923from appium .webdriver .clipboard_content_type import ClipboardContentType
2024
2125from ..mobilecommand import MobileCommand as Command
2428 from appium .webdriver .webdriver import WebDriver
2529
2630
27- class Clipboard (CanExecuteCommands ):
31+ class Clipboard (CanExecuteCommands , CanExecuteScripts , CanRememberExtensionPresence ):
2832 def set_clipboard (
2933 self , content : bytes , content_type : str = ClipboardContentType .PLAINTEXT , label : Optional [str ] = None
3034 ) -> 'WebDriver' :
@@ -39,13 +43,18 @@ def set_clipboard(
3943 Returns:
4044 Union['WebDriver', 'Clipboard']: Self instance
4145 """
46+ ext_name = 'mobile: setClipboard'
4247 options = {
4348 'content' : base64 .b64encode (content ).decode ('UTF-8' ),
4449 'contentType' : content_type ,
4550 }
4651 if label :
4752 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 )
4958 return cast ('WebDriver' , self )
5059
5160 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
6877 is supported on Android
6978
7079 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
7281 """
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' ]
7489 return base64 .b64decode (base64_str )
7590
7691 def get_clipboard_text (self ) -> str :
0 commit comments