app自動化測試python


 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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM