I am trying to run unittests that require external data on device farm with Appium Python. I am uploading the data in the form of a zip file to the 'extra data' argument, and entering "/sdcard/
" into the 'android' argument. I am trying to call the files in the python script using "/WordCountTest1MB
" here. Note that there is no extension to the files, they are just text. However, all of my tests are failing saying that:
test_paramec_0__sdcard_WordCountTest1MB failed: IOError: No such file or directory: '/sdcard/WordCountTest1MB'
Is there something else I need to be doing to access these files?
Any help is much appreciated!
EDIT: Here is the code posted below, note that we are trying to find the location of the data within the extra data argument, however the names of these directories changes with each one so we need to initially find the path of at least one of the needed files:
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()
Looks like there is a problem here
def wordcount(filename):
....
....
with open(os.path.abspath(inFile)) as file: # with can auto close the file
....
above statement is not giving the correct path of file , you need to correct this , here is a list of examples by referring to which you can correct your path
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