2017-12-02 152 views
0

我想運行需要設備農場與Appium Python的外部數據的unittests。我將數據以zip文件的形式上傳到'額外數據'參數中,並將「/sdcard/」輸入到'android'參數中。我試圖在這裏使用「/WordCountTest1MB」來調用python腳本中的文件。請注意,文件沒有擴展名,它們只是文本。然而,我所有的測試都沒有說:設備農場 - 如何通過appium 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() 

回答

0

貌似這裏還有一個問題

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 
0

兩個選項,你可以嘗試

1)在集合中的每個元素

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

2)使用COMBIN添加.TXT水珠和的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 
+0

我是否需要在測試運行設置中對'Android'參數進行任何操作。它說:「從SD卡拉文件,如果你想遞歸地拉一個文件夾的內容,使用尾部斜槓。例如/ sdcard /,/ SD卡/文件夾/,/ SD卡/文件」 –

+0

是否有可能,如果你可以粘貼你的代碼在這裏? –

+0

剛剛添加它作爲編輯 –

相關問題