当前位置:首页 » 《关注互联网》 » 正文

Python3 Appium自动化测试入门_weixin_43521305的博客

1 人参与  2022年01月16日 16:15  分类 : 《关注互联网》  评论

点击全文阅读


概述:主要是做App客户端相关自动化操作。实现自动化用例,通过pytest和allure框架完成自动化测试并输出测试报告。在客户端较稳定的情况下,可多次重复实现自动化回归。

一、环境搭建

操作系统:Mac 11.5.2

1、Pycharm //官网下载

2、Anaconda(Python3.8)//python --version

3、brew //brew --version

4、Android-sdk //adb --version

5、Appium //官网下载

6、Xcode //App store下载

其他包:

pip install Appium-Python-Client

pip install selenium

pip install -U pytest

pip install allure-pytest

brew install allure


pytest执行全部用例:

py.test test/ --alluredir ./result/

pytest执行部分用例:

py.test test/ --allure_features='购物车功能' --allure_stories='加入购物车'

allure生成报告:

allure generate ./result/ -o ./report/ --clean

二、构建Appium自动化服务器

概述:Appium原理类似Selenium,通过Appium服务器,以代码驱动去操作客户端软件

1、进入Appium

2、port:4723是安卓服务器,点击start Server

3、点击右上角放大镜(Start Inspector Session)

4、添加Desired Capabilities的参数,可参考:

{
  "platformName": "Android",	//平台
  "deviceName": "nova 5",	//设备名
  "appPackage": "com.xxx.xxx",	//要自动化的App包名
  "appActivity": "ui.activity.SplashActivity",	//框架,可不修改
  "noReset": true,
  "unicodeKeyboard": false,	//是否使用unicode打字法
  "resetKeyboard": true	//是否重制打字法
}

5、点击右下角save

6、连接手机至电脑,点击Start Session,在手机安卓Appium和Android-sdk插件。

三、编写自动化脚本

Start session后,连接成功的话,Appium会需要在手机上安装2个自动化测试相关软件。

 

Appium服务器会显示成这样。

 手机点击某一个app时,点击右上角刷新,Select Elements选择元素,右边Selector可获取元素的抓取方式,可通过xpath或id获取。

获取到元素ID或者Xpath路径,就可以开始编写Python自动化测试代码了。

我使用的是Pytest+Allure+Appium框架。

例如:

import allure
import pytest
from appium import webdriver

desired_caps = {
  'platformName': 'Android', # 被测手机是安卓
  'platformVersion': '10', # 手机安卓版本
  'deviceName': 'JPFDU19425xxxx', # 设备名,安卓手机可以随意填写
  'appPackage': 'com.xxx.xxxx', # 启动APP Package名称
  'appActivity': '.ui.activity.SplashActivity', # 启动Activity名称
  # 'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True
  'resetKeyboard': True, # 执行完程序恢复原来输入法
  'noReset': True,    
  'newCommandTimeout': 6000,
  'automationName' : 'UiAutomator2'
  # 'app': r'd:\apk\bili.apk',
}

class TestGloweAuton(object):
  def setup_method(self):	//用例初始化加载的方法
    self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)//获取driver
    print('用例开始执行')
  def teardown_method(self):	//用例结束时加载的方法
    print('用例执行完毕')
	
  def test_xxxx(self):	//测试用例,以test为开头
    driver = self.driver
    driver.find_element_by_id("com.xxx.xxx").click()//通过id获取元素
    driver.implicitly_wait(5)	//隐式等待获取新的元素

四、测试报告

pycharm点击下面terminal

pytest执行全部用例命令:

py.test test/ --alluredir ./result/

allure生成报告:

allure generate ./result/ -o ./report/ --clean

通过chrome打开index.html,观察测试报告结果


点击全文阅读


本文链接:http://zhangshiyu.com/post/33378.html

自动化  获取  执行  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1