1 #/usr/bin/python 2 #encoding:utf-8 3 import csv 4 import os 5 import time 6 7 8 class App(object): 9 def __init__(self): 10 self.content = "" 11 self.startTime = 0 12 13 #启动App 14 def LaunchApp(self): 15 cmd = 'adb shell am start -W -n com.android.browser/.BrowserActivity' 16 self.content=os.popen(cmd) 17 18 #停止App 19 def StopApp(self): 20 #cmd = 'adb shell am force-stop com.android.browser' 21 cmd = 'adb shell input keyevent 3' 22 os.popen(cmd) 23 24 #获取启动时间 25 def GetLaunchedTime(self): 26 for line in self.content.readlines(): 27 if "ThisTime" in line: 28 self.startTime = line.split(":")[1] 29 break 30 return self.startTime 31 32 #控制类 33 class Controller(object): 34 def __init__(self, count): 35 self.app = App() 36 self.counter = count 37 self.alldata = [("timestamp", "elapsedtime")] 38 39 #单次测试过程 40 def testprocess(self): 41 self.app.LaunchApp() 42 time.sleep(5) 43 elpasedtime = self.app.GetLaunchedTime() 44 self.app.StopApp() 45 time.sleep(3) 46 currenttime = self.getCurrentTime() 47 self.alldata.append((currenttime, elpasedtime)) 48 49 #多次执行测试过程 50 def run(self): 51 while self.counter >0: 52 self.testprocess() 53 self.counter = self.counter - 1 54 55 #获取当前的时间戳 56 def getCurrentTime(self): 57 currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 58 return currentTime 59 60 #数据的存储 61 def SaveDataToCSV(self): 62 csvfile = file('startTime2.csv', 'wb') 63 writer = csv.writer(csvfile) 64 writer.writerows(self.alldata) 65 csvfile.close() 66 67 if __name__ == "__main__": 68 controller = Controller(10) 69 controller.run() 70 controller.SaveDataToCSV()
1 #/usr/bin/python 2 #encoding:utf-8 3 import csv 4 import os 5 import time 6 7 #控制类 8 class Controller(object): 9 def __init__(self): 10 #定义收集数据的数组 11 self.alldata = [("id", "vss", "rss")] 12 13 #分析数据 14 def analyzedata(self): 15 content = self.readfile() 16 i = 0 17 for line in content: 18 if "com.android.browser" in line: 19 print line 20 line = "#".join(line.split()) 21 vss = line.split("#")[5].strip("K") 22 rss = line.split("#")[6].strip("K") 23 24 #将获取到的数据存到数组中 25 self.alldata.append((i, vss, rss)) 26 i = i + 1 27 28 #数据的存储 29 def SaveDataToCSV(self): 30 csvfile = file('meminfo.csv', 'wb') 31 writer = csv.writer(csvfile) 32 writer.writerows(self.alldata) 33 csvfile.close() 34 35 #读取数据文件 36 def readfile(self): 37 mfile = file("meminfo", "r") 38 content = mfile.readlines() 39 mfile.close() 40 return content 41 42 if __name__ == "__main__": 43 controller = Controller() 44 controller.analyzedata() 45 controller.SaveDataToCSV()
1 #/usr/bin/python 2 #encoding:utf-8 3 import csv 4 import os 5 import time 6 7 #控制类 8 class Controller(object): 9 def __init__(self, count): 10 #定义测试的次数 11 self.counter = count 12 #定义收集数据的数组 13 self.alldata = [("timestamp", "power")] 14 15 #单次测试过程 16 def testprocess(self): 17 #执行获取电量的命令 18 result = os.popen("adb shell dumpsys battery") 19 #获取电量的level 20 for line in result: 21 if "level" in line: 22 power = line.split(":")[1] 23 24 #获取当前时间 25 currenttime = self.getCurrentTime() 26 #将获取到的数据存到数组中 27 self.alldata.append((currenttime, power)) 28 29 #多次测试过程控制 30 def run(self): 31 #设置手机进入非充电状态 32 os.popen("adb shell dumpsys battery set status 1") 33 while self.counter >0: 34 self.testprocess() 35 self.counter = self.counter - 1 36 #每5秒钟采集一次数据 37 time.sleep(5) 38 39 #获取当前的时间戳 40 def getCurrentTime(self): 41 currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 42 return currentTime 43 44 #数据的存储 45 def SaveDataToCSV(self): 46 csvfile = file('meminfo.csv', 'wb') 47 writer = csv.writer(csvfile) 48 writer.writerows(self.alldata) 49 csvfile.close() 50 51 if __name__ == "__main__": 52 controller = Controller(5) 53 controller.run() 54 controller.SaveDataToCSV()
1 #/usr/bin/python 2 #encoding:utf-8 3 import csv 4 import os 5 import string 6 import time 7 8 #控制类 9 class Controller(object): 10 def __init__(self, count): 11 #定义测试的次数 12 self.counter = count 13 #定义收集数据的数组 14 self.alldata = [("timestamp", "traffic")] 15 16 #单次测试过程 17 def testprocess(self): 18 #执行获取进程的命令 19 result = os.popen("adb shell ps | grep com.android.browser") 20 #获取进程ID 21 pid = result.readlines()[0].split(" ")[5] 22 23 #获取进程ID使用的流量 24 traffic = os.popen("adb shell cat /proc/"+pid+"/net/dev") 25 for line in traffic: 26 if "eth0" in line: 27 #将所有空行换成# 28 line = "#".join(line.split()) 29 #按#号拆分,获取收到和发出的流量 30 receive = line.split("#")[1] 31 transmit = line.split("#")[9] 32 elif "eth1" in line: 33 # 将所有空行换成# 34 line = "#".join(line.split()) 35 # 按#号拆分,获取收到和发出的流量 36 receive2 = line.split("#")[1] 37 transmit2 = line.split("#")[9] 38 39 #计算所有流量的之和 40 alltraffic = string .atoi(receive) + string .atoi(transmit) + string .atoi(receive2) + string .atoi(transmit2) 41 #按KB计算流量值 42 alltraffic = alltraffic/1024 43 #获取当前时间 44 currenttime = self.getCurrentTime() 45 #将获取到的数据存到数组中 46 self.alldata.append((currenttime, alltraffic)) 47 48 #多次测试过程控制 49 def run(self): 50 while self.counter >0: 51 self.testprocess() 52 self.counter = self.counter - 1 53 #每5秒钟采集一次数据 54 time.sleep(5) 55 56 #获取当前的时间戳 57 def getCurrentTime(self): 58 currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 59 return currentTime 60 61 #数据的存储 62 def SaveDataToCSV(self): 63 csvfile = file('traffic.csv', 'wb') 64 writer = csv.writer(csvfile) 65 writer.writerows(self.alldata) 66 csvfile.close() 67 68 if __name__ == "__main__": 69 controller = Controller(5) 70 controller.run() 71 controller.SaveDataToCSV()
1 #/usr/bin/python 2 #encoding:utf-8 3 import csv 4 import os 5 import time 6 7 #控制类 8 class Controller(object): 9 def __init__(self, count): 10 self.counter = count 11 self.alldata = [("timestamp", "cpustatus")] 12 13 #单次测试过程 14 def testprocess(self): 15 result = os.popen("adb shell dumpsys cpuinfo | grep com.android.browser") 16 for line in result.readlines(): 17 cpuvalue = line.split("%")[0] 18 19 currenttime = self.getCurrentTime() 20 self.alldata.append((currenttime, cpuvalue)) 21 22 #多次执行测试过程 23 def run(self): 24 while self.counter >0: 25 self.testprocess() 26 self.counter = self.counter - 1 27 time.sleep(3) 28 29 #获取当前的时间戳 30 def getCurrentTime(self): 31 currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 32 return currentTime 33 34 #数据的存储 35 def SaveDataToCSV(self): 36 csvfile = file('cpustatus.csv', 'wb') 37 writer = csv.writer(csvfile) 38 writer.writerows(self.alldata) 39 csvfile.close() 40 41 if __name__ == "__main__": 42 controller = Controller(10) 43 controller.run() 44 controller.SaveDataToCSV()
adb logcat \ grep START
HybridScript:
1 # urs/bin/python 2 # encoding:utf-8 3 import time 4 from appium import webdriver 5 import unittest 6 7 class MyTestCase(unittest.TestCase): 8 def setUp(self): 9 # 定义初始化的属性信息 10 self.desired_caps = {} 11 self.desired_caps['platformName'] = 'Android' 12 self.desired_caps['platformVersion'] = '4.3' 13 self.desired_caps['deviceName'] = '192.168.56.101:5555' 14 self.desired_caps['appPackage'] = 'com.example.zhangjian.minibrowser2' 15 self.desired_caps['appActivity'] = '.myapplication.MainActivity' 16 self.desired_caps["unicodeKeyboard"] = "True" 17 self.desired_caps["resetKeyboard"] = "True" 18 self.desired_caps["automationName"] = "Selendroid" 19 self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps) 20 21 '''def testSearch(self): 22 #Locate 定位输入框 23 input = self.driver.find_element_by_id("url") 24 #Operate 操作 25 input.send_keys("http://wap.sogou.com") 26 27 searchbutton = self.driver.find_element_by_id("searchbutton") 28 searchbutton.click() 29 30 time.sleep(5) 31 32 #Switch 切换当前的上下文 33 print self.driver.contexts 34 self.driver.switch_to.context('WEBVIEW_0') 35 print self.driver.current_context 36 time.sleep(5) 37 38 #定位web输入框 39 webinput = self.driver.find_element_by_xpath('//*[@id="keyword"]') 40 webinput.click() 41 webinput.send_keys("mook") 42 websearchbutton = self.driver.find_element_by_xpath('//*[@id="searchform"]/div/div[1]/div[3]/input') 43 websearchbutton.click() 44 time.sleep(5) 45 46 #检验查询结果 47 firstresult = self.driver.find_element_by_xpath('//*[@id="mainBody"]/div[2]/div[2]/a') 48 self.assertTrue(u"中国大学" in firstresult.text)''' 49 50 def testFindElements(self): 51 # Locate 定位输入框 52 input = self.driver.find_element_by_id("url") 53 # Operate 操作 54 input.send_keys("http://wap.sogou.com") 55 56 searchbutton = self.driver.find_element_by_id("searchbutton") 57 searchbutton.click() 58 59 time.sleep(5) 60 61 # Switch 切换当前的上下文 62 print self.driver.contexts 63 self.driver.switch_to.context('WEBVIEW_0') 64 print self.driver.current_context 65 time.sleep(5) 66 67 #定位元素组 68 elements = self.driver.find_elements_by_xpath('//*[@id="page"]/div[2]/div[2]/div/table/tbody/tr/td') 69 70 #输出所有元素的名称 71 for el in elements: 72 print el.text 73 74 def tearDown(self): 75 self.driver.quit() 76 77 if __name__ == '__main__': 78 unittest.main()
nativapp:
1 # urs/bin/python 2 # encoding:utf-8 3 import time 4 from appium import webdriver 5 import unittest 6 7 class MyTestCase(unittest.TestCase): 8 #脚本初始化,获取操作实例 9 def setUp(self): 10 desired_caps = {} 11 desired_caps['platformName'] = 'Android' 12 desired_caps['platformVersion'] = '4.3' 13 desired_caps['deviceName'] = '192.168.56.101:5555' 14 #desired_caps['appPackage'] = 'com.android.calculator2' 15 #desired_caps['appActivity'] = '.Calculator' 16 #desired_caps['appPackage'] = 'com.android.customlocale2' 17 #desired_caps['appActivity'] = '.CustomLocaleActivity' 18 desired_caps['appPackage'] = 'com.example.zhangjian.minibrowser2' 19 desired_caps['appActivity'] = '.myapplication.MainActivity' 20 desired_caps["unicodeKeyboard"] = "True" 21 desired_caps["resetKeyboard"] = "True" 22 self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) 23 24 #释放实例,释放资源 25 def tearDown(self): 26 self.driver.quit() 27 28 #测试的脚本, LOVE原则 29 '''def testAdd(self): 30 #Locate 定位一个元素 31 number8 = self.driver.find_element_by_id("digit8") 32 # Operate 操作一个元素 33 number8.click() 34 # Locate 定位一个元素 35 addopertion = self.driver.find_element_by_id("plus") 36 # Operate 操作一个元素 37 addopertion.click() 38 # Locate 定位一个元素 39 number5 = self.driver.find_element_by_id("digit5") 40 # Operate 操作一个元素 41 number5.click() 42 # Locate 定位一个元素 43 equal = self.driver.find_element_by_id("equal") 44 # Operate 操作一个元素 45 equal.click() 46 47 #Verify 验证操作的结果 48 result = self.driver.find_element_by_class_name("android.widget.EditText") 49 value = result.text 50 self.assertEqual(u"13", value) 51 #Exception 处理异常的情况''' 52 def testOtherAPI(self): 53 '''elements = self.driver.find_elements_by_id("digit8") 54 elements[0].click() 55 time.sleep(5) 56 print(len(elements))''' 57 time.sleep(3) 58 #self.driver.press_keycode(8) 59 #self.driver.press_keycode(7) 60 input = self.driver.find_element_by_class_name("android.widget.EditText") 61 input.send_keys("10") 62 63 element = self.driver.find_element_by_accessibility_id(u"除") 64 element.click() 65 66 self.driver.press_keycode(12) 67 68 equal = self.driver.find_element_by_id("equal") 69 equal.click() 70 time.sleep(5) 71 72 #其他更多APIs的使用实例 73 def testMoreAPIs(self): 74 #获取元素列表 75 els = self.driver.find_elements_by_class_name('android.widget.CheckedTextView') 76 #滚动API scroll 的用法 77 #self.driver.scroll(els[10], els[1]) 78 #拖拽API drag_and_drop的用法 79 #self.driver.drag_and_drop(els[10], els[3]) 80 #滑动API swipe的用法 81 #self.driver.swipe(100, 750, 100, 100) 82 #点击API tap的用法 83 #self.driver.tap([(100, 750)]) 84 85 #快速滑动 API flick的用法 86 #self.driver.flick(100, 750, 100, 100) 87 #当前activity API current_Activity的用法 88 #print self.driver.current_activity 89 #将某一个App置于后台 90 #self.driver.background_app(3) 91 #等待指定activity显示 API wait_activity的用法 92 #print self.driver.wait_activity(".CustomLocaleActivity", 3, 1) 93 94 #判断app是否安装了 95 #print self.driver.is_app_installed("com.example.zhangjian.minibrowser2") 96 #删除app 97 #self.driver.remove_app("com.example.zhangjian.minibrowser2") 98 #安装app 99 #self.driver.install_app("/Users/zhangjian/Downloads/app-debug.apk") 100 #启动app 101 #self.driver.launch_app() 102 103 #关闭app 104 #self.driver.close_app() 105 #self.driver.launch_app() 106 #启动activity 107 self.driver.start_activity("com.example.zhangjian.minibrowser2", 108 ".myapplication.NewActivity") 109 time.sleep(3) 110 #截屏 111 self.driver.get_screenshot_as_file("test.png") 112 time.sleep(5) 113 114 115 if __name__ == '__main__': 116 suite = unittest.TestSuite() 117 suite.addTest(MyTestCase('testMoreAPIs')) 118 runner = unittest.TextTestRunner(verbosity=2) 119 runner.run(suite)