zoukankan      html  css  js  c++  java
  • appium快速入门

    appium快速入门

    使用uiautomatorviewer定位工具

    第一步:启动安卓模拟器

    本书使用“雷电模拟器”,启动后如下:


    启动后,在命令行中检查adb能否连接上该设备。

    步骤2:启动Appium桌面

    启动后,如下:

    step3:准备自动化脚本与待测APK

    test_android_contacts.py:

    #!/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
    将apk放到与代码相同的目录下

    step4:运行测试代码

    分析演示

    1. 连接Appium服务器
      在本实例中,appium服务器获得WebDriver实例作为每个用例的初始化条件,放在pytest的fixture中。
      代码如下截图

      在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服务器启动时的参数,启动会话的时候是必须提供的。它告诉appium服务器本次测试是启动浏览器还是启动移动设备,是启动andorid还是启动ios,启动android时, app的package是什么,app的activity是什么等。

    分析之前的夹具代码之前,需要先掌握好pytest测试框架。这里request.addfinalizer(fin)表示用例teardown销毁操作。return dirver表示用例setup操作的返回值是一个WebDriver驱动实例。

    1. 元素定位
      根据text定位
      el = driver.find_element_by_accessibility_id("Add Contact")

    2. 元素操作
      点击元素
      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
    1. 检查android adb环境,调用android adb 完成基本的系统操作
    2. 向android上部署bootstrap.jar包并启动
    3. Forward android的端口到pc机器上
    4. pc上监听端口,接受请求,使用webdriver协议分析命令并通过forward的端口发送给bootstrap.jar
    5. bootstrap.jar 接受命令并把命令发给uiautomator或者插桩体系。
  • 相关阅读:
    团队冲刺六
    团队冲刺五
    【Mybaits学习】03_ CRUD基于注解
    【Mybaits学习】02_ 快速入门
    【Mybaits学习】01_ 初识
    深圳国际马拉松
    深圳南山半程马拉松
    Selenide使用笔记
    UI自动化测试01-环境搭建
    Java C3p0在Spring中运用
  • 原文地址:https://www.cnblogs.com/huny/p/11959786.html
Copyright © 2011-2022 走看看