配置文件:

# 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例子的更多相关文章

  1. jenkins+appium android app自动化测试

    jenkins安装 pytest+jenkins安装+allure报告 新建任务 其他默认,保存 立即构建 test_login.py from src.pages import login_page ...

  2. Appium Android 元素定位方法 原生+H5

    APPIUM Android 定位方式   1.定位元素应用元素 1.1通过id定位元素 Android里面定位的id一般为resrouce-id: 代码可以这样写: WebElement eleme ...

  3. 六 APPIUM Android 定位方式

    文本转自:http://www.cnblogs.com/sundalian/p/5629500.html APPIUM Android 定位方式   1.定位元素应用元素 1.1通过id定位元素 An ...

  4. 安卓自动化测试: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 ...

  5. Appium Android Bootstrap控制源代码的分析AndroidElement

    通过上一篇文章中<Appium Android Bootstrap源代码分析之简单介绍>我们对bootstrap的定义以及其在appium和uiautomator处于一个什么样的位置有了一 ...

  6. Appium Android Bootstrap源码分析之启动运行

    通过前面的两篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>和<Appium Android Bootstrap源码分析之命令解析 ...

  7. Appium Android Bootstrap源码分析之命令解析执行

    通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...

  8. Appium Android Bootstrap源码分析之控件AndroidElement

    通过上一篇文章<Appium Android Bootstrap源码分析之简介>我们对bootstrap的定义以及其在appium和uiautomator处于一个什么样的位置有了一个初步的 ...

  9. 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 ...

随机推荐

  1. Verifying Package Integrity Using MD5 Checksums or GnuPG

    In this note, I reference the MySQL manual file. After downloading the MySQL package that suits your ...

  2. javascript -window与document 待整理

    window对象和document对象的区别一般来讲,一个window里就是一个document,但是,iframe里面也可以装个document,在iframe里面就有区别了 alert(docum ...

  3. ScreenCapturePro2 for Joomla_3.4.7-tinymce4x

    1.1. 与Joomla_3.4.7-tinymce4x整合 示例下载:Joomla_3.4.7,   1.1.1. 添加screencapture文件夹   1.1.2. 2.添加插件文件夹 路径: ...

  4. page next page prev

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  5. iPhone Development – core data relationships tutorial part 1

    I’m going to start a short series on Core Data relationships and maybe throw in some general Core Da ...

  6. SOAP协议初级指南 (一)

    SOAP(Simple Object Access Protocal) 技术有助于实现大量异构程序和平台之间的互操作性,从而使存在的应用能够被广泛的用户所访问.SOAP是把成熟的基于HTTP的WEB技 ...

  7. 微信小程序自定义事件

    案例结构 首先,我还是会以案例的形式向大家讲解(这样也能方便大家更好的理解)简单介绍一下案例项目的内容(以上一章自定义组件的案例为基础)项目名称:component自定义子组件cpt父组件:logs ...

  8. linux命令の删除文件和文件夹 复制粘贴文件和文件夹

    声明:此博文来自百度经验http://jingyan.baidu.com/article/642c9d34dcba80644a46f72d.html,谢谢分享! linux删除目录很简单,很多人还是习 ...

  9. [LeetCode 题解]: palindromes

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  10. httpclient 解析excel

    http://www.blogjava.net/jayslong/archive/2011/04/21/convert_xls_and_xlsx_to_csv.html 分享用Java将Excel的x ...