18
18
import copy
19
19
20
20
from selenium import webdriver
21
-
22
- from selenium .webdriver .common .by import By
23
- from selenium .webdriver .support .ui import WebDriverWait
24
21
from selenium .common .exceptions import TimeoutException , InvalidArgumentException
25
-
22
+ from selenium . webdriver . common . by import By
26
23
from selenium .webdriver .remote .command import Command as RemoteCommand
24
+ from selenium .webdriver .support .ui import WebDriverWait
27
25
28
26
from appium .webdriver .clipboard_content_type import ClipboardContentType
29
27
from appium .webdriver .common .mobileby import MobileBy
30
- from appium .webdriver .common .touch_action import TouchAction
31
28
from appium .webdriver .common .multi_action import MultiAction
32
-
33
- from .mobilecommand import MobileCommand as Command
29
+ from appium .webdriver .common .touch_action import TouchAction
34
30
from .errorhandler import MobileErrorHandler
31
+ from .mobilecommand import MobileCommand as Command
35
32
from .switch_to import MobileSwitchTo
36
33
from .webelement import WebElement as MobileWebElement
37
34
@@ -776,8 +773,7 @@ def set_value(self, element, value):
776
773
return self
777
774
778
775
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.
781
777
782
778
:Args:
783
779
- path - the path to the file on the device
@@ -799,15 +795,29 @@ def pull_folder(self, path):
799
795
}
800
796
return self .execute (Command .PULL_FOLDER , data )['value' ]
801
797
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`.
804
800
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
808
806
"""
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
+
809
819
data = {
810
- 'path' : path ,
820
+ 'path' : destination_path ,
811
821
'data' : base64data ,
812
822
}
813
823
self .execute (Command .PUSH_FILE , data )
0 commit comments