前提

最近在学习 appium,一直想做一个自动化平台,达到数据驱动,测试案例等这些数据的分离。打算分三步来学习:

熟悉 appium 的基本使用方法,可以编写简单自动化脚本

appium 的数据分离。比如用 excel 管理数据驱动

appium 的自动化平台,所有数据存放在数据库
好了废话少说,我直接上代码:

testData 数据准备

driver.ini
[DEFAULT]
devices=SKMFFQPFYSEYEULZ
platformName=android
platformVersion=4.1
appPackage=com.tencent.mm
appActivity=.ui.LauncherUI
Remote=http://localhost:4723/wd/hub

BaseGetdriver.py
#获取app的信息。设备名,测试的app的包名,appium 服务器的信息
import configparser
from testData.getDir import get_driver_ini
class getDriver():
    def __init__(self):
        config = configparser.ConfigParser()
        # config.read('d:\driver.ini'))
        config.read(get_driver_ini('driver.ini'))
        self.devices = config['DEFAULT']['devices']
        self.platformName = config["DEFAULT"]['platformName']
        self.platformVersion = config['DEFAULT']['platformVersion']
        self.appPackage = config['DEFAULT']['appPackage']
        self.appActivity = config['DEFAULT']['appActivity']
        self.Remote = config['DEFAULT']['Remote']
    def get_plaetformName(self):
        print(self.platformName)
        return self.platformName
        ......
gobal.py 扩展
from testData.BaseGetdriver import *
# 查找元素的方式
class findElemtType():
    FINDNAME = "name"
    FINDID = "id"
    XPATH = "xpath"

# 操作元素的类型
class operateType():
    CLICK = "click"

# 为了后面的扩展
class devicesinfo():
    gd = getDriver()
    platformVersion = gd.get_platformVersion()
    devices = gd.get_devices()
    appActivity = gd.get_appActivity()
    remote = gd.get_Remote()
    appPackage = gd.get_appPackage()
    plaetformName = gd.get_plaetformName()

BaseOperateElement.py 操作界面元素
# 此脚本主要用于查找元素是否存在,操作页面元素
# cts 传入的driver
# operate_type 操作类型。比如点击,下拉,拖动等等,
# element_type 格式:(查找类型,查找的具体内容)-》(id,name,xpath)
# element_info 是具体的元素 ("通讯录",com.android.calculator2:id/digit6",/android.widget.TextView[contains(@text,'Add note'

class getOperateElement():
    def __init__(self, driver="", element_type="", element_info="", operate_type=None):
        self.operate_type = operate_type
        self.cts = driver
        self.element_type = element_type
        self.element_info = element_info
    def findElement(self):
        try:
            if self.element_type == findElemtType.FINDNAME:
                WebDriverWait(self.cts.driver, 10).until(lambda x: x.find_element_by_name(self.element_info))
                return True
            if self.element_type == findElemtType.FINDID:
                WebDriverWait(self.cts.driver, 10).until(lambda x: x.find_element_by_id(self.element_info))
                return True
            if self.element_type == findElemtType.XPATH:
                WebDriverWait(self.cts.driver, 10).until(lambda x:x.find_element_by_xpath(self.element_info))
                return True
        except selenium.common.exceptions.TimeoutException:
            return False
    def operateElement(self):
        if self.element_type == findElemtType.FINDID:
            elements_index_operate_id(self.operate_type, self.cts, self.element_info)
        if self.element_type == findElemtType.FINDNAME:
            elements_index_operate_name(self.operate_type, self.cts, self.element_info)
        if self.element_type == findElemtType.XPATH:
            elements_index_operate_xpath(self.operate_type, self.cts, self.element_info)

#元素属性为id的操作
def elements_index_operate_id(index, cts, element_info):
    elements = {
        operateType.CLICK:lambda:cts.driver.find_element_by_id(element_info).click()
    }
    return elements[index]()
#元素为name属性的操作
def elements_index_operate_name(index, cts, element_info):
    elements = {
        operateType.CLICK:lambda:cts.driver.find_element_by_name(element_info).click()
    }
    return elements[index]()

def elements_index_operate_xpath(index, cts, element_info):
    print(element_info)
    elements = {
        operateType.CLICK:lambda:cts.driver.find_element_by_xpath("//android.widget.ListView/android.widget.LinearLayout[@index='7']").click()
    }
    return elements[index]()

testCase

BaseTestCase.py
class getBaseTestCase(object):
# 测试案例的基类
# operate_type 操作类型。比如点击,下拉,拖动等等,
# element_type (查找类型:name/id/xpah)
 # element_info(具体的详情:“/android.widget.TextView[contains(@text,'Add note'"))
    def __init__(self, element_type, element_info, operate_type):
        self.operate_type = operate_type
        self.element_types = element_type
        self.element_info = element_info
    def get_operate_type(self):
        return self.operate_type

    def get_element_types(self):
        return self.element_types

    def get_element_info(self):
        return self.element_info

testDriver 测试案例编写

weChatHome.py
from testCase.BaseTestCase import *
from testData.Global import *
def chatHomeClick():
   return getBaseTestCase(findElemtType.XPATH, "//android.widget.ListView/android.widget.LinearLayout[@index='7']", operateType.CLICK)

testRunner 测试运行

ch=chatHomeClick()
class ContactsAndroidTests(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = devicesinfo.plaetformName
        desired_caps['platformVersion'] = devicesinfo.platformVersion
        desired_caps['deviceName'] = devicesinfo.devices
        desired_caps['appPackage'] = devicesinfo.appPackage
        desired_caps['appActivity'] = devicesinfo.appActivity

        self.driver = webdriver.Remote(devicesinfo.remote, desired_caps)
    def tearDown(self):
        self.driver.close_app()
        self.driver.quit()
    def test_001(self):
        if getOperateElement(driver=self, element_type=ch.get_element_types(), element_info=ch.get_element_info()).findElement():
            getOperateElement(driver=self, element_type=ch.get_element_types(), element_info=ch.get_element_info(), operate_type=ch.get_operate_type()).operateElement()
        else:
           print("没有找到页面元素")
if __name__ == '__main__':
   suite = unittest.TestSuite()
    suite.addTest(ContactsAndroidTests("test_001"))
    unittest.TextTestRunner(verbosity=2).run(suite)

总结讨论

1.测试结果暂时还没有想好用什么,打算用 pyh 手动拼接 html 页面的方式。这样比较灵活,自定义样式
2.测试案例我这里真的没有想到好方法,RF 不支持 python 3,而我也是最近才开始学习 python3,代码能力不强。想引进 BDD 数据驱动的概念,不知道怎么入手?希望各位帮忙指点下测试案例这块要怎么管理?
3.以上都是我最近的学习总结和想法,希望大家多多指导,谢谢~~


↙↙↙阅读原文可查看相关链接,并与作者交流