【问题标题】:Device Farm - How to access extra data with appium pythonDevice Farm - 如何使用 appium python 访问额外数据
【发布时间】:2026-02-13 02:15:01
【问题描述】:

我正在尝试使用 Appium Python 在设备场上运行需要外部数据的单元测试。我正在以 zip 文件的形式将数据上传到“extra data”参数,并在“android”参数中输入“/sdcard/”。我试图在这里使用“/WordCountTest1MB”调用python脚本中的文件。请注意,文件没有扩展名,它们只是文本。但是,我所有的测试都失败了:

test_paramec_0__sdcard_WordCountTest1MB failed: IOError: No such file or directory: '/sdcard/WordCountTest1MB'

我还需要做些什么来访问这些文件吗?

非常感谢任何帮助!

编辑:这是下面发布的代码,请注意,我们试图在额外数据参数中找到数据的位置,但是这些目录的名称会随着每个目录的变化而变化,因此我们需要首先找到 at至少一个所需文件:

import unittest2
from appium import webdriver
from parameterized import parameterized
import os

for dirpath, dirnames, filenames in os.walk("."):
    for filename in [f for f in filenames if f.startswith("WordCountTest10MB.txt")]:
        dir_name =  os.path.join(dirpath, filename)

print os.path.dirname(dir_name)


def Test1(rootDir):
    list_dirs = os.walk(rootDir)
    for root, dirs, files in list_dirs:
        for d in dirs:
            print os.path.join(root, d)
        for f in files:
            print os.path.join(root, f)

# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

def wordcount(filename):
    for j in range(1, 10):
        wordcount = {}
        inFile = filename
        with open(os.path.abspath(inFile)) as file:  # with can auto close the file
            for word in file.read().split():
                if word not in wordcount:
                    wordcount[word] = 1
                else:
                    wordcount[word] += 1
        wordcount = sorted(wordcount.items(), key=lambda x: x[1], reverse=True)
        for k, v in wordcount:
            print(k, v)

class WordCountTest(unittest2.TestCase):

    known_files = {'/WordCountTest1MB.txt',
                   '/WordCountTest5MB.txt',
                   '/WordCountTest10MB.txt'}

    def SetUpClass(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '5.0'
        desired_caps['deviceName'] = 'Android Emulator'
        desired_caps['app'] = PATH('../../../apps/Nothing_v0_apkpure.com.apk')
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    @parameterized.expand(known_files)

    def test_paramec(self, filename):
        os.chdir(dir_name)
        print os.getcwd(), Test1(os.getcwd())
        wordcount(filename)

    def TearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest2.main()

【问题讨论】:

    标签: android python appium aws-device-farm


    【解决方案1】:

    这里好像有问题

    def wordcount(filename):
        ....
        ....
            with open(os.path.abspath(inFile)) as file:  # with can auto close the file
        ....
    

    上面的语句没有给出正确的文件路径,你需要更正这个,这里是一个例子列表,你可以参考这些例子来更正你的路径

    fileDir = os.path.dirname(os.path.realpath('__file__'))
    print fileDir
    
    #For accessing the file in the same folder
    filename = "same.txt"
    readFile(filename)
    
    #For accessing the file in a folder contained in the current folder
    filename = os.path.join(fileDir, 'Folder1.1/same.txt')
    readFile(filename)
    
    #For accessing the file in the parent folder of the current folder
    filename = os.path.join(fileDir, '../same.txt')
    readFile(filename)
    
    #For accessing the file inside a sibling folder.
    filename = os.path.join(fileDir, '../Folder2/same.txt')
    filename = os.path.abspath(os.path.realpath(filename))
    print filename
    

    【讨论】:

      【解决方案2】:

      您可以尝试两个选项

      1) 在集合的每个元素中添加.txt

       known_files = {'/sdcard/WordCountTest1MB.txt',
       '/sdcard/WordCountTest5MB.txt',
       '/sdcard/WordCountTest10MB.txt'}
      

      2) 使用 glob 和文件输入的组合

       import fileinput
       from glob import glob
       #This will store all the file names
       fnames = glob('WordCountTest*')
       for line in fileinput.input(fnames):
           pass # modify your code
      

      【讨论】:

      • 我是否需要对测试运行设置中的“Android”参数执行任何操作。它说:“从 sdcard 中提取文件。如果要递归提取文件夹的内容,请使用尾部斜杠。例如 /sdcard/、/sdcard/folder/、/sdcard/file”