Device Farm - 如何使用 appium 访问额外数据 python

Device Farm - How to access extra data with appium python

我正在尝试 运行 需要使用 Appium Python 在 Device Farm 上获取外部数据的单元测试。我正在以 zip 文件的形式将数据上传到“extra data”参数,并在“[=27”中输入“/sdcard/” =]' 参数。我正在尝试在此处使用“/WordCountTest1MB”调用 python 脚本中的文件。请注意,文件没有扩展名,它们只是文本。但是,我所有的测试都失败了:

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

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

非常感谢任何帮助!

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

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()

您可以尝试两个选项

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

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

2) 使用 glob 和 fileinput 的组合

 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

这里好像有问题

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