Running monkeyrunner python script with mySQL - android

I am trying to use MySQL to store my results with my monkeyrunner script. I am getting this error:
import MySQLdb
ImportError: No module named named MySQLdb
I write my monkeyrunner script using python. I run the scripts on android-sdk/tools folder where monkeyrunner is located. And I already setup python-mySQL connection, it's working fine when I run python scripts inside Python27 folder(not inside Android)
How do I import MySQLdb to my monkeyrunner script?
Thanks.

Monkeyrunner uses jython, not python. Hence, you are able to connect to mysqldb using python scripts. Not using the jython script.
From monkeyrunner, to access mySQL you should use zxJDBC package for jython.
Add JDBC driver in your monkeyrunner class path.
The zxJDBC package provides a nearly 100% Python DB API 2.0 compliant interface for database connectivity in Jython
And following code should get you to connect to the mysql database.
from com.ziclix.python.sql import zxJDBC
params = {}
params['serverName'] = 'localhost'
params['databaseName'] = 'ziclix'
params['user'] = None
params['password'] = None
params['port'] = 3306
db = apply(zxJDBC.connectx, ("org.gjt.mm.mysql.MysqlDataSource",), params)
Below link can elaborate more on this.
http://www.jython.org/archive/21/docs/zxjdbc.html

Keep it simple. Instead of dealing with jars, versions, and other stuff just use the command line:
#! /opt/android-sdk/tools/monkeyrunner
import subprocess
USER = 'myuser'
PASSWORD = 'mypass'
DB = 'mydb'
p1 = subprocess.Popen([ "mysql", "--user=%s" % USER, "--password=%s" % PASSWORD, DB ], stdin=subprocess.PIPE)
print p1.communicate('INSERT INTO t1 VALUES(1, "other")')

Related

Can not simultaneously run monkeyrunner scripts (e.g. two monkeyrunner process) on multi device

test.py script content:
import ....
device = MonkeyRunner.waitForConnection(10,sys.argv[1])
device.startActivity(component='package/activity')
'''
some monkeyrunner events
'''
I have two device , labled device1-id and device2-id
run the monkeyrunner test.py device1-id &
run the monkeyrunner test.py device2-id &
I found some events in device2-id were sent to device1-id. I don't know why ?
I noticed some tutorials , they said , if run monkeyrunner on more devices, could write the script like below:
device1 = MonkeyRunner.waitForConnection(10,device1-id)
device2 = MonkeyRunner.waitForConnection(10,device2-id)
device1.actions
device2.actions
but this wasn't what I need. Anybody know why the monkeyrunner behaves this ?
What I need is that , I have one script , would run the same script on multi device simultaneously .
You have to specify the monkey port so you may want to use a command line argument like so
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import sys
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection(timeOut,"emulator-"+ sys.argv[1])
MONKEYRunner Actions . . . .
NOTE: sys.arv[0] is always the test file
Call by entering the following on the command line:
monkeyrunner test.py PortNumber
I believe that Monkeyrunner is not thread safe.
To test this, create 2 scripts, hardcoding the deviceId into each.
Start each script:
In windows, use the "start script1" and then "start script2"
In Unix use "script1 &; script2 &"
Notice that script1 fails with errors after it looks like it started working just fine. and that SCript2 also fails to do what it was intended to do because it gets commands from both windows.

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)

how to run external program inside a monkeyrunner code?

I have a monkeyrunner code (file extension is .py) which is working fine. I am using CentOS. Now, I need to run a an external program and I need to insert that code in the monkeyrunner code. how do I do it? What do I need to import and what is the command?
You should use the subprocess module.
import subprocess
# Your code ...
# Call the external program/ script:
subprocess.call('your script', shell = True)

Run python Script in android application

I want to get list of installed software on remote computer.For that I want to use python script in my android application.Now,I have a python script which is getting the list of installed software on remote computer.But,I don't know how to make it supported in android.
For this, I found SL4A android Scripting here . So, I tried to run my python script in android device using SL4A.But,It's not working and giving me error because some packages like win32.client is missing.I don't know more about SL4A so I don't know how to convert my python script in Android supported form.So,anyone have any idea or code please suggest me.....
Also If anyone have another way to get installed software list from remote Pc then please suggest...
Below is my python script
import wmi
from winreg import (HKEY_LOCAL_MACHINE, KEY_ALL_ACCESS, OpenKey, EnumValue, QueryValueEx)
c = wmi.WMI(computer="PC02",user="admin",password="a#1",namespace="root/default").StdRegProv
result, names = c.EnumKey (hDefKey=HKEY_LOCAL_MACHINE, sSubKeyName=r"Software\Microsoft\Windows\CurrentVersion\Uninstall")
print('These subkeys are found under "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall"\n\n')
separator = "*" * 80
keyPath = r"Software\Microsoft\Windows\CurrentVersion\Uninstall"
count = 0
while count < len(names):
try:
print(separator+'\n')
path = keyPath + "\\" + names[count]
key = OpenKey(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS)
temp = QueryValueEx(key, 'DisplayName')
display = str(temp[0])
print (" Name: "+display+'\n',"key:",names[count])
count += 1
except:
print ("Key:",names[count])
count += 1
continue
Run the script on your remote computer, and expose the list of installed software on HTTP, a good way to write this simple web app is to use flask and its development server to serve the list of installed software, then write a python script which uses the native android web interface to fetch the list and display it.
You are having problems with missing libraries because you are importing windows specific ones. At any rate, this isn't the correct script to be running. This script seems to be for a computer, not an android phone.
You're trying to use a Python script that uses Windows Management Instrumentation (WMI), on a device that doesn't have that library.
Sadly, WMI on Python requires the win32 library, which is only available on Windows. I don't think you're going to have much success on checking the installed programs on remote Windows computer from an Android device in this way.
Since WMI is based on WBEM, you may be able to use wbem to access it; you might want to try using pywbem, a pure python wbem library.
Running python scripts is now achievable in gradle system using Tasks
task pythonFile(type:Exec) {
workingDir 'src_path'
commandLine 'python', 'my_script.py'
}

How to import a py file function in monkeyrunner test file

I want to create test cases for my Android application in Monkeyrunner.
I am thinking to create a sub tests case file to do before actual test case. like a install , uninstall functions in a separate py file. How can i call these install/uninstall apk or any other function in my monkeyrunner test case?
I have a successful experiment of calling a function from other imported py file in my py file in PYTHON. but same function cannot called while running through monkeyrunner.
import new
print new.foo()
this is working while running through python but not working in monkeyrunner.
Any solution?
monkeyrunner (jython) and python should import modules exactly the same way, the only difference might be the content of the module search path.
To verify it try:
import sys
print sys.path
in both python and monkeyrunner and see if there are any differences.
If you want to include some path, do
sys.path.append("/path/to/my/new/module")
import new
print new.foo()
and should work.

Categories

Resources