Appium python 框架

转载地址:https://testerhome.com/topics/3460


希望给点意见和建议,毕竟周围没有人可以交流。。。

前言

嘿嘿,第一次发帖有点小激动。
接触appium也有一个多月了,自己根据以前做selenium的经验(其实只有一年不到!!!)搭建了框架,希望大家给点意见啊!!!毕竟我身边没有可以和我交流的!!!万分感谢

流程

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是读取配置文件

有点遗憾的地方

1.目前异常机制还不够完善,也怪我学艺不精
2.线程是先学的,线程安全之类也没有考虑,目前做到的仅仅是可以用了
3.测试数据参数化还没有实现(这个还要再研究)
4.重要的话说3遍:没有人交流!!!
没有人交流!!!
没有人交流!!!
希望大家在看完后,多多题意见

最后附上源码地址:https://github.com/tongshan1/appium_python

 本帖已被设为精华帖!
共收到  27 条回复
4bf98d35d61ca410925f6df65892012b
testerlin ·  #1 · 1 天前  喜欢 

鼓掌

4863
among29 ·  #2 · 1 天前  喜欢 

在第2点中,判断appium是否开启,我使用的是http get 这个url: http://127.0.0.1:4723/wd/hub/status ,可以获取到appium的状态,如果在执行中,还可以得到正在执行的手机的udid等信息。

第3点钟,杀进程没关系,不放心可以在案例的最后 delete session。


第一点根据你提供的方法我写出来了

def isStartServer():

    response = None
    url = baseUrl+"/status"
    try:
        response = urllib.request.urlopen(url, timeout=5)

        if str(response.getcode()).startswith("2"):
            print(response.getcode())
            return True
        else:
            return False
    except URLError:
        return False
    finally:
        if response:
            response.close()

第二点

我发现在case执行结束后有这么一句log
info: <-- DELETE /wd/hub/session/ebfae3a8-b604-47c1-bb58-17cf02b28b57 200 977.879 ms - 76 {"status":0,"value":null,"sessionId":"ebfae3a8-b604-47c1-bb58-17cf02b28b57"}
是不是意味着我不需要在delete session了呢?


### 回答1: Appium是一个开源的跨平台移动应用程序测试框架,使用Python作为编程语言,可以用来测试Android和iOS平台上的移动应用程序。它支持使用原生应用,移动web应用和混合应用进行自动化测试。 ### 回答2: Appium 是一款流行的开源移动应用自动化测试框架,它能够支持多种移动操作系统平台,例如 iOS 和 Android 。Appium 使用 WebDriver 协议来连接到移动设备并控制应用程序。Appium 通过传递各种指令来驱动测试用例,例如点击和滑动,它能够使我们自动化实现用户对移动应用程序的基本功能测试,例如登录,点击和提交表单,这些功能测试对于移动应用的发布至关重要。通过使用 Appium 框架,我们可以自动化测试我们的应用程序的不同功能,从而帮助我们提高应用程序的质量并加速其发布时间。 Appium python 自动化框架是使用 Python 编写的 Appium 框架Python 是一种易于学习且易于实现的编程语言,它在 web 开发和数据分析领域广受欢迎。带有 PythonAppium 框架可以使用 Python 编写的测试用例来调用 Appium API。这些测试用例能够以编程方式与移动应用程序交互并测试其各种功能,例如输入文本、点击按键、滚动,以及读取应用程序的状态等。Python Appium 框架的一个重要优点在于,它可以轻松地与其他 Python 库进行集成,例如测试框架和数据处理库,使我们能够更加高效地进行测试和数据处理。 在使用 Appium python 自动化框架时,需要具备以下技能:Python 编程语言,基本的测试用例设计(例如编写简单的测试用例)、了解 Appium 的功能和原理、熟悉应用程序和移动操作系统的表现方式。如果开发人员不熟悉这些技能,可以通过学习相关知识和参考开源社区的代码库,来帮助自己提高这些技能。 总结起来,Appium python 自动化框架是一个流行的移动应用测试自动化工具,可以通过 Python 编写测试用例来调用 Appium API。它不仅具有易于编写和执行的特点,而且能够轻松地集成到测试和数据处理库中,为移动应用开发人员提供了有力的支持。 ### 回答3: Appium Python自动化框架是一种用于移动应用程序测试的自动化框架。它基于Appium的开源代码库,使用Python语言编写,并可以与多种设备和平台集成,从而提供了一个稳定的、可靠的、易于使用的测试解决方案。 Appium Python自动化框架支持Android和iOS平台的应用,可以在真实设备和模拟器上进行测试。它提供的API接口简单易用,可以快速构建测试脚本,实现自动化测试。通过使用Python语言,测试人员可以轻松地编写可重复使用的、模块化的测试代码,这使得测试周期缩短,测试效率提高。 Appium Python自动化框架的主要特点包括下面几个方面: 1. 支持多种编程语言,包括Python、Java、Ruby和JavaScript等。 2. 可以与多种自动化测试工具集成,如Robot Framework和Selenium等,从而提高测试效率。 3. 支持多个应用测试,包括原生应用程序和混合应用程序,以及测试Web应用程序。 4. 具有良好的可扩展性和灵活性,可以根据需要添加自定义功能。 总之,Appium Python自动化框架是一个强大的移动应用程序自动化测试工具,能够帮助测试团队快速、准确地测试应用程序。它提供了一个通用的测试解决方案,可以在不同的平台和设备上进行多种类型的应用测试。如果你正在寻找一款高效的、易于使用的自动化测试工具,那么Appium Python自动化框架会是一个不错的选择。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值