Appium python 框架

news2024/9/22 19:39: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 sleep

from selenium.common.exceptions import WebDriverException
import threading

mylock = 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 appiumPath
        threading.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 server
        os.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, appiumPath
        self.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 None

        return 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 False
        else:
            return True
    except WebDriverException:
        raise


if __name__ == '__main__':

    thread1 = myServer()
    thread2 = Alltest()

    thread2.start()
    thread1.start()

    while thread2.is_alive():
        sleep(10)#"allTest is alive,sleep10"
    else:
        #kill myServer
        os.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 False
        else:
            return True
    except 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 myServer
        os.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.coloradmin.cn/o/759226.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C语言——指针详解(初阶)

轻松学会C语言指针 前言:一、指针是什么?1.1 指针是什么?1.2 指针变量1.3 总结 二、指针和指针类型2.1指针-整数2.2 指针的解引用 三、野指针3.1野指针的成因3.2如何避免野指针 四、指针运算4.1 指针-整数4.2指针-指针4.3指针的关系运算 五、…

【学会动态规划】不同路径(5)

目录 动态规划怎么学? 1. 题目解析 2. 算法原理 1. 状态表示 2. 状态转移方程 3. 初始化 4. 填表顺序 5. 返回值 3. 代码编写 写在最后: 动态规划怎么学? 学习一个算法没有捷径,更何况是学习动态规划, 跟我…

Delete `␍`eslint(prettier/prettier)报错的终极解决方案

1.背景 在进行代码仓库clone打开后,vscode报错全屏的 Delete ␍eslint(prettier/prettier)问题 2. 解决方案: 1.vscode直接转化 好处:直接转化当前页面的报错 坏处:每个界面都需要来一遍 2.设置git配置 好处:一…

竞赛信息管理系统——SSM

目录 一、项目简介 二、前置配置 1、创建数据库 2、编写application.yml文件 三、公共基础类 1、自定义登录拦截器类 2、自定义拦截规则 3、统一数据返回类 4、统一异常处理类 5、工具类 a、密码工具类 b、时间工具类 6、全局变量 四、用户模块 1、定义…

echarts环形图两层

1、实现效果 环形图&#xff0c;有两层环形&#xff0c;扇形之间有间隔&#xff0c;中间是标题&#xff0c;图例是自定义图片 2、实现 在template里写一个盒子放图表 <div class"chartMachineStyle" ref"chartMachine"></div>在style里设置盒…

状态模式:游戏、工作流引擎中常用的状态机是如何实现的?

从今天起&#xff0c;我们开始学习状态模式。在实际的软件开发中&#xff0c;状态模式并不是很常用&#xff0c;但是在能够用到的场景里&#xff0c;它可以发挥很大的作用。从这一点上来看&#xff0c;它有点像我们之前讲到的组合模式。 可以简短的回顾一下组合模式&#xff1a…

Windows cmd窗口下的代码页

查看当前的活动代码页 在cmd窗口下执行命令chcp可以查看当前的活动代码页&#xff1a; 临时修改活动代码页 在cmd窗口下执行命令chcp [nnn]&#xff0c;可以临时修改活动代码页&#xff08;窗口关闭后修改就失效了&#xff09;&#xff0c;其中[nnn]表示具体的代码页标识符…

Java 中的反射是什么?如何使用它?

Java 中的反射是什么&#xff1f;如何使用它&#xff1f; 在 Java 编程中&#xff0c;反射是一种高级的编程技术&#xff0c;可以在运行时动态地获取和操作类的信息。反射使得程序可以在运行时对类进行检查和操作&#xff0c;而不需要在编译时知道类的完整信息。这使得程序可以…

flstudio怎么保存工程文件?详解FL Studio 21保存文件的方法

FL Studio 21全称Fruity Loops Studio2023&#xff0c;这款软件也被人们亲切的称之为水果&#xff0c;它是一款功能强大的音乐创作编辑软件&#xff0c;拥有全功能的录音室&#xff0c;大混音盘以及先进的音乐制作工具&#xff0c;用户通过使用该软件&#xff0c;就可以轻松制作…

Ubuntu下搭建Redis分片集群

目录 准备实例和配置 启动分片集群 测试分片集群 分片集群需要的节点数量较多&#xff0c;搭建一个最小的分片集群&#xff0c;包含3个master节点&#xff0c;每个master包含一个slave节点&#xff0c;并且master之间通过心跳机制互相监听&#xff0c;此模式下不需要哨兵监听…

js高级进阶:promise同步编程技巧

promise是ES6引进的异步编程解决方案&#xff0c;是一个构造函数&#xff0c;可以实例化对象&#xff0c;可以解决回调地狱的问题。 首先我们看一下promise的实例化对象是什么&#xff1a; let P new Promise(function(){});//new一个promise传入一个函数console.log(P);打印结…

读发布!设计与部署稳定的分布式系统(第2版)笔记23_互联层之DNS

1. 互连层是可以真正构建高可用性的地方 1.1. 流量管理 1.2. 负载均衡 1.3. 服务发现 2. 不同规模的解决方案 2.1. 在小公司中 2.1.1. 只有少数开发人员的小企业可以直接使用DNS条目 2.1.2. 生成变更的开发人员较少&#xff0c;变更频度变低 2.1.3. 可能根本就没有独立…

高阶C语言|指针的进阶

指针的主题&#xff0c;在指针初阶阶段&#xff0c;我们知道了指针的概念&#xff1a; 1.指针就是个变量&#xff0c;用来存放地址&#xff0c;地址唯一标识一块内存空间。 2.指针的大小是固定4/8个字节&#xff08;32为平台/64位平台&#xff09;。 3.指针是有类型&#xff0c…

java+springboot基于云的学习笔记系统设计与开发 _44va6

学习笔记系统按照权限的类型进行划分&#xff0c;分为管理员和用户共两个模块。系统实现登录、个人信息修改&#xff0c;还可以对个人中心&#xff0c;用户管理&#xff0c;笔记本管理&#xff0c;笔记分享管理&#xff0c;分享类型管理&#xff0c;学习资料管理&#xff0c;购…

Makefile:10分钟带你了解makefile

1、Makefile是什么 在Linux系统中&#xff0c;Makefile是一个脚本文件&#xff0c;通常名为Makefile或者makefile&#xff0c;它使得程序员能够快速便捷地完成调用程序、编译代码、定位故障等工作。 Makefile是一个用于自动化构建和编译程序的脚本文件。它包含了程序的所有源…

Ubuntu下配置Redis哨兵集群

目录 准备实例和配置 启动哨兵集群 测试配置 搭建一个三节点形成的Sentinel集群&#xff0c;来监管Redis主从集群。 三个sentinel哨兵实例信息如下&#xff1a; 节点IPPORTs1192.168.22.13527001s2192.168.22.13527002s3192.168.22.13527003 准备实例和配置 要在同一台虚…

01. Docker基础环境构建

目录 1、前言 2、关于Docker 2.1、几个术语 2.2、Docker容器化的价值 3、搭建基础环境 3.1、安装VMware 3.2、安装Doker 3.3、启动 3.4、验证Docker环境 4、小结 1、前言 在这里我们将学习关于Docker的一些技能知识&#xff0c;那么首先我们应该怼Docker有一个基础的…

服务器离线部署docker,镜像迁移,mysql主从搭建等服务

公司项目要上线项目&#xff0c;买了两台云服务器&#xff0c;需进行环境部署&#xff08;1台接入公网&#xff0c;一台只能局域网访问&#xff09;&#xff0c;主要部署以下内容 1、服务器之间配置ssh免密 2、离线docker部署 3、docker镜像迁移 4、redis服务 5、minio文件…

Idea配置Maven优先从本地仓库获取依赖

idea配置maven依赖优先从指定本地仓库获取 在设置中搜索 Runner ,在VM Option中设置参数-DarchetypeCataloginternal <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http…

沐风老师MaxScript快速入门教程

Maxscript是将自定义3dMax应用的更强大的方法之一。结合内置的侦听器和编辑器&#xff0c;我们在运行时操作和测试代码&#xff0c;使其成为用户试验和探索改进软件体验的强大选项。通过Maxscript&#xff0c;我们几乎可以操作软件中的每一个对象&#xff0c;包括但不限于&…