安卓开发

Jenkins持续集成体系建设

2021-07-01  本文已影响0人  InsaneLoafer

目录

一、任务目标

二、 安卓App构建、打包、部署与自动化测试

涉及技术点

测试App构建、打包过程

安卓App部署

安卓UI自动化测试

测试源码

**iApp BVT自动化测试程序(Python版)**
运行环境:
- appium server
- python3
- unittest, pytest
- git

配置文件:iAppBVT_Python.json
- 将配置文件复制到本地磁盘
- 填入设备的 deviceName 与 udid

运行命令:
pytest -sv test/bvt_test.py --tc-file /full path/iAppBVT_Python.json --tc-format json
from appium import webdriver
import unittest
import time
from pytest_testconfig import config

timeout = 30 # 超时
poll = 2 # 轮询


class IAppBVT(unittest.TestCase):

    def setUp(self):
        desired_caps = {}
        appium_server_url = config['appium_server_url']
        desired_caps['platformName'] = config['desired_caps']['platformName']
        desired_caps['udid'] = config['desired_caps']['udid']
        desired_caps['deviceName'] = config['desired_caps']['deviceName']
        desired_caps['appPackage'] = config['desired_caps']['appPackage']
        desired_caps['appActivity'] = config['desired_caps']['appActivity']
        desired_caps['automationName'] = config['desired_caps']['automationName']
        desired_caps['noReset'] = config['desired_caps']['noReset']

        self.driver = webdriver.Remote(appium_server_url, desired_caps)

    def tearDown(self):
        self.driver.quit()

    def test_bvt(self):
        print('BVT test is started!')
        status = self.is_record_event_btn_exist()
        print(f'The record event button is exist - {status}')
        self.assertTrue(status, f'Check record_button result is {status}')
        print('Test finished!')

    def is_record_event_btn_exist(self):
        elem = self._find_elem_by_xpath('//android.widget.Button[contains(@resource-id,"id/trackEventButton")]')
        return elem is not None

    def _find_elem_by_xpath(self, elem_xpath, time_out=timeout, raise_exception=True):
        start = time.time()
        elem = None
        while time.time() - start < time_out and elem is None:
            time.sleep(poll)
            try:
                elem = self.driver.find_element_by_xpath(elem_xpath)
            except Exception:
                print('by pass the element not found')

        if elem is None and raise_exception:
            raise LookupError(f'The element which xpath is {elem_xpath} could not be found')

        return elem
{
    "desired_caps": {
        "platformName": "Android",
        "deviceName": "<your device name>",
        "udid": "<your device udid>",
        "noReset": "false",
        "automationName": "UiAutomator2",
        "appPackage": "com.appsflyer.androidsampleapp",
        "appActivity": ".MainActivity"
    },
    "appium_server_url": "http://localhost:4723/wd/hub"
}
allure-pytest
appium-python-client
pytest
pytest-testconfig
requests
selenium
urllib3

三、安卓App持续集成体系建设实战演练

实现流程图

image.png

安卓打包任务

image.png

安卓APP发布 输出apk文件

image.png

安卓app自动部署、测试任务

image.png

App打包任务与自动部署、测试任务关联

配置任务结果邮件通知

image.png

三、安卓App持续集成体系建设实战

创建打包任务

  1. 创建job


    image.png
  2. 选择任务运行的节点-master


    image.png
  3. 添加项目的GitHub地址,并复制项目名称


    image.png
  4. 将复制的项目名称复制到sub-directory


    image.png
  5. 添加shell命令

. ~/.bash_profile
cd AndroidSampleApp
sh gradlew clean assembleDebug
image.png
  1. 添加发布路径:AndroidSampleApp/app/build/outputs/apk/debug/app-debug.apk

    image.png
  2. 进行构建,查看控制台输出以及运行结果


    image.png
    image.png

创建自动化测试任务

  1. 创建任务


    image.png
  2. 选择打包的机器


    image.png
  3. 添加项目的GitHub地址,复制项目名称


    image.png
  4. 粘贴项目名称到sub-directory


    image.png
  5. 输入shell命令

{
    adb uninstall com.appsflyer.androidsampleapp
} | | {
    echo 'no app package installed on the device'
}
. ~/.bash_profilepwd= `pwd`
apk=$pwd/../1219_buildAndroidsample/AndroidSampleApp/app/build/outputs/apk/debug/app-debug.apk
{
    adb uninstall com.appsflyer.androidsampleapp
}||{
    echo 'no app package installed on the device'
}
adb install $apk
cd iAppBVT_Python
pip3.9 install -r requirements.txt
pytest -sv test/bvt_test.py --tc-file /Users/jizhiqian/iAppBvT_Python.json --tc-format json
image.png
  1. 构建job,进行运行


    image.png

配置消息通知

关联任务

构建打包任务会自动触发自动化测试脚本

image.png
image.png

四、课程总结

五、FAQ

如何做失败后的自动重试

image.png
上一篇下一篇

猜你喜欢

热点阅读