Appium python 框架

news/2025/2/12 0:42:30/

目录

前言

流程

结构

具体说说 run.py

思路

其他模块


前言

Appium是一个开源的移动应用自动化测试框架,它允许开发人员使用多种编程语言(包括Python)来编写自动化测试脚本。Appium框架提供了一套API和工具,可以与移动设备进行通信,并模拟用户在移动应用上的操作。

流程

1.打开 appium server
2.获取当前手机的 device Name 和 安卓版本号,打开 driver
3.运行 case
4.生成报告
5.关闭 driver
6.关闭 appium server

结构

具体说说 run.py

整个程序是从这个模块开始运行的,也是花了我最长时间的地方。下面上代码

# ========================================================
# Summary        :run
# Author         :tong shan
# Create Date    :2015-10-09
# Amend History  :
# Amended by     :
# ========================================================import readConfig
readConfigLocal = readConfig.ReadConfig()
import unittest
from testSet.common.DRIVER import myDriver
import testSet.common.Log as Log
import os
from time import sleepfrom selenium.common.exceptions import WebDriverException
import threadingmylock = threading.RLock()
log = Log.myLog.getLog()# ========================================================
# Summary        :myServer
# Author         :tong shan
# Create Date    :2015-10-10
# Amend History  :
# Amended by     :
# ========================================================
class myServer(threading.Thread):def __init__(self):global appiumPaththreading.Thread.__init__(self)self.appiumPath = readConfigLocal.getConfigValue("appiumPath")def run(self):log.outputLogFile("start appium server")rootDirectory = self.appiumPath[:2]startCMD = "node node_modules\\appium\\bin\\appium.js"#cd root directory ;cd appiuu path; start serveros.system(rootDirectory+"&"+"cd "+self.appiumPath+"&"+startCMD)# ========================================================
# Summary        :Alltest
# Author         :tong shan
# Create Date    :2015-10-10
# Amend History  :
# Amended by     :
# ========================================================
class Alltest(threading.Thread):def __init__(self):threading.Thread.__init__(self)global casePath, caseListLpath, caseList, suiteList, appiumPathself.caseListPath = readConfig.logDir+"\\caseList.txt"self.casePath = readConfig.logDir+"\\testSet\\"self.caseList = []self.suiteList = []self.appiumPath = readConfigLocal.getConfigValue("appiumPath")# =================================================================
# Function Name   : driverOn
# Function        : open the driver
# Input Parameters: -
# Return Value    : -
# =================================================================def driverOn(self):myDriver.GetDriver()# =================================================================
# Function Name   : driverOff
# Function        : colse the driver
# Input Parameters: -
# Return Value    : -
# =================================================================def driverOff(self):myDriver.GetDriver().quit()# =================================================================
# Function Name   : setCaseList
# Function        : read caseList.txt and set caseList
# Input Parameters: -
# Return Value    : -
# =================================================================def setCaseList(self):print(self.caseListPath)fp = open(self.caseListPath)for data in fp.readlines():sData = str(data)if sData != '' and not sData.startswith("#"):self.caseList.append(sData)# =================================================================
# Function Name   : createSuite
# Function        : get testCase in caseList
# Input Parameters: -
# Return Value    : testSuite
# =================================================================def createSuite(self):self.setCaseList()testSuite = unittest.TestSuite()if len(self.caseList) > 0:for caseName in self.caseList:discover = unittest.defaultTestLoader.discover(self.casePath, pattern=caseName+'.py', top_level_dir=None)self.suiteList.append(discover)if len(self.suiteList) > 0:for test_suite in self.suiteList:for casename in test_suite:testSuite.addTest(casename)else:return Nonereturn testSuite# =================================================================
# Function Name   : runTest
# Function        : run test
# Input Parameters: -
# Return Value    : -
# =================================================================def run(self):try:while not isStartServer():mylock.acquire()sleep(1)log.outputLogFile("wait 1s to start appium server")mylock.release()else:log.outputLogFile("start appium server success")suit = self.createSuite()if suit != None:log.outputLogFile("open Driver")self.driverOn()log.outputLogFile("Start to test")unittest.TextTestRunner(verbosity=2).run(suit)log.outputLogFile("end to test")log.outputLogFile("close to Driver")self.driverOff()else:log.outputLogFile("Have no test to run")except Exception as ex:log.outputError(myDriver.GetDriver(), str(ex))def isStartServer():try:driver = myDriver.GetDriver()if driver == None:return Falseelse:return Trueexcept WebDriverException:raiseif __name__ == '__main__':thread1 = myServer()thread2 = Alltest()thread2.start()thread1.start()while thread2.is_alive():sleep(10)#"allTest is alive,sleep10"else:#kill myServeros.system('taskkill /f /im node.exe')log.outputLogFile("stop appium server")

思路

刚接触的时候发现每次都要手动打开 appium 服务,然后再运行代码,想着是不是太麻烦了?就想试着可不可做成一步?
这一想,就费了我好多功夫。
1.打开 appium server
开始的时候,连如何用命令行打开服务都不会,在群里问了一圈(感谢回答问题的大大们!!!),然后自己又琢磨了一下,搞定了,可是!!!用 os.system(),居然阻塞进程,最后还是问了群里的大大解决(再次感谢!!!!)。
2.如何判断 server 是否被成功开启
这个问题我想了很久,现在我解决了,但是解决方法我不太满意,希望有大大可以给我一个好点的方法或者思路(跪谢!!)
目前做法:判断是否可以返回一个正常的 driver 对象

def isStartServer():try:driver = myDriver.GetDriver()if driver == None:return Falseelse:return Trueexcept WebDriverException:raise

3.关闭 appium server
目前的做法是强制杀死,还是不太满意,不知道以后会不会有什么不可预见的问题,希望有大大可以给我一个好点的方法或者思路(跪谢!!)

if __name__ == '__main__':thread1 = myServer()thread2 = Alltest()thread2.start()thread1.start()while thread2.is_alive():sleep(10)#"allTest is alive,sleep10"else:#kill myServeros.system('taskkill /f /im node.exe')log.outputLogFile("stop appium server")

其他模块

1.common.py 共同方法,主要指封装了一些方法,供其他模块使用。
2.DRIVER.py 获取 driver,这里是做成了一个单例模式,run.py 中打开,关闭,其他模块调用。
3.Log.py 中有 2 个类 log 和 myLog,同样也把 myLog 做成了一个单例模式
4.myPhone.py 主要使用了 adb 命令来识别和获取手机参数
5.readConfig.py 是读取配置文件

  作为一位过来人也是希望大家少走一些弯路

在这里我给大家分享一些自动化测试前进之路的必须品,希望能对你带来帮助。

(WEB自动化测试、app自动化测试、接口自动化测试、持续集成、自动化测试开发、大厂面试真题、简历模板等等)

相信能使你更好的进步!

点击下方小卡片


http://www.ppmy.cn/news/902073.html

相关文章

python爬虫02 - 爬虫请求模块 request库 json数据

1. urllib.request模块 先用之前学到过的方法解决 第一种方法 先创建一个爬取图片.py 在新标签页中打开图片 这就是该图片的url import requests#图片的url url=https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&a…

原来Kubernetes部署如此简单,看完全明白了

制作镜像 制作镜像分为三步: 第一步基础镜像,是基于哪个操作系统,比如CentOS 7或者其他的第二步中间件镜像,比如服务镜像,跑的像Nginx服务,Tomcat服务第三步项目镜像,它是服务镜像之上的&#x…

京东关键词搜索接口,item_search - 根据关键词取京东商品列表接口接入解决方案

一、京东关键词搜索接口,item_search - 根据关键词取京东商品列表接口解决方案 点击注册获取key和secret测试账号 提取京东关键词搜索商品详情页各项数据,包含skuid、价格、收藏数、加购数、月销售量、主图、标题、详情页图片等参数。 二、建议使用场景 1、商品销售情况分析,…

HTML(完结)

学习目标: 了解常用浏览器掌握WEB标准理解标签语义化掌握常用的排版标签掌握常用的文本格式化图像链接等标签掌握三种列表标签掌握表格标签掌握表格标签掌握表单标签 typora-copy-images-to: media 自我介绍 传智讲师 刘晓强 江湖人称 强哥 但是不是 也不是: 其实…

nginx常见问题处理

nginx常见的问题: 1.如果客户端访问服务器提示“Too many open files”如何解决 2.如何解决客户端访问头部信息过长的问题 3.如何让客户端浏览器缓存数据 4.如何自定义返回给客户端的404错误页面 5.如何查看服务器状态信息 7.如何开启gzip压缩功能&#xff0c…

深入了解epoll模型 -- 开卷有益

希望打开此篇对你有所帮助。 文章目录 什么是epoll?或者说,它和select有什么判别?什么是select为什么select最大只允许1024?什么是epollepoll设计思路简介 select、epoll原理图epoll一定优于select吗?什么时候不是&…

京东API接口:(item_search - 按关键字搜索商品)

以下是行业内了解到的一些情况,本帖只展示部分代码,需要全部参数以及更多API调试请移步注册API账号 http://console.open.onebound.cn/console/?ilucy 响应示例: {"items": {"url": "https://search.jd.com/Searc…

按关键词搜索京东商品列表接口,关键词搜索京东商品接口,京东列表接口,京东关键词搜索商品接口接入方案

一、关键词搜索京东商品列表接口参数说明: 通过关键词搜索京东商品列表,可以获取商品宝贝ID,商品详情页,商品标题,宝贝图片,价格,优惠价,销量,商品风格标识ID,物流费用&a…