app专项测试实战2

综合提取

bash 提取空闲内存

while true; do adb shell vmstat | tail -1 | awk '{print $4}'; done

python 提取空闲内存

    cmd = "adb shell vmstat"
    res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    logging.info(str(res.stdout.read(), encoding="utf-8").split("\r\n")[2].split()[3])

webview

https://www.w3.org/TR/navigation-timing/#performancenavigation

下面分别实现导航,重新加载,回退的操作码

const unsigned short TYPE_NAVIGATE = 0;
const unsigned short TYPE_RELOAD = 1;
const unsigned short TYPE_BACK_FORWARD = 2;

查询当前的操作内容

window.performance.navigation.type

动作模拟

window.history.back()
window.history.forward()
window.location.reload()
window.location.href="https://www.baidu.com/"

     caps = {}
    caps["platformName"] = "Android"
    caps["deviceName"] = "127.0.0.1:7555"
    caps["appPackage"] = "com.xueqiu.android"
    caps["appActivity"] = ".view.WelcomeActivityAlias"
    caps['noReset'] = "true"
    caps['chromedriverExecutable'] = "D:/develop/chromedriver/2.20.exe"
    driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
    driver.implicitly_wait(15)
    driver.find_element(By.XPATH, "//*[@text='交易']").click()
    webview = driver.contexts[-1]
    driver.switch_to.context(webview)
    # 查看刚进入页面的操作码:0
    print(driver.execute_script("return window.performance.navigation.type"))
    # 进入到百度页面
    driver.execute_script("window.location.href='https://www.baidu.com/'")
    # 实现页面重载操作
    driver.execute_script("window.location.reload()")
    # 查看操作码:1
    print(driver.execute_script("return window.performance.navigation.type"))

使用 location.href 查看前端代码位置

    caps = {}
    caps["platformName"] = "Android"
    caps["deviceName"] = "127.0.0.1:7555"
    caps["appPackage"] = "com.xueqiu.android"
    caps["appActivity"] = ".view.WelcomeActivityAlias"
    caps['noReset'] = "true"
    caps['chromedriverExecutable'] = "D:/develop/chromedriver/2.20.exe"
    driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
    driver.implicitly_wait(15)
    driver.find_element(By.XPATH, "//*[@text='交易']").click()
    webview = driver.contexts[-1]
    driver.switch_to.context(webview)
    # 查看刚进入页面的操作码:0
    print(driver.execute_script("return window.location.href"))
#----------------------------- res

file:///data/user/0/com.xueqiu.android/files/h5/modules_new/xueqiu.com/broker/tradeHome.html?hkus_login=false

把内容存入本地

adb pull /data/user/0/com.xueqiu.android/files/h5/modules_new/xueqiu.com Desktop/xueqiu.com

使用 logcat 找出 http 信息

adb logcat | grep http

耗电量

adb shell dumpsys power

]

// 问题代码
  void doSomethingAndRelease() throws MyException {
        wakeLock.acquire();
        doSomethingThatThrows();
        wakeLock.release();  // does not run if an exception is thrown
    }

// 优化代码
 void doSomethingAndRelease() throws MyException {
        try {
            wakeLock.acquire();
            doSomethingThatThrows();
        } finally {
            wakeLock.release();
        }
    }

课间练习 webiew 的性能获取,耗电量情况

dumpsys

查看所有服务
adb shell dumpsys 
查看可用服务
adb shell dumpsys -l

卡顿分析

   cmd = "adb shell dumpsys gfxinfo com.xueqiu.android"
    res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    lines = res.stdout.readlines()
    i = 1
    # 数据的提取
    for line in lines:
        i += 1
        if "com.xueqiu.android.common.MainActivity" in line.decode("utf-8"):
            break
    # 处理,删除 windows 特殊字符,并提取 120 帧
    lines = [x.decode("utf-8").replace("\r\n", "").replace("\t", " ").strip() for x in lines]
    lines = lines[i:i + 120]
    datas = [[] for row in range(4)]
    # 把四列数据分别存入二维 list
    for x in lines:
        datas[0].append(float(x.split()[0]))
        datas[1].append(float(x.split()[1]))
        datas[2].append(float(x.split()[2]))
        datas[3].append(float(x.split()[3]))
    # 生成画布
    fig = pyplot.figure()
    # 折线图,第一个图片
    ax1 = fig.add_subplot(2, 2, 1)
    ax1.plot(datas[0])
    ax1.set_title("draw")
    # 直方图,第二个图片
    ax1 = fig.add_subplot(2, 2, 2)
    ax1.hist(datas[1], range(5))
    ax1.set_title("prepare")
    # 散点图,第三个图片
    ax1 = fig.add_subplot(2, 2, 3)
    ax1.scatter(range(120), datas[2])
    ax1.set_title("process")
    # 虚线图,第图个图片
    ax1 = fig.add_subplot(2, 2, 4)
    ax1.plot(range(120),datas[3], 'k--')
    ax1.set_title("execute")
    pyplot.show()

崩溃问题

ANR:
主线程5s没响应
Java Crash:
未捕获的 android vm 异常
Native Crash
未处理的native异常

image

image

检测方法
查看 trace 日志(新版本 android)
adb pull /data/anr/traces.txt
开发者: Take bug report
adb bugreport E:\Reports\MyBugReports

关闭