在appium脚本中执行UiAutomator命令

时间:2018-03-29 14:20:47

标签: appium uiautomator android-uiautomator appium-android

我很想知道是否有办法直接从appium测试执行UiAutomator命令?

我看到的唯一API是"findElementByAndroidUIAutomator",它希望从uiutomator(UiScrollable / UiObject)获取返回值以便照顾它。

在我的流程中,我尝试执行一个返回布尔值的UiScrollable().flingBackward()命令...

有没有办法实现它?

2 个答案:

答案 0 :(得分:0)

除了您提到的代码之外,没有其他方法可以运行原生Uiautomator代码:findElementByAndroidUIAutomator

所以你要么试试,例如scrollIntoView和标准,用于在停止滚动的位置查找元素,或者查看TouchActons Appium客户端类。

答案 1 :(得分:0)

虽然不是Appium,但这是一个CulebraTester生成的脚本,可以完全满足您的要求,在flingBackward()对象上调用UIScrollable,在这种情况下调用Launcher工作区

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2018  Diego Torres Milano
Created on 2018-04-02 by CulebraTester 
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''


import re
import sys
import os


import unittest
try:
    sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

import pkg_resources
pkg_resources.require('androidviewclient>=12.4.0')
from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
from com.dtmilano.android.uiautomator.uiautomatorhelper import UiAutomatorHelper, UiScrollable, UiObject, UiObject2

TAG = 'CULEBRA'


class CulebraTests(CulebraTestCase):

    @classmethod
    def setUpClass(cls):
        cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
        cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': True, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
        cls.options = {'start-activity': None, 'concertina': False, 'device-art': None, 'use-jar': False, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'glare': False, 'dictionary-keys-from': 'id', 'scale': 1, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'log-actions': False, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': True, 'verbose-comments': False, 'gui': False, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': None, 'unit-test-method': None, 'interactive': False}
        cls.sleep = 5

    def setUp(self):
        super(CulebraTests, self).setUp()

    def tearDown(self):
        super(CulebraTests, self).tearDown()

    def preconditions(self):
        if not super(CulebraTests, self).preconditions():
            return False
        return True

    def testSomething(self):
        if not self.preconditions():
            self.fail('Preconditions failed')

        _s = CulebraTests.sleep
        _v = CulebraTests.verbose

        UiScrollable(self.vc.uiAutomatorHelper, uiSelector='clazz@com.android.launcher3.Workspace,res@com.google.android.apps.nexuslauncher:id/workspace,index@0,parentIndex@0,package@com.google.android.apps.nexuslauncher').flingBackward()


if __name__ == '__main__':
    CulebraTests.main()