美文网首页
Appimu常用API

Appimu常用API

作者: 葫芦娃大战屎壳郎 | 来源:发表于2019-04-17 15:31 被阅读0次

    以下内容来自:Appium API Documentation

    创建Session,得到self.driver,后面跟设备交互都调用driver的方法

    desired_caps = desired_caps = {
      'platformName': 'Android',
      'platformVersion': '7.0',
      'deviceName': 'Android Emulator',
      'automationName': 'UiAutomator2',
      'app': PATH('/path/to/app.apk'),
      'noReset': True,
      'fullReset': False
      
    }
    self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    

    更启动参数及参数说明: Appium Desired Capabilities.

    结束Session

    self.driver.quit()
    

    页面相关

    # 模拟返回键
    self.driver.back()
    
    # 获取横竖屏
    orientation = self.driver.orientation()
    
    # 设置横竖屏
    driver.orientation = "LANDSCAPE"
    
    # 启动某个Android 页面
    self.driver.start_activity("com.example", "ActivityName");
    
    # 获取当前 Activity
    activity = self.driver.current_activity()
    
    # 获取当前包名称
    package = self.driver.current_package()
    

    App相关

    # 安装APK
    self.driver.install_app('/Users/johndoe/path/to/app.apk')
    
    # 卸载APP
    self.driver.remove_app('com.example.AppName')
    
    # 判断APP是否安装
    self.driver.is_app_installed('com.example.AppName')
    
    # 启动APP
    self.driver.launch_app()
    
    # 关闭APP
    self.driver.close_app()
    
    # 重置APP,清空APP数据
    self.driver.reset()
    
    # 在后台运行
    self.driver.background_app(10)
    
    

    页面元素操作

    # 在当前页面查找元素
    el = self.driver.find_element_by_accessibility_id('elementID')
    
    # 查找多个元素
    el = self.driver.find_elements_by_accessibility_id('elementID')
    
    # 点击某个元素
    el = self.driver.find_element_by_accessibility_id('elementID')
    el.click()
    
    # 设置文本
    el = self.driver.find_element_by_accessibility_id('elementID')
    el.send_keys('Hello world!')
    
    # 清空文本
    el = self.driver.find_element_by_accessibility_id('elementID')
    el.clear()
    
    # 获取元素文本内容
    el = self.driver.find_element_by_accessibility_id('elementID')
    text = el.text
    
    # 获取元素tag名称
    el = self.driver.find_element_by_accessibility_id('elementID')
    tagName = el.tag_name
    
    # 获取元素的属性
    el = self.driver.find_element_by_accessibility_id('elementID')
    tagName = el.get_attribute('content-desc')
    
    # 判断是否为选中状态
    el = self.driver.find_element_by_accessibility_id('elementID')
    el.is_selected()
    
    # 判断是否为可用状态
    el = self.driver.find_element_by_accessibility_id('elementID')
    el.is_enabled()
    
    # 判断是否显示
    el = self.driver.find_element_by_accessibility_id('elementID')
    el.is_displayed()
    
    # 获取元素的位置
    el = self.driver.find_element_by_accessibility_id('elementID')
    x, y = el.location
    
    # 获取元素的宽高
    el = self.driver.find_element_by_accessibility_id('elementID')
    width, height = el.size
    

    事件相关

    # 点击某个物理键
    self.driver.press_keycode(10)
    
    # 长按某个物理键
    self.driver.long_press_keycode(10)
    
    # 隐藏键盘
    self.driver.hide_keyboard()
    
    # 模拟鼠标点击
    actions = ActionChains(driver)
    actions.move_to_element(element)
    actions.click()
    actions.perform()
    
    # 模拟按住
    actions = ActionChains(driver)
    actions.move_to_element(element)
    actions.click_and_hold()
    actions.perform()
    
    # 模拟双击
    actions = ActionChains(driver)
    actions.move_to_element(element)
    actions.double_click()
    actions.perform()
    
    # 模拟按下之后又松开
    actions = ActionChains(driver)
    actions.move_to_element(element)
    actions.click_and_hold()
    actions.move_to_element(element, 10, 10)
    action.release();
    actions.perform()
    
    # 导入TouchAction
    from appium.webdriver.common.touch_action import TouchAction
    
    # 单击
    actions = TouchAction(driver)
    actions.tap(element)
    actions.perform()
    
    # 双击
    actions = TouchAction(driver)
    actions.double_tap(element)
    actions.perform()
    
    # 按住拖动
    actions = TouchAction(driver)
    actions.tap_and_hold(element)
    actions.move(50, 50)
    actions.perform()
    
    # 手指按住屏幕
    actions = TouchAction(driver)
    actions.tap_and_hold(element)
    actions.move(50, 50)
    actions.perform()
    
    # 手指按住屏幕,并放开
    actions = TouchAction(driver)
    actions.tap_and_hold(20, 20)
    actions.release(50, 50)
    actions.perform()
    
    # 手指长按屏幕
    actions = TouchAction(driver)
    actions.long_press(element)
    actions.perform()
    
    # 滑动一段距离
    actions = TouchAction(driver)
    actions.scroll_from_element(element, 10, 100)
    actions.scroll(10, 100)
    actions.perform()
    
    # 抛动
    actions = TouchAction(driver)
    actions.flick_element(element, 1, 10, 10)
    actions.perform()
    
    # 多点触控
    a1 = TouchAction()
    a1.press(10, 20)
    a1.move_to(10, 200)
    a1.release()
    
    a2 = TouchAction()
    a2.press(10, 10)
    a2.move_to(10, 100)
    a2.release()
    
    ma = MultiAction(self.driver)
    ma.add(a1, a2)
    ma.perform()
    
    # 模拟触摸事件
    actions = TouchAction(driver)
    actions.tap_and_hold(20, 20)
    actions.move_to(10, 100)
    actions.release()
    actions.perform()
    

    文件操作

    self.driver.push_file('/path/to/device/foo.bar', 'QXJlIHlvdXIgYmVlcnMgb2theT8=');
    
    file_base64 = self.driver.pull_file('/path/to/device/foo.bar')
    
    folder_base64 = self.driver.pull_folder('/path/to/device/foo.bar')
    

    屏幕录制

    # 开启屏幕录制
    self.driver.start_recording_screen()
    
    # 结束屏幕录制
    self.driver.stop_recording_screen()
    
    # 屏幕截屏
    screenshotBase64 = self.driver.get_screenshot_as_base64()
    

    设备相关

    # 震动手机
    self.driver.shake()
    
    # 锁屏幕,解锁
    self.driver.lock()
    self.driver.unlock()
    
    # 获取设备时间
    time = self.driver.device_time()
    

    相关文章

      网友评论

          本文标题:Appimu常用API

          本文链接:https://www.haomeiwen.com/subject/vbsnwqtx.html