【课程贴】app自动化训练营

参考链接

allure 官网地址:http://allure.qatools.ru/

开营仪式

adb常用命令

appium inspector

编写第一个测试用例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
# pip install appium-python-client
from time import sleep

import pytest
import yaml
from appium import webdriver

# pytest 单元测试框架
from appium.webdriver.common.mobileby import MobileBy

"""
改造1: 改造成pytest单元测试框架的模式
改造2: 加入setup,teardown
改造3: 将定位器改写成可维护型
改造4:  适当的加入断言来判断逻辑的正确性
改造5: 添加参数化与数据驱动,动态生成测试用例
"""

with open("./datas/searchkey.yml") as f:
    datas = yaml.safe_load(f)


class TestCart:
    def setup(self):
        caps = {}
        caps["platformName"] = "Android"
        caps["deviceName"] = "hogwarts"
        caps["appPackage"] = "com.dangdang.buy2"
        caps["appActivity"] = ".StartupActivity"
        caps["noReset"] = "True"
        caps['udid'] = 'emulator-5554'
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        # 隐式等待, 每次查找元素的时候,都会在5秒之内查找这个元素,每隔0.5秒 找一次,
        # 直到5秒结束都没找到的话,才抛出未找到元素的异常
        self.driver.implicitly_wait(5)

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

    @pytest.mark.parametrize('searchkey', datas)
    def test_case1(self, searchkey):
        # searchkey = "android"
        el1 = self.driver.find_element(MobileBy.ID, "com.dangdang.buy2:id/home_view_flipper")
        el1.click()
        el2 = self.driver.find_element(MobileBy.ID, "com.dangdang.buy2:id/et_search")
        el2.send_keys(searchkey)
        el3 = self.driver.find_element(MobileBy.XPATH,
                                       f"//*[@text='{searchkey}' and contains(@resource-id,'tv_sug')]")
        el3.click()
        el4 = self.driver.find_elements(MobileBy.ID,
                                        "com.dangdang.buy2:id/product_img_iv")[0]
        el4.click()
        el5 = self.driver.find_element(MobileBy.ID, "com.dangdang.buy2:id/tv_magic_btn_right")
        el5.click()
        # 强制等待,死等
        sleep(2)
        print(self.driver.page_source)
        ele = self.driver.find_element(MobileBy.XPATH, "//*[@class='android.widget.Toast']")
        assert '商品已成功加入购物车' == ele.text

allure 的安装

1、安装 allure , 不同的系统安装方式不同,参照官方文档
2、安装 allure-pytest

pip install allure-pytest

pytest 常用插件

  • pytest-ordering 用例顺序控制
  • pytest-dependency 用例依赖
    https://pytest-dependency.readthedocs.io/en/stable/

allure 运行

  • 生成中间结果文件,json,txt文件
    pytest test_a.py --alluredir result/

  • 打开结果文件
    allure serve ./result

  • 生成最终的报告
    allure generate ./result

[appium-desktop-1.12.1-x64.nsis.7z]文件从github上无法下载,速度太慢

建议下载1.15版本,学院论坛提供了下载链接。
windows下载链接:百度网盘 请输入提取码

密码: jlbg

#!/usr/bin/env python

-- coding:utf-8 --

This sample code uses the Appium python client

pip install Appium-Python-Client

Then you can paste this into a file and simply run with Python

import pytest
from time import sleep
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy

class Testdangdu:
def setup(self):
caps = {}
caps[“platformName”] = “android”
caps[“deviceName”] = “jjj”
caps[“appPackage”] = “com.dangdang.buy2”
caps[“appActivity”] = “.activity.ActivityMainTab”
caps[“noReset”] = “True”
caps[“ensureWebviewsHavePages”] = True
# 局部变量设置为实例变量用.self

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


    el1 = self.driver.find_element(MobileBy.ID ,"com.dangdang.buy2:id/tab_category_iv" )
    el1.click ()

    el3 = self.driver.find_element (MobileBy.XPATH,"//*[@text='书单' and @resource-id= 'com.dangdang.buy2:id/tv_tab_title']" )
    el3.click ()

    sleep(5)
    el4 = self.driver.find_elements (MobileBy.ID,"com.dangdang.buy2:id/rv_rich_discovery")[0]
    el4.click ()

    el5 =self. driver.find_element_by_id ( "com.dangdang.buy2:id/tv_attention" )
    el5.click ()
    # print(self.driver.page_source)
    el6 = self.driver.find_element(MobileBy.XPATH,"//*[@class = 'android.widget.Toast']")
    assert el6.text =='关注成功'
    el7 = self.driver.find_element_by_id ( "com.dangdang.buy2:id/tv_rich_detial_back" )
    el7.click ()
    # 点击右上角的小人
    el8 = self.driver.find_elements ( MobileBy.XPATH,"//*[@class = 'android.widget.TextView' and @resource-id= 'com.dangdang.buy2:id/etv_icon']" )[1]
    el8.click ()
    sleep(5)

    el10 = self.driver.find_element ( MobileBy.XPATH,
                                      "//*[@text='关注' and @resource-id= 'com.dangdang.buy2:id/tv_tab_fan_name']" )
    el10.click ()

    el9 = self.driver.find_element ( MobileBy.ID,
                                     "com.dangdang.buy2:id/v_split")
    el9.click ()
    sleep(5)
    el11 = self.driver.find_element ( MobileBy.ID,
                                     "com.dangdang.buy2:id/follow_tv" )
    el11.click ()

    el10 = self.driver.find_element(MobileBy.XPATH,"//*[@class = 'android.widget.Toast']")
    assert el10.text =='已取消关注'

测试用例写的很不错,定位元素的方式很灵活
可以试着加入参数化和数据驱动,动态的生成测试用例。
用例稍微有点复杂,复杂的功能可以适当的拆分成多条用例来实现

**使用appium 命令行运行报错,不知道怎么回事  pycharm报错关键信息:**
raise exception_class(message, screen, stacktrace)
E       selenium.common.exceptions.WebDriverException: Message: An unknown server-side error 
occurred while processing the command. Original error: Neither ANDROID_HOME nor 
ANDROID_SDK_ROOT environment variable was exported. Read 
https://developer.android.com/studio/command-line/variables for more details

根据报错信息 , ANDROID_HOME 这个环境变量没有配置。