1818import copy
1919
2020from selenium import webdriver
21-
22- from selenium .webdriver .common .by import By
23- from selenium .webdriver .support .ui import WebDriverWait
2421from selenium .common .exceptions import TimeoutException , InvalidArgumentException
25-
22+ from selenium . webdriver . common . by import By
2623from selenium .webdriver .remote .command import Command as RemoteCommand
24+ from selenium .webdriver .support .ui import WebDriverWait
2725
2826from appium .webdriver .clipboard_content_type import ClipboardContentType
2927from appium .webdriver .common .mobileby import MobileBy
30- from appium .webdriver .common .touch_action import TouchAction
3128from appium .webdriver .common .multi_action import MultiAction
32-
33- from .mobilecommand import MobileCommand as Command
29+ from appium .webdriver .common .touch_action import TouchAction
3430from .errorhandler import MobileErrorHandler
31+ from .mobilecommand import MobileCommand as Command
3532from .switch_to import MobileSwitchTo
3633from .webelement import WebElement as MobileWebElement
3734
@@ -776,8 +773,7 @@ def set_value(self, element, value):
776773 return self
777774
778775 def pull_file (self , path ):
779- """Retrieves the file at `path`. Returns the file's content encoded as
780- Base64.
776+ """Retrieves the file at `path`. Returns the file's contents as base64.
781777
782778 :Args:
783779 - path - the path to the file on the device
@@ -799,15 +795,29 @@ def pull_folder(self, path):
799795 }
800796 return self .execute (Command .PULL_FOLDER , data )['value' ]
801797
802- def push_file (self , path , base64data ):
803- """Puts the data, encoded as Base64, in the file specified as `path`.
798+ def push_file (self , destination_path , base64data = None , source_path = None ):
799+ """Puts the data from the file at `source_path` , encoded as Base64, in the file specified as `path`.
804800
805- :Args:
806- - path - the path on the device
807- - base64data - data, encoded as Base64, to be written to the file
801+ Specify either `base64data` or `source_path`, if both specified default to `source_path`
802+ :param destination_path: the location on the device/simulator where the local file contents should be saved
803+ :param base64data: file contents, encoded as Base64, to be written to the file on the device/simulator
804+ :param source_path: local file path for the file to be loaded on device
805+ :return: WebDriver instance
808806 """
807+ if source_path is None and base64data is None :
808+ raise InvalidArgumentException ('Must either pass base64 data or a local file path' )
809+
810+ if source_path is not None :
811+ try :
812+ with open (source_path , 'rb' ) as file :
813+ data = file .read ()
814+ except FileNotFoundError :
815+ message = 'source_path {} could not be found. Are you sure the file exists?' .format (source_path )
816+ raise InvalidArgumentException (message )
817+ base64data = base64 .b64encode (data ).decode ('utf-8' )
818+
809819 data = {
810- 'path' : path ,
820+ 'path' : destination_path ,
811821 'data' : base64data ,
812822 }
813823 self .execute (Command .PUSH_FILE , data )
0 commit comments