🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 演示官方demo 作为一个新手,刚搭建好appium环境,让我们先来演示一下官方demo,让我们体验一下运行一个appium自动化的过程吧。 ### step1:启动安卓模拟器 本书使用“雷电模拟器”,启动后如下: ![](https://box.kancloud.cn/f8ed974e76e81e45ef02e1c156f96db8_486x444.jpg) 启动后,在命令行中检查adb能否连接上该设备。 ### step2: 启动Appium Desktop 启动后,如下: ![](https://box.kancloud.cn/c1a510070b76e793271c0530e53dd077_923x181.jpg) ### step3:准备自动化脚本与待测APK test_android_contacts.py : ```python #!/usr/bin/env python # -*- coding: utf-8 -*- import os import pytest from appium import webdriver # Returns abs path relative to this file and not cwd PATH = lambda p: os.path.abspath( os.path.join(os.path.dirname(__file__), p) ) APPIUM_LOCAL_HOST_URL = 'http://localhost:4723/wd/hub' PLATFORM_VERSION = '5.1.1' class TestWebViewAndroid(): @pytest.fixture(scope="function") def driver(self, request): desired_caps = { 'appPackage': 'com.example.android.contactmanager', 'appActivity': '.ContactManager', 'platformName': 'Android', 'platformVersion': PLATFORM_VERSION, 'deviceName': 'Android Emulator', 'app': PATH('ContactManager.apk') } driver = webdriver.Remote(APPIUM_LOCAL_HOST_URL, desired_caps) def fin(): driver.quit() request.addfinalizer(fin) return driver # provide the fixture value def test_add_contacts(self, driver): el = driver.find_element_by_accessibility_id("Add Contact") el.click() textfields = driver.find_elements_by_class_name("android.widget.EditText") textfields[0].send_keys("Appium User") textfields[2].send_keys("someone@appium.io") assert 'Appium User' == textfields[0].text assert 'someone@appium.io' == textfields[2].text driver.find_element_by_accessibility_id("Save").click() # for some reason "save" breaks things #alert = driver.switch_to.alert # no way to handle alerts in Android driver.find_element_by_android_uiautomator('new UiSelector().clickable(true)').click() driver.press_keycode(3) if __name__ == '__main__': pytest.main() ``` 本实例,使用官方的代码与apk,点击下载:[ContactManager.apk](https://github.com/appium-boneyard/sample-code/blob/master/sample-code/apps/ContactManager/ContactManager.apk) 将apk放到与代码相同的目录下 ### step4:运行测试代码 ![](https://box.kancloud.cn/7f36c794304980f48c23f6b76c569f4d_1193x971.gif) ## 分析Demo代码 1. 连接Appium服务器 在本实例中,appium server 连接获得WebDriver实例作为每个用例的初始化条件,放在pytest的fixture中。 代码如下截图 ![](https://box.kancloud.cn/48b7b235dbdb0bad7005bae5a1098bfe_676x338.jpg) 在fixture中,最关键的一句代码是初始化,获得WebDriver实例 `python driver = webdriver.Remote(APPIUM_LOCAL_HOST_URL, desired_caps)` 初始化时,需要指定**command_executor**,默认是“http://127.0.0.1:4444/wd/hub", 这里我们必须指定我们的4723端口,修改为“http://localhost:4723/wd/hub'” 同时,我们还需要通过**desired_capabilities**参数, 设置appium server启动时的参数,启动session的时候是必须提供的。它告诉appium server 本次测试是启动浏览器还是启动移动设备,是启动andorid还是启动ios,启动android时,app的package是什么,app的activity是什么等。 >分析这里的fixture代码之前,需要先掌握好pytest测试框架。这里 `request.addfinalizer(fin)` 表示用例teardown销毁操作。`return dirver` 表示用例setup操作的返回值是一个WebDriver驱动实例。 2. 元素定位 **根据 text 定位** `el = driver.find_element_by_accessibility_id("Add Contact")` ![](https://box.kancloud.cn/f04d490fd7167bcc9624a55e06140396_1050x725.jpg) 3. 元素操作 **点击元素** `el.click()` ## 分析Appium的加载流程 **通过分析Appium Server中的日志,分析Appium的加载流程** 1.创建会话Session,通过**desired_capabilities**设置appium server启动时的参数。 ``` [MJSONWP] Calling AppiumDriver.createSession() with args: [{"appPackage":"com.example.android.contactmanager","appActivity":".ContactManager","platformName":"Android","platformVersion":"5.1.1","deviceName":"Android Emulator","app":"E:\\workspace\\python_learn\\ContactManager.apk"},null,{"firstMatch":[{}],"alwaysMatch":{"appium:appPackage":"com.example.android.contactmanager","appium:appActivity":".ContactManager","platformName":"Android","appium:platformVersion":"5.1.1","appium:deviceName":"Android Emulator","appium:app":"E:\\workspace\\python_learn\\ContactManager.apk"}}] [BaseDriver] Event 'newSessionRequested' logged at 1537521688377 (17:21:28 GMT+0800 (中国标准时间)) [Appium] Creating new AndroidDriver (v2.7.0) session [Appium] Capabilities: [Appium] platformName: Android [Appium] appPackage: com.example.android.contactmanager [Appium] appActivity: .ContactManager [Appium] platformVersion: 5.1.1 [Appium] deviceName: Android Emulator [Appium] app: E:\workspace\python_learn\ContactManager.apk [BaseDriver] W3C capabilities {"alwaysMatch":{"platformNa... and MJSONWP desired capabilities [object Object] were provided [BaseDriver] Creating session with W3C capabilities: {"alwaysMatch":{"platformNa... [BaseDriver] Session created with session id: 6ec4b5b7-b79c-426c-87ce-017472a07294 ``` 2. 检查android adb环境,调用android adb 完成基本的系统操作 3. 向android上部署bootstrap.jar包并启动 4. Forward android的端口到pc机器上 5. pc上监听端口,接受请求,使用webdriver协议分析命令并通过forward的端口发送给bootstrap.jar 6. bootstrap.jar 接受命令并把命令发给uiautomator或者插桩体系。 **更多内容,请留意后面章节~**