Python:将文件推送到Android设备传递但文件为空

时间:2019-06-20 10:45:45

标签: android python appium driver

我有正在测试的应用程序,并且想将file推送到我的android设备(真实设备)中

这是我尝试过的:

self.driver.push_file('/mnt/sdcard/Pictures/photo.png', r'C:\photo.png')

因此该操作通过了,我可以在file上看到device,但是它的size1kb,当我尝试打开它时,我有这个{{ 1}}:

  

我们似乎不支持此文件格式

我做错了什么?

1 个答案:

答案 0 :(得分:1)

请注意,使用 Appium Python 语言,当您调用self.driver.push_file()方法时,第二个参数是文件的内容base64 (而不是计算机上文件的路径)。

这意味着您首先必须读取文件,将其转换为base64(并使用utf-8对其进行解码),然后再将其传递给此方法:

import base64
...
with open(r'C:\photo.png','rb') as file:
    driver.push_file('/mnt/sdcard/Pictures/photo.png',
                     base64.b64encode(file.read()).decode('utf-8'))

或者,您可以简单地使用以下命令(仅将source_path=添加到您的代码段中):

self.driver.push_file('/mnt/sdcard/Pictures/photo.png', source_path=r'C:\photo.png')

..因为push_file()方法最近已更新为还支持源路径(请参见Pull #270):

def push_file(self, destination_path, base64data=None, source_path=None)

我当然会建议使用source_path参数:)