如何使用AndroidViewClient检查设备或模拟器是否已启动?

2024-10-01 19:22:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的一个项目使用AndroidViewClient库。我面临的问题是我的脚本在模拟器上运行良好,但在真正的设备上我必须添加一些代码行。但是我没有找到如何检查我的设备是真实的还是模拟器。我需要这样的短信:

if device.isSimulator():
      # todo smth 
else  # todo antoher

另外,我知道模拟器的名称以“emulator-1234”开头,但没有想到如何获得连接设备的名称


Tags: 项目代码脚本名称ifdevice模拟器短信
2条回答

试试这个

SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE);
    if (manager.getSensorList(Sensor.TYPE_ALL).isEmpty()) {
        // running on an emulator
    } else {
        // running on a device
    }

我假设您已经使用culebra生成了脚本的基,如果不是真的,那么您的代码也可能与此非常相似:

...
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)

然后,serialno可以帮助您确定您是否在某个特定的设备或模拟器上。在

所有这些选项都将打印serialno

^{pr2}$

另一方面,如果您已经使用culebra来生成单元测试(-U, unit-test-class generates unit test class and script),那么可以使用自动生成的preconditions()方法来检查您是否在真实的设备或模拟器上运行

...
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': True, 'verbose-comments': False, 'gui': False, 'find-views-with-text': True, 'prepend-to-sys-path': True, 'drop-shadow': False, 'output': '/Users/diego/tmp/serialno-test.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
        if not re.match('emulator-.*', self.serialno):
            self.fail("This tests only run on emulator");
        return True

相关问题 更多 >

    热门问题