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