Giter VIP home page Giter VIP logo

appium_test's People

Contributors

hanhan1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

appium_test's Issues

Appium环境搭建

一.必备环境

  1. python node.js android studio/genymotion(安卓模拟器,及sdk)
  2. 配置好的jdk androidhome
  3. 编辑器 pycharm
  4. 命令行 gitbash

二.安装appium

  1. 用npm命令
    npm install -g appium
    npm 是node包管理器和分发工具,有npm可以对需要安装的包进行下载安装,及管理
    若无npm命令则先装node.js,再配置环境变量,直到在你的gitbash上运行

    说明node环境安装配置成功
    接下去使用npm下载appium安装包
    在windows上下载很慢且通常都下载不下来(卡住且最后返回错误)

  2. 下载安装包(推荐)
    由于Appium是基于.NET开发的,所以要依赖 . NET Framework相关组件,若需要升级.NET Framework
    百度安装最新版即可完成安装
    最后appium安装完成的界面是

    appium安装完成

    配置appium的环境变量到path中

Appium-test项目详解

定位元素

定位元素是自动化测试必不可少的工作,在pytest中是用一些框架能够使定位元素变得简单易行,代码也更加清晰。
PyYAML用于解析yaml文件
pip install PyYAML
watchdog 用于监听某文件是否发生变化,一旦发生变化就执行回调
pip install watchdog
JingJa2 用于生成模板代码
pip install Jinja2

项目的代码结构

Apk文件夹放测试的apk文件
Data/page.yaml定位元素的文件
Data/config.ini需要修改 name 与 account中的内容
Test/conftest.py setup&teardown操作

编写步骤

  1. 开启appium服务
  2. 开启模拟器安装apk
  3. 开启uiautomatorviewer编写pages.yaml文件
cd Library/Android/sdk/tools/bin
./uiautomatorviewer

  1. 编写pages.yaml文件之前先开启watch_dog
    python watch_dog.py
    然后针对界面元素的信息完成pages.yaml文件


保存文件后就会在page/pages.py中生成控件元素

5.编写用例

在test/目录下新建一个test_home文件

  1. 运行python run.py


用例就跑起来了,跑完后,会在report/html中生成测试报告

Python测试框架pytest

Python 有多个测试框架如unittest doctest pytest nose
选择pytest是因为它容易上手,且能够支持简单的单元测试和复杂的功能测试,有很多的第三方插件可以自定义扩展,支持allure

安装pytest

进入命令行pip install pytest
或者通过pycharm的插件安装

Pytest测试样例的命名规则

  1. 测试文件以test_开头或结尾(否则用py.test命令行不能自动识别)
  2. 测试类以Test开头,且不能带有_init_方法
  3. 测试函数以test_开头
  4. 断言使用assert
  5. fixture的文件名必须是conftest.py

举例说明上述规则

新建一个pytest-sample项目
项目结构如下图所示


打开gitbash到该项目目录下
运行 py.test

可以看到只有test_sample.py 的文件两个测试用例运行了,sample.py却没有运行
如果我们指定运行的是sample.py,才会执行此测试用例

Fixture是pytest特有的功能,用pytest.fixture标识,定义在函数前面,在编写测试函数的时候,可以将此函数名称作为传入参数,pytest将会以依赖注入的方式,将函数的返回值当作参数传递给测试函数

这个函数指定了fixture的初始化规则

@pytest.fixture(scope=”session”)

表示全局初始化一次,用于全局系统的初始化

@pytest.fixture(scope=”module”)

表示一个模块只初始化一次,如果一个模块使用多次该fixture,将会使用同个对象

@pytest.fixture(scope=”function”)

表示每个function都初始化一次

上面的截图表示只全局化初始一次count,则在test_sample.py中两个测试用例中的两个count 只会被初始一次
可以使用py.test –fixture test_module.py 查看绑定的fixture

Fixture的自动执行autouse
需要某些fixture在全局自动执行,如某些全局变量的初始化操作,或者全局化的清理或者初始化函数
@pytest.fixture(session=”session”,autouse=true)

Pytest 的 fixture的存在使得我们在编写测试函数的准备函数、销毁函数或者多个条件的测试提供了更加灵活的选择。

参考链接https://docs.pytest.org/en/latest/contents.html

Allure测试报告与Jenkins集成

Allure 官方介绍http://allure.qatools.ru/
Allure 生成的报告不仅美观且方便CI集成
这是项目最后的报告截图

安装adapter

pip install pytest-allure-adapter
生成报告
pytest -s -q --alluredir [path_to_report_dir]
这时候就会发现用例执行完成后会在当前目录下生成了一个report文件

生成report

allure generate report/ -o report/html
最终报告会生成在report/html目录下
打开index.html即可看报告

定制report

让生成报告的可读性更高,可以用一些api来实现

@allure.step 和 @allure.attach 可以让生成的报告更直观(可理解为注释)

@allure.step在报告中增加步骤显示
@allure.attach在报告中增加额外的信息:allure.attach(’arg1’,’arg2’,’arg3’):
arg1:是在报告中显示的名称
arg2:表示添加的内容
arg3:表示添加的类型(支持类型:HTML,JPG,PNG,JSON,OTHER,TEXTXML)
往报告中添加额外的信息
allure.attach(‘this is an attach’,’aaaaa’)
往报告中添加图片

F= open(‘./b.png’,’rb’).read()
Allure.attach(‘this is a img’,f,allure_type.PNG)

Severity标注bug的严重等级,可以针对某个等级执行用例,其他等级都跳过,也可将等级高的bug优先修复
如果希望只跑critical和blocker这两个等级的case
在原先命令上加上--allure_severities=critical,blocker参数
py.test --alluredir report --allure_severities=critical,blocker -s –q
featuresstories 与severity一样可以将case分类
只执行部分 feature/stories 的话,用下面命令
py.test --alluredir report --allure_features=feature1,feature2 --allure_stories=story1,story2

集成Jenkins

参考链接

安装插件


jenkins 安装插件 Allure Jenkins Plugin HTML Publisher plugin
在系统设置中将JDK / Maven 等其他一些基本配置(建议jdk版本1.8)

在jenkins中添加allure执行工具

jenkins添加allure执行工具

  1. 下载 allure执行的压缩文件(allure-commandline.zip)
  2. 用tomcat搭建一个简单的服务器将allure-commandline.zip当作静态资源提供下载 (http://localhost:8080/allure-commandline.zip)
  3. 在jenkins的系统配置—–Allure Commandline —-选择自动安装—–选择解压zip—-填写提供下载zip包的url地址
  4. 填写其他必填项
  5. 点击保存

新建job

在job中添加步骤 Allure report(Results:第一行表示xml文件的路径;第二行表示生成报告的路径)
目前job的整个流程:

  1. 构建时,从svn上拉取最新的测试代码
  2. 执行测试脚本并且生成allure需要xml结果文件
  3. 通过Allure report生成测试报告
  4. 设置问题追踪(在Allure Report 下选择增加:

Key: allure.issues.tracker.pattern Value: http://tracker.company.com/%s)

由于jenkins1.5.2以上版本对插件的安全性做了限制必须要在jenkins 启动时添加启动代码

java 
-Dhudson.model.DirectoryBrowserSupport.CSP="default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';" 
-Djenkins.model.DirectoryBrowserSupport.CSP="default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';"
-jar jenkins.war

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.