【问题标题】:Automating the setting up Android CTS testing自动设置 Android CTS 测试
【发布时间】:2013-07-10 16:03:16
【问题描述】:

我想知道是否有办法或是否有人尝试自动设置用于 CTS 测试的设备?

基本上我有以下场景

  • 我正在使用 jenkins 来刷新设备,这会将修改后的 4.2 加载到设备上
  • 设备刷新后,我想运行 CTS 测试。
  • 无需用户交互,我希望能够设置设备并运行 CTS。

有没有办法做到这一点?还是用户交互是设置设备进行 CTS 测试的唯一方法?

【问题讨论】:

    标签: android adb android-4.2-jelly-bean monkeyrunner cts


    【解决方案1】:

    是的,有一种方法可以为 CTS 设置设备。我使用AndroidViewClient 来做到这一点。以下是您如何实现这一目标的一些示例:

    # This must be imported before MonkeyRunner and MonkeyDevice,
    # otherwise the import fails.
    from com.dtmilano.android.viewclient import ViewClient, View
    
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyView
    
    package = 'com.android.settings'
    activity = '.Settings'
    component_name = package + '/' + activity
    EXCLUDE_FROM_RECENTS = 0x00800000
    ap_name = "Put acces point name here"
    
    # Connect to device with the IP received as a parameter
    device, serialno = ViewClient.connectToDeviceOrExit()
    
    width = int(device.getProperty('display.width'))
    height = int(device.getProperty('display.height'))
    
    # Press the HOME button to start the test from the home screen
    device.press('KEYCODE_HOME','DOWN_AND_UP')
    MonkeyRunner.sleep(2)
    
    # Open the Settings app
    device.startActivity(component = component_name, flags = EXCLUDE_FROM_RECENTS)
    i = 0
    while str(device.getProperty('am.current.package')) != package and i<10:
       MonkeyRunner.sleep(1)
       i = i + 1
    if i == 10:
       raise Exception('Cannot open package')
    
    # Create the view client object
    vc = ViewClient(device=device, serialno=serialno)
    
    # Enable Wi-Fi
    device.shell("svc wifi enable")
    i = 0
    while not vc.findViewWithText(ap_name) and i<30:
        MonkeyRunner.sleep(1)
        vc.dump()
        i += 1
    if i == 30:
        print "Cannot enable Wi-Fi"
    for i in range(5):
        device.press('KEYCODE_DPAD_RIGHT', MonkeyDevice.DOWN_AND_UP)
        MonkeyRunner.sleep(0.5)
    device.press('KEYCODE_DPAD_LEFT', MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(0.5)
    device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(2)
    device.type(ap_name)
    vc.dump()
    if vc.findViewWithText('Save'):
        vc.findViewWithText('Save').touch()
        MonkeyRunner.sleep(5)
    
    # Set Screen Lock to None
    drag_start = (width*1/4, height*9/10)
    drag_end = (width*1/4, height*1/10)
    device.drag(drag_start, drag_end)
    vc.dump()
    if vc.findViewWithText('Security'):
        vc.findViewWithText('Security').touch()
    MonkeyRunner.sleep(1)
    vc.dump()
    if vc.findViewWithText('Screen lock'):
        vc.findViewWithText('Screen lock').touch()
    MonkeyRunner.sleep(1)
    vc.dump()
    if vc.findViewWithText('None'):
        vc.findViewWithText('None').touch()
    MonkeyRunner.sleep(1)
    vc.dump()
    
    # Activate device administrator
    if vc.findViewWithText('Device administrators'):
        vc.findViewWithText('Device administrators').touch()
    MonkeyRunner.sleep(1)
    vc.dump()
    admin1 = vc.findViewWithText('android.deviceadmin.cts.CtsDeviceAdminReceiver')
    if admin1:
        for aux_view in admin1.parent.parent.children:
            if aux_view['class'] == 'android.widget.CheckBox':
                break
        if aux_view['checked'] == 'false':
            admin1.touch()
            MonkeyRunner.sleep(1)
            vc.dump()
            if vc.findViewWithText('Activate'):
                vc.findViewWithText('Activate').touch()
    MonkeyRunner.sleep(1)
    vc.dump()
    admin2 = vc.findViewWithText('android.deviceadmin.cts.CtsDeviceAdminReceiver2')
    if admin2:
        for aux_view in admin2.parent.parent.children:
            if aux_view['class'] == 'android.widget.CheckBox':
                break
        if aux_view['checked'] == 'false':
            admin2.touch()
            MonkeyRunner.sleep(1)
            vc.dump()
            if vc.findViewWithText('Activate'):
                vc.findViewWithText('Activate').touch()
    MonkeyRunner.sleep(1)
    
    # Enable Delegating Accessibility Service
    MonkeyRunner.sleep(1)
    vc.dump()
    if vc.findViewWithText('Accessibility'):
        vc.findViewWithText('Accessibility').touch()
    MonkeyRunner.sleep(1)
    vc.dump()
    if vc.findViewWithText('Delegating Accessibility Service'):
        vc.findViewWithText('Delegating Accessibility Service').touch()
    MonkeyRunner.sleep(1)
    for count in range(5):
        device.press('KEYCODE_DPAD_RIGHT', MonkeyDevice.DOWN_AND_UP)
        MonkeyRunner.sleep(0.5)
    device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(1)
    vc.dump()
    MonkeyRunner.sleep(1)
    if vc.findViewWithText('Use Delegating Accessibility Service?'):
        vc.findViewWithText('OK').touch()
    else:
        vc.findViewWithText('Cancel').touch()
    MonkeyRunner.sleep(1)
    

    这些是为 CTS 准备设备的一些步骤。按照 CTS 设置指南中每个部分的示例进行操作。

    【讨论】:

    • 很棒的剧本。这对于运行 CTS 的其他人可能非常有用。您可能希望将其添加到 AndroidViewClient 示例中。上传到github,我很乐意合并。
    • @dtmilano 当我只连接一个设备时,使用 AndroidViewClient 可以正常工作。但是,当我连接了多个设备时,脚本中不断出现错误。例如,当我想连接到特定设备(例如设备)时,serialno = ViewClient.connectToDeviceOrExit(serialno="016B756C1001A016") 我收到以下错误:vc = ViewClient(device=device, serialno=serialno) xml.parsers.expat。 ExpatError:结尾部分不允许内容。这可能是模块处理多个设备的方式的缺陷。
    • @user2569315 启用DEBUG_RECEIVED 并检查其中的内容会很有趣。尾随会话中不允许的内容意味着在 XML 之后有一些内容。
    • @dtmilano 在 Galaxy nexus 设备上,当委派可访问性打开时,当调用 uiautomator 转储时,我得到“Killed”,例如:“4]” />Killed”。但是在 Nexus 4 上运行相同的测试它工作正常。因此这导致我相信它自己的 android 源中可能存在导致 GN 问题的错误。这个问题与问题 stackoverflow.com/questions/17130500/… 非常相似。有解决方法吗?
    • 你是对的。这绝对是一个 UiAutomator 错误,但 AndroidViewClient 可以从中弥补。你试过在github.com/dtmilano/AndroidViewClient/blob/master/…中设置ignoreuiautomatorkilled=True
    猜你喜欢
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2011-05-06
    相关资源
    最近更新 更多