【问题标题】:Is it possible with androidviewclient to obtain and print button status?androidviewclient 是否可以获取和打印按钮状态?
【发布时间】:2016-06-09 01:24:16
【问题描述】:

我正在使用 androidviewclient 11.5.6 并尝试自动化具有 powerSwitch 按钮的 BLE 应用程序。我想获取按钮的状态并打印状态。 androidviewclient 是否有可能,或者我应该尝试使用其他工具?请帮忙!! 这是我的代码

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import re
import sys
import os
import time


import unittest

from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase

TAG = 'CULEBRA'


class CulebraTests(CulebraTestCase):

    @classmethod
    def setUpClass(cls):
        cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
        cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, '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': False, 'verbose-comments': False, 'gui': False, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': 'mytest.py', '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

        self.vc.dump(window='-1')
    #com_csr_csrmeshdemo___id_powerSwitch = self.vc.findViewByIdOrRaise("com.csr.csrmeshdemo:id/powerSwitch").touch()
        com_csr_csrmeshdemo___id_powerSwitch = self.vc.findViewWithTextOrRaise(u'OFF').touch()
    print"Light ON"
    self.vc.dump(window='-1')
    time.sleep(5)
    #com_csr_csrmeshdemo___id_powerSwitch = self.vc.findViewByIdOrRaise("com.csr.csrmeshdemo:id/powerSwitch").touch()
    com_csr_csrmeshdemo___id_powerSwitch = self.vc.findViewWithTextOrRaise(u'ON').touch()
    print"Light OFF"



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

【问题讨论】:

  • 我在 findviewwithtext( ) 函数下添加了一条打印语句,现在我可以打印设备状态了。我们可以在 findviewbyId() 函数中检索这样的状态吗?我需要在我的代码中添加一个 if 语句,例如 if switch=on: else。如何在我的程序中调用状态?请帮忙!!

标签: android python-2.7 androidviewclient


【解决方案1】:

如果我理解正确,您正在寻找的是

com_csr_csrmeshdemo___id_powerSwitch = self.vc.findViewByIdOrRaise("com.csr.csrmeshdemo:id/powerSwitch")
print "Light", com_csr_csrmeshdemo___id_powerSwitch.getText()

【讨论】:

  • 谢谢!!但它显示像这样的错误 ERROR: testSomething (main.CulebraTests) ----------------------------- ----------------------------------------- Traceback(最近一次调用最后):文件“t6.py”,第 49 行,在 testSomething com_csr_csrmeshdemo___id_powerSwitch.getText() AttributeError: 'NoneType' object has no attribute 'getText'
  • 你描述的很奇怪。如果com_csr_csrmeshdemo___id_powerSwitchNone 是因为没有找到具有这种ID 的视图,那么应该抛出异常...OrRaise()。你确定你有确切的代码作为例子吗?
  • 我的代码是com_csr_csrmeshdemo___id_powerSwitch = self.vc.findViewWithTextOrRaise(re.compile(u'(OFF)|(ON)')).touch() print"Light ", com_csr_csrmeshdemo___id_powerSwitch.getText()
  • 我也试过com_csr_csrmeshdemo___id_powerSwitch = self.vc.findViewByIdOrRaise("com.csr.csrmeshdemo:id/powerSwitch").touch() print"Light ", com_csr_csrmeshdemo___id_powerSwitch.getText() 仍然得到同样的错误!!
  • print com_csr_csrmeshdemo___id_powerSwitch 检查它的值。同样,faindViewByIdOrRaise() 返回 View 或抛出异常,它不可能返回 None。所以,显示你得到的错误到底是什么。
猜你喜欢
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多