Error: No module named dtmilano - android

I followed the steps mentioned in the below post:
Can't get AndroidViewClient example code to run
But im still getting error "No module named dtmilano"
In my test.py file i have added the following lines :
sys.path.append('C:\AndroidViewClient-master\AndroidViewClient-master\AndroidViewClient\src')
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.dtmilano.android.viewclient import ViewClient
the same is done in dump.py file
I have also set the environment variable for AndroidViewClient.
Is there anything i missed ?
Thanks in advance.

I guess is related with how the '\' (backslash) are treated, they may be escaping the following character.
Using AndroidViewClient plugin as explained here should solve this Windows problem.
For example:
c:>\path\to\monkeyrunner -plugin \path\to\androidviewclient-<version>.jar myscript.py

Related

Mismatched input 'result expecting RPAREN: While running jython script

I have been trying to run a jython script which installs a package and opens a activity and then takes its screen shot and finally saves it in a File. I am using the following Code to do this:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection()
device.installPackage('F:\jind\Example.apk')
package= 'com.android.example'
activity= 'com.android.example.main_activity'
runComponent= package + '/' + activity
device.startActivity(component=runComponent)
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)
result=device.takeSnapshot()
result.writeToFile('F:\jind\lk.png','png')
After running the script. writeToFile() is giving Error saying 'mismatched input 'result' expecting RPAREN
Thanks in Advance
The RPAREN error is due to parameter error in result.writeToFile('F:\jind\lk.png','png').
Its because of escape character.
use result.writeToFile('F:\\jind\\lk.png','png') instead to escape '\'.
Hope it will work.
fix missing ')':
device.press()

Issues using a variable in monkeyrunner's ".installPackage()"

I haven't been able to find much on this topic.
I am trying to automate application testing, to where I place an app in a particular folder and I run the script: monkeyrunner.bat -v ALL myscript.py, and the script executes on whatever apk is in the folder called apkrepository. This makes it to where I do not have to alter my python script every time I test a new application.
The part where I am running into trouble is I am trying to use a variable for device1.installPackage()
See below for the code leading upto it.
installme = os.popen(r'dir C:\users\uname\desktop\apkrepository /A:-d /B').read()
print installme
# => com.application.android.apk #or whatever the package name is
filepath = r'C:\users\uname\desktop\apkrepository'
androidapp = filepath + '\\\' + installme
print androidapp
# => C:\users\uname\desktop\apkrepository\com.application.android.apk
#This exactly what I type below manually to get it to work
device1= MonkeyRunner.waitForConnection(15, "emulator-5554")
#Emulator was started in previous section of code, which is not shown here.
device1.installPackage(androidapp)
#DOES NOT WORK!!
device1.installPackage('c:\users\uname\desktop\apkrepository\com.application.android.apk')
#The only way it works seems to be to write the path in manually everytime.
I have tried many different ways to get this to work correctly, and I wasn't sure if it was something in the way(s) I was/were trying to do it. If the variable prints the correct file path I do not see how it would have issues working. This is probably something really easy, but this is where I am stuck. The error it gives:
E/Device: Error dyring Sync: Local Path does not exist. Error installing package C:\users\uname\desktop\apkrepository\com.application.android.apk
I am using windows 7 64 bit with python2.7 and the android sdk.
Thank you for any input/assistance provided! I have been stumped by this for a couple days.
Variables DO work for other places (monkeyrunner affiliated classes), like the device1.startActivity(component=runcomponent), where runcomponent is a combination of package + activity variables. Also note: I showed both device1.installPackages side by side for easy viewing. I do not run both in a row on my script.
I tried the same in linux machine, it works. Please have a look at the monkeyrunner script.
#! /usr/bin/env monkeyrunner
import re
import sys
import os
import java
import glob
import os
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device1= MonkeyRunner.waitForConnection(15, "emulator-5554")
mydir="/home/user/apk"
os.chdir(mydir)
for files in glob.glob("*.apk"):
print files
print "path " ,os.path.abspath(mydir+"/"+files)
device1.installPackage(mydir+"/"+files)

Monkey Runner import giving error

i created an empty file an named it something.py, and then i just copied the lines of code from the android developer website. However, if i try to run it, i get an
ImportError: No module named com.android.monkeyrunner
Is there something i am missing? There doesn't seem to be anything at the android developer website that addresses this issue. Here are the lines of code
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection()
device.installPackage('myproject/bin/Stamp_Client.apk')
package = 'com.example.main'
activity = 'com.example.Stamp_Client'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)
device.press('KEYCODE_BUTTON_SELECT','DOWN_AND_UP')
device.press('KEYCODE_U','DOWN_AND_UP')
device.press('KEYCODE_S','DOWN_AND_UP')
device.press('KEYCODE_E','DOWN_AND_UP')
device.press('KEYCODE_R','DOWN_AND_UP')
device.press('KEYCODE_ENTER','DOWN_AND_UP')
device.press('KEYCODE_P','DOWN_AND_UP')
device.press('KEYCODE_A','DOWN_AND_UP')
device.press('KEYCODE_S','DOWN_AND_UP')
device.press('KEYCODE_S','DOWN_AND_UP')
device.press('KEYCODE_ENTER','DOWN_AND_UP')
device.press('KEYCODE_ENTER','DOWN_AND_UP')
Make sure you run:
$ monkeyrunner yourfile.py
instead of:
$ python yourfile.py
I guess you are missing the shebang:
#! /usr/bin/env monkeyrunner
that is if you are using Linux or OSX, for Windows you probably have to create a batch file.

Applying different commands at different location

I am working in Ubuntu.
I have a bunch of commands (say 10 commands like cmd1, cmd2, cmd3..............cmd10)
I want to write a python script, which can achieve the following:
It should traverse through the directory structure and apply a command
at particular directory path.
The location and the commands are already known to me.
Here is the example how, I want the script to operate.
/local/mnt/myspace/sample1$ cmd1
/local/mnt/myspace/sample2$ cmd2
/local/mnt/myspace$ cmd3
/local/mnt$cmd4
/local/mnt/myspace/sample9$ cmd 8
/local/mnt/myspace/sample3$ cmd10
can someone please help on this.
Maybe something like this:
import subprocess
import os
jobs=[
('/local/mnt/myspace/sample1', 'cmd1'),
('/local/mnt/myspace/sample2', 'cmd2'),
('/local/mnt/myspace', 'cmd3'),
('/local/mnt', 'cmd4'),
('/local/mnt/myspace/sample9', 'cmd', '8'),
('/local/mnt/myspace/sample3', 'cmd10'),
]
for job in jobs:
print "In", job[0], "executing", job[1:]
os.chdir(job[0])
subprocess.Popen(job[1:]).wait()
(just a quick shot)
Look how I have "abused" the apparent mistake at cmd 8 to show how to call programs which take parameters.
import os
pathCommands = {r'C:\Windows':'dir', r'C:\test':'cd..' }
for path, command in pathCommands.items():
os.chdir(path)
os.system(command)
Just use os.chdir(path).
Something like:
import os
from itertools import izip
paths = ('/local/mnt/myspace/sample1', '../sample2',
'../', '../', 'myspace/sample9/', '../sample3']
commands = (func1, func2, func3, func4, func5, func6)
for path, command in izip(paths, commands):
os.chdir(path)
command()
And just put each command in a function.
Edit: I thought it was different Python commands you wanted to run in different directories. If it's different external programs, use:
commands = (['cmd1', 'arg1'], ['cmd2', 'arg2'], ...)
for path, command in izip(paths, commands):
os.chdir(path)
subprocess.call(command)
No reason to use Popen and wait when this is specifically what call is for.

Where is the help.py for Android's monkeyrunner

I just can't find the help.py file in order to create the API reference for the monkeyrunner. The command described at the Android references
monkeyrunner <format> help.py <outfile> does not work when i call monkeyrunner html help.py /path/to/place/the/doc.html.
It's quite obvious that the help.py file is not found and the monkeyrunner also tells me "Can't open specified script file". But a locate on my system doesn't bring me a help.py file that has anything to do with monkeyrunner or Android.
So my question is: Where did they hide the help.py file for creating the API reference?
I cannot find it either. But one can assume that it is simply calling MonkeyRunner.help() with the passed in arguments. If you just want to get something quick use this script I created also named help.py:
#!/usr/bin/env python
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
text = MonkeyRunner.help("html");
f = open('help.html', 'w')
f.write(text);
f.close();
Run it just like any other monkeyrunner script:
$ monkeyrunner help.py
After I have all codes in my machine (i.e, repo sync), it is at mydroid/sdk/monkeyrunner/scripts along with other three:
help.py monkey_playback.py monkey_recorder.py mr_pydoc.py
This is brilliant answer https://stackoverflow.com/a/4470513/551383 but if you really want this file is in android source i.e. http://androidxref.com/4.2_r1/xref/sdk/monkeyrunner/scripts/help.py
http://androidxref.com/source/xref/sdk/monkeyrunner/scripts/help.py
I believe the documentation on the website starts from that script, but I'm pretty sure somebody edits it a bit afterwards as well.
There's an error in monkeyrunner's help documentation (monkeyrunner Built-in Help), you should use parameters in another order:
monkeyrunner help.py <format> <outfile>
And don't forget about specifying a full path to the script, if you're running it outside of the monkeyrunner.bat directory (android monkeyrunner scripts).
If you don't have Repo Sync, described by users above, you can find the sources (including help.py), for example, here: monkeyrunner scripts.
I opened an issue at Google Code (Issue 26259: monkeyrunner Built-in Help Description Error) and I hope that they'll fix it soon.

Categories

Resources