Skip to content

Commit 34ad055

Browse files
committed
run Andorid test and upload its screen shot
1 parent 2e0a479 commit 34ad055

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

py2014/app_poi.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -*- coding: utf-8 -*-
2+
3+
'''
4+
Created on Apr 21, 2014
5+
6+
@author: Jay <yongjie.ren@dianping.com>
7+
'''
8+
9+
from StringIO import StringIO
10+
import pycurl
11+
import time
12+
import os
13+
from poi import poi
14+
15+
16+
class app_poi(object):
17+
'''
18+
running DianPing App in an Andorid device or emulator.
19+
we assume DianPing App and my TestApp are already installed in the android system.
20+
'''
21+
22+
upload_url = "http://qa-show-web02.nh/upload.php"
23+
adb = '/Users/jay/workspace/adt-bundle-mac-x86_64-20130917/sdk/platform-tools/adb'
24+
app_img_dir = '/mnt/sdcard/Robotium-Screenshots'
25+
26+
def __init__(self):
27+
pass
28+
29+
def clear_storage(self):
30+
'''
31+
clear the Robotium screen shot storage.
32+
'''
33+
34+
clear_storage_cmd = '%s shell rm -f %s/*_*.jpg' % (self.adb, self.app_img_dir)
35+
os.system(clear_storage_cmd)
36+
37+
def gen_img(self, poi_info=None, img_dir='img'):
38+
'''
39+
generate APP screen snapshot on different poi geo_location
40+
41+
oi_info must be a list as [shopid, shopname, longitude, latitude, citiyid, cityname]
42+
e.g. [9125655, '龙马川菜馆', 119.940181, 31.969895, 93, '常州']
43+
'''
44+
45+
# which_adb = 'which adb'
46+
# p = os.popen(which_adb)
47+
# adb = p.read().strip()
48+
cwd = os.getcwd()
49+
if not os.path.exists(img_dir):
50+
os.makedirs(img_dir)
51+
cityid = poi_info[4]
52+
lng = poi_info[2]
53+
lat = poi_info[3]
54+
# image name is $cityid_$shopid.jpg '.jpg' will be automatically added in Robotium takecreenshot function.
55+
imgname = '%s_%s' % (cityid, poi_info[0])
56+
57+
# currently, I assume there's only one android device/emulator running on the PC
58+
run_test_cmd = '%s shell am instrument -e lng %s -e lat %s -e cityid %s -e imgname %s ' % (self.adb, lng, lat, cityid, imgname)\
59+
+ '-w com.dianping.v1.test/com.dianping.v1.test.MyTestRunner'
60+
os.system(run_test_cmd)
61+
time.sleep(20)
62+
os.chdir(img_dir)
63+
pull_img_cmd = '%s pull %s/%s.jpg %s.jpg' % (self.adb, self.app_img_dir, imgname, imgname)
64+
os.system(pull_img_cmd)
65+
time.sleep(2)
66+
os.chdir(cwd) #need to change current dir to the previous dir.
67+
68+
# return the absolute path of the image file
69+
return '%s/%s/%s.jpg' % (cwd, img_dir, imgname)
70+
71+
def upload_img(self, mypic):
72+
'''
73+
upload the image to the remote server.
74+
'''
75+
76+
storage = StringIO()
77+
c = pycurl.Curl()
78+
values = [
79+
("upload_file[]", (pycurl.FORM_FILE, str(mypic)))
80+
]
81+
c.setopt(c.URL, self.upload_url)
82+
c.setopt(c.WRITEFUNCTION, storage.write)
83+
c.setopt(c.HTTPPOST, values)
84+
c.perform()
85+
c.close()
86+
content = storage.getvalue()
87+
print content
88+
89+
if __name__ == '__main__':
90+
myapp = app_poi()
91+
myapp.clear_storage()
92+
poi = poi()
93+
poi_info_list = poi.xml_to_poi_info_list()
94+
for i in poi_info_list:
95+
mypic = myapp.gen_img(poi_info=i)
96+
myapp.upload_img(mypic)
97+
# myapp.upload_img('/Users/jay/workspace/aew_backend/qa-show/img/93_16887136.jpg')

0 commit comments

Comments
 (0)