Skip to content

Instantly share code, notes, and snippets.

@qihnus
Created February 25, 2012 18:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save qihnus/1909923 to your computer and use it in GitHub Desktop.
Dectects text change in an EditText. Concept prototype for Android functional testing.
################################################################################
#
# Requirements:
# * pexpect (http://www.noah.org/wiki/Pexpect)
# * Java SDK is on the PATH
#
# Steps:
# 1. Start target activity on a device or emulator
# 2. Start DDMS, click on the package name of the started activity from the
# devices & processes window on the left, the rightmost column should now
# say "86XX / 8700" in a default setup
# 3. Run this script
# 4. When it says ready, enter text in any EditTexts in the target activity
# 5. The script should print out relevent info
#
################################################################################
import re
import pexpect
_JDB_PROMPT = '\n> '
_JDB_BREAKPOINT_PROMPT = '\n<\d> '
def _get_expr_val(child, expr):
child.sendline('print %s' % expr)
child.expect(_JDB_BREAKPOINT_PROMPT)
pat = re.compile('%s = (.+)' % re.escape(expr))
val = pat.findall(child.before.strip())
assert val and len(val) > 0
return val[0]
def main():
cmd = 'jdb -attach localhost:8700'
child = pexpect.spawn(cmd)
child.expect(_JDB_PROMPT)
child.sendline('stop in android.widget.TextView.onTextChanged'),
print 'ready'
while True:
ret = child.expect([_JDB_PROMPT, _JDB_BREAKPOINT_PROMPT], timeout=None)
if ret != 1:
continue
cls = _get_expr_val(child, 'this.getClass().getName()')
if cls == '"android.widget.EditText"':
vid = int(_get_expr_val(child, 'this.getId()'))
text = _get_expr_val(child, 'text')
print 'text in EditText', vid, 'changed to', text
child.sendline('cont')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment