zoukankan      html  css  js  c++  java
  • appium +android例子

    运行的时候记得appium要先打开!!!!!!!!

    配置文件:

    # 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'] = '23'  # 平台版本
                # 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'] = '120'  # 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)  # 等待测试报告生成

    生成测试报告:

  • 相关阅读:
    [转载]应用系统架构设计
    带隐藏功能的菜单导航
    3 CSS 高级语法
    FarPoint Spread 基础知识
    端口映射工具
    2008年上半年信息系统项目管理师考试试题分析
    DateTime.Now
    华为战略部门研究山寨机决定出售手机部门
    网管必须掌握的八个DOS命令(转)
    智能机常见问题(新手必读)
  • 原文地址:https://www.cnblogs.com/may18/p/10553092.html
Copyright © 2011-2022 走看看