app专项测试实战1

anrdoid studio : 应用性能指南  |  Android 开发者  |  Android Developers

过滤表达式

domain. 展示 domain 中的资源, *.com
has-response-header. 包含指定 HTTP 响应 header
is. 表达式
larger-than. 展示大于某个尺寸的资源,1000 等于 1k
method. 指定http请求方法,比如 get 或者 post
mime-type. 资源 mime 类型,比如 application/json
scheme. HTTP (scheme:http) 或者 HTTPS (scheme:https).
status-code. 状态码

  • 例子

大于 10k 的图片资源
mime-type:image/png larger-than:1K
所有状态码为 304 的 http 请求
scheme:https status-code:304
域名以 .com 结尾,使用 http 协议
domain:*.com scheme:https

前端加载情况

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

以卸载举例

处于同源时,进行时间计算
protocol、port 和 host 相同

<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> <title>Title</title></head>
<script>
var sleep = function(time) {
var startTime = new Date().getTime() + parseInt(time, 10);
while(new Date().getTime() < startTime) {}};
</script><body onunload="sleep(3000)"> </body>  </html>
window.performance.timing.unloadEventEnd-window.performance.timing.unloadEventStart

使用 appium 获取性能数据

from appium import webdriver
from selenium.webdriver.common.by import By


def test_xueqiu():
    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)
    performance = driver.execute_script("return window.performance.timing")
    print(performance['domComplete'] - performance['responseStart'])

bash性能获取

while true ;do adb shell top -n 1 | grep xueqiu | awk '{print $3}';done
adb shell monkey -p com.xueqiu.android -v 200  2000

作业

  1. 使用 appium 获取交易页面的性能数据,并且计算 connectStart 到 responseEnd 的时间
   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)
    performance = driver.execute_script("return window.performance.timing")
    print(performance)
    print(performance['responseEnd'] - performance['connectStart'])
  1. 使用 top 提取雪球的 cpu 使用率,并循环打印
while true; do adb shell top -n 1 | grep xueqiu | awk '{print $3}'; done

我提取的是微信的

while true ; do adb shell top -n 1 | grep com.tencent.mm | awk '{print $9}';done

交作业:
1.使用 appium 获取交易页面的性能数据,并且计算 connectStart 到 responseEnd 的时间

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.common.by import By


class TestTransaction:
    def setup(self):
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "emulator-5554"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps['automationName'] = 'UiAutomator2'
        caps["autoGrantPermissions"] = True
        caps["onReset"] = True
        caps["chromedriverExecutable"] = '/Users/duxiuyan/projects/chromedriver/2.43/chromedriver'
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        self.driver.implicitly_wait(15)

    def test_trans(self):
        self.driver.find_element(By.XPATH, '//*[@resource-id="com.xueqiu.android:id/tab_name" and @text="交易"]').click()
        webview = self.driver.contexts[-1]
        self.driver.switch_to.context(webview)
        performance = self.driver.execute_script("return window.performance.timing")
        print(performance['domComplete'] - performance['responseStart'])

2、使用 top 提取雪球的 cpu 使用率,并循环打印

while true;do adb shell top -n 1 | grep com.xueqiu.android | awk '{print $5}';done

问题:
用真机调试的时候,performance = self.driver.execute_script(“return window.performance.timing”)这句就报错。

交作业

https://github.com/seselinfeng/Homework/tree/master/AppSpecificMasonSeven

app专项测试实战1 作业

  1. 通过appium完成启动APP-Me页面自动化流程
   # 切换到webview,才可以通过execute_script方法执行js脚本
   webview =driver.swith_to.context(driver.contexts[-1])
  1. 通过js获取页面加载时间
    window.performance.timing
    window.performance.timing.connectStart-window.performance.timing.responseEnd
  1. 通过bash命令获取cpu使用率
while true; do adb shell top n 1|grep xueqiu| awk '{print $3}';  done

作业1:

def test_performance():
    caps = dict()
    caps['platformName'] = 'Android'
    caps['deviceName'] = 'Genymotion'
    caps['udid'] = '192.168.239.101:5555'
    caps['appPackage'] = 'com.xueqiu.android'
    caps['appActivity'] = '.view.WelcomeActivityAlias'
    caps['noReset'] = 'true'
    driver = app_driver.Remote(command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities=caps)
    driver.implicitly_wait(15)
    driver.find_element(MobileBy.XPATH, '//*[@resource-id="android:id/tabs"]//*[@text="交易"]').click()
    webview = driver.contexts[-1]
    driver.switch_to.context(webview)
    perf_timing = driver.execute_script("return window.performance.timing")
    print(perf_timing['responseEnd']-perf_timing['connectStart'])

作业2:

while true; do top -n 1|grep xueqiu|awk '{print $3}';done

问题:老师,如果使用while true; do adb shell top -n 1|grep xueqiu|awk '{print $3}';done查看的是电脑上xueqiu的cpu占用率,如果查看在手机的cpu占用率,应该先adb shell 进到手机,再while true; do top -n 1|grep xueqiu|awk '{print $3}';done吗?