线上七期_Android 自动化测试_20181020

参考命令

chown -R seveniruby:staff /usr/local/lib/node_modules/

Appium Log分析

  • sessions信息获取
  • 创建session,提供app信息
  • 探测java
adb shell getprop ro.build.version.sdk
adb shell pm list packages com.xueqiu.android
adb shell echo ping
adb shell pm list packages io.appium.settings
adb shell dumpsys package io.appium.settings
adb shell ps
adb shell am start -W -n io.appium.settings/.Settings -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000
adb shell appops set io.appium.settings android\\:mock_location allow
adb shell pm list packages io.appium.unlock
adb shell dumpsys package io.appium.unlock
adb shell getprop ro.build.version.release
adb shell wm size
adb shell getprop ro.product.model
adb shell getprop ro.product.manufacturer
adb shell pm list packages com.xueqiu.android
adb shell am force-stop com.xueqiu.android
adb shell pm clear com.xueqiu.android
adb shell ps
adb shell dumpsys window
adb shell am start -W -n com.xueqiu.android/.view.WelcomeActivityAlias -S
adb shell cat /proc/net/unix

Uiautomator2

adb shell am force-stop com.xueqiu.android
adb shell input keyevent 3
adb shell ps
adb shell am force-stop io.appium.unlock
adb shell getprop ro.build.version.sdk
adb shell getprop ro.build.version.release
adb shell wm size
adb shell wm density
adb shell getprop ro.product.model
adb shell getprop ro.product.manufacturer
adb shell am force-stop io.appium.uiautomator2.server
adb shell pm list packages io.appium.uiautomator2.server
adb shell dumpsys package io.appium.uiautomator2.server
adb shell pm list packages io.appium.uiautomator2.server.test
adb shell dumpsys package io.appium.uiautomator2.server.test
adb shell pm list instrumentation
adb shell echo ping
adb shell pm list packages io.appium.settings
adb shell dumpsys package io.appium.settings
adb shell ps
adb shell am start -W -n io.appium.settings/.Settings -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000
adb shell appops set io.appium.settings android\\:mock_location allow
adb shell pm list packages io.appium.unlock
adb shell dumpsys package io.appium.unlock
adb shell rm -rf /data/local/tmp/strings.json
adb shell pm path com.xueqiu.android
adb shell pm list packages com.xueqiu.android
adb shell am force-stop com.xueqiu.android
adb shell pm clear com.xueqiu.android
adb shell ps
adb shell am force-stop io.appium.uiautomator2.server
adb shell am instrument -w io.appium.uiautomator2.server.test/android.support.test.runner.AndroidJUnitRunner
adb shell dumpsys window
adb shell am start -W -n com.xueqiu.android/.view.WelcomeActivityAlias -S
adb shell cat /proc/net/unix
```

## ADB

![](/uploads/photo/2018/c223f131-a206-49b2-b00b-9ce861e3ecf4.png!large)

![](/uploads/photo/2018/f83d59c4-add0-441b-ae94-d3cacfc98db9.png!large)

```bash
MacBook-Pro-2:~ seveniruby$ adb shell ps | grep adbd
root      133   1     13648  728   poll_sched 0808e8f5 S /sbin/adbd

MacBook-Pro-2:~ seveniruby$ ps -ef | grep adb
  503  6816     1   0 10:46AM ??         0:19.85 adb -L tcp:5037 fork-server server --reply-fd 4
  503  6820  6760   0 10:46AM ??         0:00.03 /usr/local/Cellar/android-sdk/24.4.1_1/platform-tools/adb.ori -s 192.168.59.101:5555 shell
  503  6823  6760   0 10:46AM ??         0:00.29 /usr/local/Cellar/android-sdk/24.4.1_1/platform-tools/adb.ori -s 192.168.59.101:5555 logcat -v time
  503 10108  5338   0 11:23AM ??         0:00.07 /usr/local/Cellar/android-sdk/24.4.1_1/platform-tools/adb.ori -P 5037 -s 192.168.59.101:5555 logcat -v threadtime
  503 14586  5592   0 11:54AM ttys003    0:00.00 grep adb
  503  8814  7472   0 11:02AM ttys004    0:00.03 /usr/local/Cellar/android-sdk/24.4.1_1/platform-tools/adb.ori shell

```

## POM文件模板

```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testerhome.hogwart</groupId>
    <artifactId>AppiumDemo1013</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>6.1.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
```

## 自动启动模拟器
capability配置avd 

## Android定位符
- https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/android/uiautomator-uiselector.md

UA1的定位
![](/uploads/photo/2018/71f16b51-98e3-4b85-9c8d-c17d9916969c.png!large)

UA2的定位逻辑
![](/uploads/photo/2018/537cc015-2e8a-4334-826b-ca8dff6bfb52.png!large)

## 短信
![](/uploads/photo/2018/35722fde-57a2-4d9e-b55a-f9a2ce995f65.png!large =400x)


## 演练demo
- https://github.com/appium/sample-code/raw/master/sample-code/apps/ApiDemos/bin/ApiDemos-debug.apk

## Toast
![](/uploads/photo/2018/5032a6b1-6301-4a42-8830-c1e7442065ac.png!large)

```python
from appium.webdriver.common.mobileby import MobileBy
from hamcrest import *

#配置程序入口信息
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By

class ContactsAndroidTests(unittest.TestCase):
    def setUp(self):
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "hogwarts"
        #caps["appPackage"] = "com.xueqiu.android"
        #caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["automationName"]="uiautomator2"
        caps["appPackage"]="io.appium.android.apis"
        caps["appActivity"]=".ApiDemos"
        #caps["unicodeKeyboard"]='true'
        #caps["resetKeyboard"]='true'
        #caps["udid"]="192.168.59.102:5555"

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)

    def test_toast(self):
        self.driver.find_element_by_accessibility_id("Views").click()
        self.driver.find_element_by_android_uiautomator(
            'new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className("android.widget.TextView"), "Popup Menu")'
        ).click()
        self.driver.find_element_by_accessibility_id("Make a Popup!").click()
        self.driver.find_element_by_android_uiautomator('new UiSelector().text("Search")').click()
        print(self.driver.find_element_by_xpath("//*[@class='android.widget.Toast']").text)

```
## 浏览器

## WebView
使用UiautomatorViewer判断特定的控件的class是否是android.webkit.WebView,或者其他的继承于android.webkit.WebView的控件


```python
from appium import webdriver
import time
import unittest

from appium.webdriver.common.mobileby import MobileBy
from hamcrest import *

#配置程序入口信息
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By


class ContactsAndroidTests(unittest.TestCase):
    def setUp(self):
        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "hogwarts"
        caps['chromedriverExecutable']='/Users/seveniruby/projects/chromedriver/2.20/chromedriver'
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["automationName"]="uiautomator2"
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)

    def test_webview(self):
        self.driver.find_element_by_xpath("//*[@text='好的']").click()
        self.driver.find_element_by_xpath("//*[@text='交易']").click()

        for i in range(5):
            print(self.driver.contexts)
            print(self.driver.current_context)
            time.sleep(1)

        self.driver.switch_to.context("WEBVIEW_com.xueqiu.android")
        print(self.driver.page_source)
        self.driver.get("https://testerhome.com")
```


## 问题列表
- 1.9.1无法启动  https://github.com/appium/appium/issues/11477
- MobileBy.ANDROID_UIAUTOMATOR python不生效
- error: 'ms' must be a double

## 作业1
- 获取Uiautomator2引擎下appium启动所用到的所有命令,并挨个注释下每个命令的作用

## 作业2
完成一个app的覆盖升级测试

## 作业3
toast识别,点击Edit,打印出toast弹出的内容
关闭