Android测试Android开发Android

Windows下搭建Appium(Android APP自动化测

2017-03-18  本文已影响418人  Karma1026

Appium是原生和混合移动移动应用自动化测试的跨平台解决方案,支持iOS、Android本地应用以及Hybrid和Mobile Web应用的测试,支持真机和模拟测试,支持本地和云端部署

Appium的设计哲学:

Appium在底层使用Apple和Google的自动化测试框架来进行测试:

Appium在Windows下安装过程

1)安装node.js

(** 温馨提示:由于GFW的原因,下载会经常出问题 **,现在推荐一个国内的下载地址:http://nodejs.cn/download/

下载自己相对应的版本,按照提示,点击下一步安装。(** 那么简单我就不说安装了,不会请Google一下 **)

验证是否安装成功,打开命令窗口输入node -v

2)安装配置Java的JDK(Java软件开发工具包)

官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
下载自己相对应的版本,按照提示,点击下一步安装。安装完成之后配置环境变量

注意变量值字符串前面有一个.表示当前目录,设置CLASSPATH 的目的,在于告诉Java执行环境,在哪些目录下可以找到您所要执行的Java程序所需要的类或者包;

验证是否安装成功,输入java -version

3)安装配置Android的SDK(Android软件开发工具包)

官方下载地址:https://developer.android.com/studio/install.html
国内下载地址:http://www.android-studio.org/

安装完成后,配置环境变量:

配置好,下载需要的sdk版本

3)安装Appium

官网下载地址:http://appium.io/

点击Download Appium,默认就下载的是AppiumForWindows安装包,下载完成后,解压安装包,运行appium-installer.exe进行安装;


** 注:还可以使用node.js的模块管理工具npm,输入命令在线安装npm install -g appium,但在线安装失败的几率比较大,貌似是因为网络不稳或权限问题无法解压,还是推荐直接下载Windows安装包;**

安装完成后,配置环境变量:

4)Appium使用

5)编写Python脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/3/16 15:35
# @Author  : Karma
# @github  : karmalove
# @email   : 1026karma@gmail.com

from appium import webdriver
from time import sleep

desired_caps = {}
desired_caps['platformName'] = 'Android'  # 测试平台
desired_caps['platformVersion'] = '5.1'  # 平台版本
desired_caps['deviceName'] = 'HUAWEI CUN-AL00'  # 设备名称,多设备时需区分
desired_caps['appPackage'] = 'com.heipiao.app.customer'  # app package名
desired_caps['appActivity'] = '.MainActivity'  # app默认Activity

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) #启动Remote RPC


# 获取屏幕宽和高
def getSize():
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    return (x, y)


# 向左滑动
def swipeLeft(t):
    l = getSize()
    x1 = int(l[0] * 0.75)
    y1 = int(l[1] * 0.5)
    x2 = int(l[0] * 0.25)
    driver.swipe(x1, y1, x2, y1, t)


# 向右滑动
def swipeRight(t):
    l = getSize()
    x1 = int(l[0] * 0.25)
    y1 = int(l[1] * 0.5)
    x2 = int(l[0] * 0.75)
    driver.swipe(x1, y1, x2, y1, t)


# 向上滑动
def swipeUp(t):
    l = getSize()
    x1 = int(l[0] * 0.5)
    y1 = int(l[1] * 0.75)
    y2 = int(l[1] * 0.25)
    driver.swipe(x1, y1, x1, y2, t)


# 向下滑动
def swipeDown(t):
    l = getSize()
    x1 = int(l[0] * 0.5)
    y1 = int(l[1] * 0.25)
    y2 = int(l[1] * 0.75)
    driver.swipe(x1, y1, x1, y2, t)


# 钓点
driver.find_element_by_id('com.heipiao.app.customer:id/foot_bar_home').click()
el = driver.find_element_by_class_name("android.widget.ListView")
item_list = el.find_elements_by_class_name("android.widget.LinearLayout")
try:
    item_list[1].click()
except:
    print "Error! in item list len"
driver.back()

# 大师
driver.find_element_by_id('com.heipiao.app.customer:id/foot_bar_dashi').click()

# 发现
driver.find_element_by_id('com.heipiao.app.customer:id/foot_bar_find').click()


# 我的
def me():
    # 我的
    driver.find_element_by_id('com.heipiao.app.customer:id/main_footbar_user').click()
    el = driver.find_element_by_class_name("android.widget.ScrollView")
    # 我的钱包
    driver.find_element_by_id('com.heipiao.app.customer:id/rl_me_money').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/immediately_recharge').click()
    driver.find_element_by_name('¥ 50').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/rl_alipay').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/tv_recharge').click()
    driver.back()
    driver.back()
    # 我的钓场券
    driver.find_element_by_id('com.heipiao.app.customer:id/rl_me_order').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/not_consume_listView')
    swipeUp(300)
    sleep(1)
    driver.find_element_by_id('com.heipiao.app.customer:id/refund_ticket').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/re5').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/confirm_ticket').click()
    sleep(1)
    driver.find_element_by_id('com.heipiao.app.customer:id/yes_consume').click()
    sleep(1)
    driver.find_element_by_id('com.heipiao.app.customer:id/failure').click()
    sleep(1)
    driver.back()
    # 我的优惠券
    driver.find_element_by_id('com.heipiao.app.customer:id/rl_me_coupon').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/no_use_coupon').click()
    swipeUp(300)
    sleep(1)
    driver.find_element_by_id('com.heipiao.app.customer:id/last_coupon').click()
    sleep(1)
    driver.find_element_by_id('com.heipiao.app.customer:id/use_coupon').click()
    sleep(1)
    driver.find_element_by_id('com.heipiao.app.customer:id/save_fish').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/cancel').click()
    driver.back()
    # 我的存鱼
    driver.find_element_by_id('com.heipiao.app.customer:id/rl_me_save_fish').click()
    driver.back()
    # 我的订单
    driver.find_element_by_id('com.heipiao.app.customer:id/rl_my_order').click()
    driver.find_element_by_id("com.heipiao.app.customer:id/buyers_status").click()
    # driver.find_element_by_name('待支付').click()
    # driver.find_element_by_id('com.heipiao.app.customer:id/pay_order').click()
    # driver.find_element_by_id("com.heipiao.app.customer:id/cancel_order").click()
    driver.back()
    # 我的关注
    swipeUp(200)
    driver.find_element_by_id('com.heipiao.app.customer:id/rl_me_follow_site').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/follow_site').click()
    sleep(1)
    driver.find_element_by_id('com.heipiao.app.customer:id/follow_fishtool').click()
    sleep(1)
    driver.find_element_by_id('com.heipiao.app.customer:id/follow_teacher').click()
    sleep(1)
    driver.back()
    # 设置
    driver.find_element_by_id('com.heipiao.app.customer:id/rl_me_insurance_inquiry').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/my_settings_about_heipiao').click()
    driver.find_element_by_id('com.heipiao.app.customer:id/phone_me').click()
    driver.back()
    driver.back()
    driver.back()


me()

** 后续会写完整的测试用例和生成测试报告的代码 **

上一篇 下一篇

猜你喜欢

热点阅读