appium +android例子
配置文件:
# coding:utf-8
__author__ = 'Helen'
"""
description:配置全局参数
"""
import time
import os # 获取项目路径
# project_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)[0]), '.'))
project_path = os.path.abspath(os.path.join(os.path.dirname(os.path.split(os.path.realpath(__file__))[0]), '.'))
# 测试用例代码存放路径(用于构建suite,注意该文件夹下的文件都应该以test开头命名)
test_case_path = project_path+"\\src\\test_case"
# print u'日志路径:'+log_path
# 测试报告存储路径,并以当前时间作为报告名称前缀
# 获取到当前文件的目录,并检查是否有Report文件夹,如果不存在则自动新建Report文件
report_path = project_path+"\\report\\"
if not os.path.exists(report_path):
os.makedirs(report_path)
report_name = report_path+time.strftime('%Y%m%d%H%S', time.localtime())
# 设置用户名和密码
login_name = "****" # 登录名称
login_password = "*888" # 登录密码
公共类:
driver_configure.py
# coding:utf-8
__author__ = 'm'
'''
description:driver配置
'''
from appium import webdriver class Driver_configure():
def get_driver(self):
"""
获取driver
"""
try:
self.desired_caps = {}
self.desired_caps['platformName'] = 'Android' # 平台
self.desired_caps['platformVersion'] = '' # 平台版本
# self.desired_caps['app'] = 'E:/autotestingPro/app/UCliulanqi_701.apk' # 指向.apk文件,如果设置appPackage和appActivity,那么这项会被忽略
self.desired_caps['appPackage'] = 'com.***.android' # APK包名
# cls.desired_caps['appActivity'] = '.ui.****_Activity'
# 被测程序启动时的Activity .'activity名,
self.desired_caps['unicodeKeyboard'] = 'true' # 是否支持unicode的键盘。如果需要输入中文,要设置为“true”
self.desired_caps['resetKeyboard'] = 'false' # 是否在测试结束后将键盘重轩为系统默认的输入法。
self.desired_caps['newCommandTimeout'] = '' # Appium服务器待appium客户端发送新消息的时间。默认为60秒 # 超时时间
self.desired_caps['deviceName'] = '*****'
# 手机ID(adb devices可获得)
self.desired_caps['noReset'] = True # true:不重新安装APP,false:重新安装app
# cls.desired_caps['autoGrantPermissions'] = True
# 远程控制,通过appium可设置;若是真机,直接填写http://localhost:4723/wd/hub 或者http://127.0.0.1:4723/wd/hub即可
self.driver = webdriver.Remote("http://localhost:4723/wd/hub", self.desired_caps)
return self.driver
except Exception as e:
raise e
baseClass.py
import os
import time
# from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException class BaseClass(object):
def __init__(self, driver):
self.driver = driver def find_element(self, *loc):
"""
重写find_element方法,显式等待
"""
try:
# self.driver.wait_activity(self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("%s")'), 10)
# WebDriverWait(self.driver, 15).until(EC.visibility_of_element_located(loc))
time.sleep(5)
return self.driver.find_element(*loc)
except NoSuchElementException as msg:
print(u"查找元素异常: %s" % msg)
# self.driver.back()
raise msg # 抛出异常 def send_keys(self, value, *loc):
try:
self.find_element(*loc).clear()
self.find_element(*loc).send_keys(value)
except AttributeError as e:
raise e def element_click(self, *loc): # 点击操作
try:
self.find_element(*loc).click()
except AttributeError as e:
raise e
gesture_operator.py
# coding:utf-8
__author__ = 'Helen'
'''
description:手势操作
'''
class Gesture_mainpulation:
def swipe_left(self,driver):
'''左滑'''
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
driver.swipe(x*3/4,y/4,x/4,y/4) def swipe_right(self,driver):
'''右滑'''
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
driver.swipe(x/4,y/4,x*3/4,y/4) def swipe_down(self,driver):
'''下滑'''
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
driver.swipe(x/2,y*3/4,x/2,y/4) def swipe_up(self,driver):
'''上滑'''
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
driver.swipe(x/2,y/4,x/2,y*3/4)
页面:
login_page.py
from src.common import baseClass
from appium.webdriver.common import mobileby class login_page(baseClass.BaseClass): # 继承公共类
by = mobileby.MobileBy()
user = (by.ANDROID_UIAUTOMATOR,'new UiSelector().resourceId("com.****.android:id/name")')
pwd = (by.ANDROID_UIAUTOMATOR,'new UiSelector().resourceId("com.****.android:id/pass")')
login_button = (by.ANDROID_UIAUTOMATOR,'new UiSelector().resourceId("com.***.android:id/but_OK")') def input_user(self, username):
self.send_keys(username, *self.user) def input_Pws(self, password):
self.send_keys(password, *self.pwd) def click_btnLogin(self):
self.find_element(*self.login_button).click()
测试用例:
test_login.py
from src.pages import login_page
from src.common import driver_configure, gesture_operator
import unittest
import time
# from appium import webdriver
import warnings
# driver = driver_configure.Driver_configure() class Test_appium(unittest.TestCase):
@classmethod
def setUpClass(cls):
warnings.simplefilter("ignore", ResourceWarning)
dconfigur = driver_configure.Driver_configure()
cls.driver = dconfigur.get_driver()
cls.GM = gesture_operator.Gesture_mainpulation() # 手势 @classmethod
def tearDownClass(cls):
# cls.driver.quit()
pass # 登陆
def test_login(self):
time.sleep(1)
self.login_page = login_page.login_page(self.driver)
self.login_page.input_user("1*******")
self.login_page.input_Pws("***")
self.login_page.click_btnLogin()
# self.driver.find_element_by_id('com.****.android:id/but_OK').click()
# 设置隐式等待时间
self.driver.implicitly_wait(3) if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(Test_appium('test_login'))
执行用例:
runtest.py
# coding:utf-8
__author__ = 'Helen'
'''
description:执行测试
'''
import unittest
from config.globalparameter import test_case_path,report_name
import HTMLTestRunner
import time
# from src.common import send_mail # 构建测试集,包含src/test_case目录下的所有以test开头的.py文件
suite = unittest.defaultTestLoader.discover(start_dir=test_case_path,pattern='test_*.py') # 执行测试
if __name__=="__main__":
report = report_name + "Report.html"
fb = open(report, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fb,
title=u'appium自动化测试报告',
description=u'项目描述:test'
)
runner.run(suite)
fb.close()
time.sleep(2) # 等待测试报告生成
生成测试报告:
appium +android例子的更多相关文章
- jenkins+appium android app自动化测试
jenkins安装 pytest+jenkins安装+allure报告 新建任务 其他默认,保存 立即构建 test_login.py from src.pages import login_page ...
- Appium Android 元素定位方法 原生+H5
APPIUM Android 定位方式 1.定位元素应用元素 1.1通过id定位元素 Android里面定位的id一般为resrouce-id: 代码可以这样写: WebElement eleme ...
- 六 APPIUM Android 定位方式
文本转自:http://www.cnblogs.com/sundalian/p/5629500.html APPIUM Android 定位方式 1.定位元素应用元素 1.1通过id定位元素 An ...
- 安卓自动化测试:Android studio 自带的 Record Espresso Test || [ Appium & (Android studio || Python|| Eclipse ) ]
1.Android studio 自带的 Record Espresso Test https://developer.android.com/studio/test/espresso-test-r ...
- Appium Android Bootstrap控制源代码的分析AndroidElement
通过上一篇文章中<Appium Android Bootstrap源代码分析之简单介绍>我们对bootstrap的定义以及其在appium和uiautomator处于一个什么样的位置有了一 ...
- Appium Android Bootstrap源码分析之启动运行
通过前面的两篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>和<Appium Android Bootstrap源码分析之命令解析 ...
- Appium Android Bootstrap源码分析之命令解析执行
通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...
- Appium Android Bootstrap源码分析之控件AndroidElement
通过上一篇文章<Appium Android Bootstrap源码分析之简介>我们对bootstrap的定义以及其在appium和uiautomator处于一个什么样的位置有了一个初步的 ...
- Appium 【已解决】提示报错:Attempt to re-install io.appium.android.ime without first uninstalling.
详细报错:Failed to install D:\AutoTest\appium\Appium\node_modules\appium\build\unicode_ime_apk\UnicodeIM ...
随机推荐
- Zedboard学习(一):移植Ubuntu桌面操作系统 标签: ubuntu移植zedboardFPGA 2017-07-04 21:53 26人阅读
环境准备: 首先,需要的肯定是Ubuntu操作系统.可以在自己的电脑上安装物理机,也可以是虚拟机下运行的.我的是在Vmware下运行的Ubuntu14.04 32位操作系统. 由于zedboard上的 ...
- 10个最新手机美食APP界面设计欣赏
移动软件时代,简单下载美食app,动动手指,滑动几下手机屏幕,即可足不出户,搜索,预定和购买各路美食.然而,对于作为手机app UI 界面设计师的你来说,最大的问题并不在于如何使用这些美食软件来方便生 ...
- PXE
PXE 摘自:http://www.360doc.com/content/15/0226/08/17652659_450872586.shtml 一.简介 1.1 什么是PXE PXE(Pre ...
- 掌握zigbee网络里的相关的重要概论
1.zigbee无线通信,需要高频的载波来提供发射效率,zigbee模块之间要可以正常的收发,必须把接收频率设置和发射模块的载波频率一致. 2.zigbee有27个载波可以进行通信,载波叫做信道(无线 ...
- app里面嵌套的H5页面上电话号码怎么才能点击拨号?
代码: <p>联系我们:<a href="tel://66666666666">66666666666</a> </p>
- mybatis SqlMapConfig.xml
一.SqlMapConfig.xml 1.属性properties 在入门时,以抽取出连接数据库的属性得到properties文件. a.可以通过resource和url来获得属性. b.proper ...
- Jquery 欲绑定事件
有时候,想提前给即将添加的的元素绑定事件,这时候使用on就不行了,利用事件的冒泡机制可以完成这个功能 Jquery 提供了delegate方法就是这样实现的. $("#schemaaccor ...
- Ubuntu 将应用程序 固定到快快速启动栏(以Sublime为例)
因为Sublime Text并不是需要安装,所以缺少Ubuntu桌面运行的一些基本配置,比如不能将它加入桌面侧边的启动器. 而Ubuntu上也没有快捷方式的说法,而通过软件中心安装的软件就有图标,并能 ...
- 长时间停留在calculating requirements and dependencies
如果安装插件的时候,Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 )这个问题通常就是在点击安装之后显示“Calcu ...
- 淘宝IP地址库采集器c#
个人原创.欢迎转载.转载请注明出处.http://www.cnblogs.com/zetee/articles/3482085.html 采集器概貌,如下: 最近做一个项目,功能类似于CNZZ站长统计 ...