I'm trying to get monkeyrunner to accept an external .py file. The .py file just has some functions in it, nothing fancy.
mainscript.py:
import unittest
import logging
import sys
sys.path.append("C:\path\to\helperscripts\")
from monkeyHelper import monkeyHelper
#log to STDERR
logging.basicConfig(level=logging.DEBUG)
class TestDepthOneFunctions(unittest.TestCase):
def setUp(self):
mh = monkeyHelper()
self.device = mh.setupDevice()
#monkeyHelper.setupDevice()
def test_myMusic(self):
self.assertEqual(self.device,3)
def tearDown(self):
pass
if __name__ == '__main__':
unittest.main()
monkeyHelper.py:
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import logging
import sys
class monkeyHelper(object):
def __init__(self):
return
def setupDevice(self):
return 3
I've tried just running
#>monkeyrunner.bat mainscript.py
and it doesn't work. I've seen the full command line that monkeyrunner.bat puts out - is there any way to add C:\path\to\helperScripts to that command line? This is what monkeyrunner.bat spits out:
C:\Windows\system32\java.exe -Xmx512m -Djava.ext.dirs=lib\;lib\x86_64 -Dcom.android.monkeyrunner.bindir=..\framework -jar lib\monkeyrunner.jar mainscript.py
the error i'm getting is:
import monkeyHelper
ImportError: No module named monkeyHelper
I've been banging my head against this all day - any help is appreciated!
Since you can't randomly put \ in a string, try:
sys.path.append(r"C:\path\to\helperscripts")
The r means that the string is to be treated as a raw string, which means all escape codes will be ignored.
Also make sure helperMonkey.py is in that path,
If you use monkeyrunner
you can add a folder 'lib' under
C:\Documents and Settings\<user name>\Local Settings\Application Data\Android\android-sdk\tools\lib'
then put your py files under
C:\Documents and Settings\<user name>\Local Settings\Application Data\Android\android-sdk\tools\lib\lib
if your file is called myPyFile.py
just
from myPyFile import *
update:
careful that if you going to update Android SDK tool, back up your file first, last time I update, my file in lib were deleted.
Related
I am getting this error while trying to use jnius's autoclass to get the android webview inside Kivy app. What could be the fix?
Thanks
My code looks like this:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
#from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.android.PythonActivity').mActivity
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
# Clock.schedule_once(self.create_webview, 0) `
# #run_on_ui_thread
# def create_webview(self, *args):
# webview = WebView(activity)
# webview.getSettings().setJavaScriptEnabled(True)
# wvc = WebViewClient();
# webview.setWebViewClient(wvc);
# activity.setContentView(webview)
# webview.loadUrl('http://www.google.com')
class ServiceApp(App):
def build(self):
return Wv()
if __name__ == '__main__':
ServiceApp().run
First, you should check if you are running the code in an Android enviroment as the android.webkit.WebView runs only under Android and not on a PC under Windows or Linux.
Recommended is an Android device and run the app using Buildozer command buildozer android debug deploy run with debugging options.
In case, you want to run it on a PC then you can install the Android VM from Kivy and follow the following steps on https://kivy.org/docs/guide/packaging-android-vm.html:
Download the Kivy / Buildozer VM, in the Virtual Machine section. The
download is 1.2GB. Extract the file and remember the location of the
extracted directory.
Download the version of VirtualBox for your machine from the VirtualBox download area and install it.
Start VirtualBox, click on “File”, “Import Appliance”.
Select the extracted directory, file should be named “Buildozer VM.ovf”
Start the Virtual machine and click on the “Buildozer” icon.
and let in run in kivy.org/docs/guide/packaging-android-vm.html
If you still face the problem of a jnius.JavaException: Class not found 'android/webkit/WebView' exception, then you have to add that jar file to the classpath:
import os
os.environ['CLASSPATH'] = 'absolute/path/file.jar'
In case of the Class not found 'android/webkit/WebView' exception you could either locate the jar on your device or download it and add the path where you store it to the classpath.
Use Pydroid3 to test your apps without compiling them
I was developing a mobile app that can send mqtt messages to AWS Iot. It needs paho-mqtt library. It can be included in buildozer.spec requirements. My problem is in the SSL part, because I need to import SSL in the code which seems to have a problem with the python version running in python-for-android, which is 2.7.2.
The code is below, which works fine on the PC, but on th phone it is not working.
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.label import Label
import paho.mqtt.publish as mqtt
import paho.mqtt.client as mqttclient
#
class MqttTest(App):
def build(self):
topic = "topic1"
my_ca_cert = "RootCA.pem"
my_pri_cert = "my.cert.pem"
my_key_cert = "my.private.key"
try:
import ssl
mqttc = mqttclient.Client("Python_Ex_Pub")
mqttc.tls_set(my_ca_cert,
certfile=my_pri_cert,
keyfile=my_key_cert,
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None)
mqttc.connect("myaddress", 8883)
mqttc.publish(topic, "This is a test pub from Python.")
return Label(text="Hi it works!")
except Exception as e:
import traceback
a=traceback.format_exc()
try:
f1=open("/storage/emulated/0/Download/err.txt","w")
f1.write(str(a))
f1.close()
except:
pass
return Label(text=str (a))
if __name__ == '__main__':
MqttTest().run()
without adding anything related to SSL in buildozer.spec requirements, I get the following error:
no module named _ssl
If I added openssl as one of the requirements, then I get the following error:
'module' object has no attribute 'PROTOCOL_TLSv1_2'
As noted PROTOCOL_TLSv1_2 was added in later Python 2 version than p4a provides.
You can try to build apk with Python 3:
Change your buildozer's requirements line replacing python2 with python3crystax
Download and unpack crystax ndk here
Change your buildozer's android.ndk_path to point unpacked crystax ndk directory
Run buildozer android debug
If you're lucky enough you'll be able to build apk with Python 3 without any other actions.
I encountered the similar issue. I am trying to import pydrive without doing anything at first. Below is from logcat.
I tried 3 os environment, osx, ubuntu, bulldozer vm. All give me the same error.
I/python (13323): File "/Users/macuser/test/.buildozer/android/platform/build/dists/myapp/private/lib/python2.7/site-packages/httplib2/init.py", line 960, in
I/python (13323): AttributeError: 'module' object has no attribute 'HTTPSConnection'
In httplib.py, i found below code.
try:
import ssl
except ImportError:
pass
else:
class HTTPSConnection(HTTPConnection):
"This class allows communication via SSL."
So I suspect ssl not import successfully. Then, check ssl.py and found PROTOCOL_SSLv3 cannot import successfully.
from _ssl import PROTOCOL_SSLv3, PROTOCOL_SSLv23, PROTOCOL_TLSv1
Until here i am not able to proceed further. I am using python3crystax and it still not work as claim in this post.
Since buildozer will download all library separately, i suspect python automatically downloaded by bulldozer is a python version that doesn't include ssl3 support.
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)
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.
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.