1

I am using AndroidViewClient library for one of my project. I faced with problem that my scrip working good on the emulator, but on the real device I have to add some lines of code. But I didn't find how to check is my device real or simulator. I need smth like this:

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

Also I know that the name of simulator starts with "emulator-1234" but didn't figure out how to get the name of conncted device

Mikhail Valuyskiy
  • 1,238
  • 2
  • 16
  • 31

2 Answers2

1

I'm assuming you have generated the base of your script using culebra, if not true probably your code is also very similar to this:

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

then you have serialno that would help you determine if you are on some specific device or emulator.

All of these options will print the serialno:

print vc.serialno
print device.serialno
print serialno

On the other hand, if you have used culebra to generate unit tests (-U, --unit-test-class generates unit test class and script) you can use the auto-generated preconditions() method to check if you are running on a real device or emulator

...
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
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Try this

SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE);
    if (manager.getSensorList(Sensor.TYPE_ALL).isEmpty()) {
        // running on an emulator
    } else {
        // running on a device
    }
Luca Ziegler
  • 3,236
  • 1
  • 22
  • 39