|
1 | | -#! /usr/bin/env monkeyrunner |
| 1 | +#! /usr/bin/env python |
2 | 2 | ''' |
3 | 3 | Copyright (C) 2012 Diego Torres Milano |
4 | 4 | Created on Feb 3, 2012 |
5 | 5 |
|
6 | 6 | This example starts the TemperatureConverter activity then type '123' into the 'Celsius' field. |
7 | 7 | Then a ViewClient is created to obtain the view dump and the current values of the views with |
8 | | -id/celsius and id/fahrenheith are obtained and the conversion printed to stdout. |
| 8 | +id/celsius and id/fahrenheit are obtained and the conversion printed to stdout. |
9 | 9 | Finally, the fields are obtained by using their tags and again, conversion printed to stdout. |
10 | | - |
| 10 | + |
| 11 | +If --localViewServer is passed in the command line then LocalViewServer provided by |
| 12 | +TemperatureConverter is used. This is very useful when the device is secure and ViewServer |
| 13 | +cannot be started. |
| 14 | +
|
11 | 15 | @author: diego |
12 | 16 | ''' |
13 | 17 |
|
14 | 18 |
|
15 | 19 | import re |
16 | 20 | import sys |
17 | 21 | import os |
| 22 | +import time |
18 | 23 |
|
19 | | -# This must be imported before MonkeyRunner and MonkeyDevice, |
20 | | -# otherwise the import fails. |
21 | 24 | # PyDev sets PYTHONPATH, use it |
22 | 25 | try: |
23 | 26 | for p in os.environ['PYTHONPATH'].split(':'): |
|
33 | 36 |
|
34 | 37 | from com.dtmilano.android.viewclient import ViewClient |
35 | 38 |
|
36 | | -from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice |
37 | | - |
38 | 39 | localViewServer = False |
39 | 40 | if len(sys.argv) > 1 and sys.argv[1] == '--localViewServer': |
40 | 41 | localViewServer = True |
|
48 | 49 | componentName = package + "/" + activity |
49 | 50 |
|
50 | 51 | device.startActivity(component=componentName, flags=FLAG_ACTIVITY_NEW_TASK) |
51 | | -MonkeyRunner.sleep(5) |
| 52 | +time.sleep(5) |
52 | 53 |
|
53 | 54 |
|
54 | 55 | device.type("123") |
55 | | -MonkeyRunner.sleep(3) |
| 56 | +time.sleep(3) |
56 | 57 |
|
57 | 58 | vc = ViewClient(device, serialno, startviewserver=(not localViewServer)) |
58 | 59 |
|
59 | | -# obtain the views by id |
60 | | -celsius = vc.findViewByIdOrRaise("id/celsius") |
61 | | -fahrenheit = vc.findViewByIdOrRaise("id/fahrenheit") |
| 60 | +if vc.build['ro.build.version.sdk'] >= 16: |
| 61 | + # obtain the views by contentDescription |
| 62 | + celsius = vc.findViewWithContentDescriptionOrRaise("celsius") |
| 63 | + fahrenheit = vc.findViewWithContentDescriptionOrRaise("fahrenheit") |
| 64 | +else: |
| 65 | + # obtain the views by id |
| 66 | + celsius = vc.findViewByIdOrRaise("id/celsius") |
| 67 | + fahrenheit = vc.findViewByIdOrRaise("id/fahrenheit") |
62 | 68 |
|
63 | 69 | ct = celsius.getText() |
64 | 70 | if ct: |
|
0 commit comments