配置文件:

# 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. WebAPI的路由规则

    1.自定义路由 public static class WebApiConfig { public static void Register(HttpConfiguration config) { / ...

  2. Linux分区挂载点介绍

    一.Linux分区挂载点介绍 Linux分区挂载点介绍,推荐容量仅供参考不是绝对,跟各系统用途以及硬盘空间配额等因素实际调整: 分区类型 介绍 备注 /boot 启动分区 一般设置100M-200M, ...

  3. SQL Server横向扩展:设计,实现与维护(2)- 分布式分区视图

    为了使得朋友们对分布式分区视图有个概念,也为了方便后面的内容展开,我们先看看下面一个图:     讲述分布式分区视图之前,很有必要将之与我们常常熟悉的分区表和索引进行区别. 首先,分布式分区视图是一个 ...

  4. code1099 字串变换

    BFS 听上去蛮简单的,实际编程复杂度较高(至少一个快睡着的人是这么认为的...) 抄的题解(感谢题解的作者<'_'>): #include<queue> #include&l ...

  5. 9个使用前必须再三小心的 Linux 命令-乾颐堂

    Linux shell/terminal 命令非常强大,即使一个简单的命令就可能导致文件夹.文件或者路径文件夹等被删除. 在一些情况下,Linux 甚至不会询问你而直接执行命令,导致你丢失各种数据信息 ...

  6. pcl知识

    1.pcl/io/pcd_io.h pcl/io/ply_io.h pcl::PLYReader reader; pcl::PCDWriter pcdwriter; 读取ply pcd 2.voidl ...

  7. 案例研究:手机APP的UI设计流程

    以下内容由Mockplus(http://www.mockplus.cn)团队翻译整理,仅供学习交流. UI设计——不仅仅是创造漂亮的图像. 面临的挑战 我为自己提供了一个绝佳的机会来训练我的视觉设计 ...

  8. Java中String、StringBuffer和StringBuilder之间的区别

    String在Java中是字符串常量 例如 String str = "abc"; str = str + 1; System.out.println(str); 结果将是abc1 ...

  9. Replication--分区+复制

    1>配置订阅表使用分区,在发布的项目属性中设置"复制分区方案"和"复制索引分区方案"为true,然后初始化订阅 2>在发布数据库上修改发布属性 -- ...

  10. mvn -v提示Permission denied

    解决办法: 增加权限 chmod a+x  /usr/local/apache-maven-3.5.2/bin/mvn 解释: (a:所有用户  +:增加权限   x:执行权限)