You can do it with AndroidViewClient/culebra.
Here is an example you can base your script on (autogenerated by culebra):
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2013-2018 Diego Torres Milano
Created on 2018-08-09 by Culebra v15.4.0
__ __ __ __
/ \ / \ / \ / \
____________________/ __\/ __\/ __\/ __\_____________________________
___________________/ /__/ /__/ /__/ /________________________________
| / \ / \ / \ / \ \___
|/ \_/ \_/ \_/ \ o \
\_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
"""
import re
import sys
import os
import unittest
from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
TAG = 'CULEBRA'
class CulebraTests(CulebraTestCase):
@classmethod
def setUpClass(cls):
cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'debug': {}, 'startviewserver': True, 'compresseddump': True}
cls.options = {'start-activity': None, 'concertina': False, 'device-art': None, 'use-jar': False, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'glare': False, 'dictionary-keys-from': 'id', 'scale': 0.5, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'concertina-config': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'log-actions': False, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': True, 'verbose-comments': False, 'gui': True, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': 'aonoff.py', 'unit-test-method': None, 'interactive': False}
cls.sleep = 5
def setUp(self):
super(CulebraTests, self).setUp()
def tearDown(self):
super(CulebraTests, self).tearDown()
def preconditions(self):
if not super(CulebraTests, self).preconditions():
return False
return True
def testSomething(self):
if not self.preconditions():
self.fail('Preconditions failed')
_s = CulebraTests.sleep
_v = CulebraTests.verbose
self.vc.dump(window=-1)
self.vc.uiDevice.openQuickSettings()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.findViewWithContentDescriptionOrRaise(u'''Airplane mode''').touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.device.press('KEYCODE_BACK')
self.device.press('KEYCODE_BACK')
print >> sys.stderr, 'Now Airplane mode is ON, switching it back to OFF'
self.vc.sleep(5)
self.vc.uiDevice.openQuickSettings()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.vc.findViewWithContentDescriptionOrRaise(u'''Airplane mode''').touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
self.device.press('KEYCODE_BACK')
self.device.press('KEYCODE_BACK')
if __name__ == '__main__':
CulebraTests.main()
Answer from Diego Torres Milano on Stack OverflowHow to control an android phone using python?
control android device/emulator by python - Stack Overflow
Looking to automate an Android app using Python - advice on where to begin?
Android studios emulator is pretty good nowadays, but very likely the best you could do is use Python to trigger touch events. You won't likely be able to interface directly with the app to do things like trades. Android is pretty good at not letting apps inject touch events and could cause security exceptions. Something as small as rext input would very likely entirely break any attempt to try to do an Android only solution. That being said, if the app exposes intents to do things like trading you could very easily use adb or an app to trigger them, but it seems unlikely that a stock trade app would expose those intents.
More on reddit.comHow to control android device using appium and python? - Stack Overflow
I have recently posted my first blog on dev.to. In the blog, I talked about how one can control a mobile phone using python and do multiple tasks like sending WhatsApp messages, calling, screen recording, opening apps and URLs etc.
Here is the link for that: https://dev.to/yash_makan/controlling-mobile-phone-using-python-192o
I've been using Robinhood to invest and am trying to write an automated trading algorithm. Unfortunately, Robinhood is only on Android and doesn't have access to an API for automated trading.
I just had the idea that Android apps can probably be emulated on PC, right? So I'm looking for both:
1) An Android app emulator on PC (free is best but I'll pay for performance if it's necessary)
2) A Python library for automating Android apps on that emulator. I'm thinking Selenium, but for Android infrastructure.
Has anyone had any experience doing this before? Thank you so much!
Edit: Just wanted to clarify that I originally tried Quantopian because that seemed like the perfect solution. Unfortunately, my strategy is a timing strategy and Quantopian only allows the import of external data once a day via CSV.
Android studios emulator is pretty good nowadays, but very likely the best you could do is use Python to trigger touch events. You won't likely be able to interface directly with the app to do things like trades. Android is pretty good at not letting apps inject touch events and could cause security exceptions. Something as small as rext input would very likely entirely break any attempt to try to do an Android only solution. That being said, if the app exposes intents to do things like trading you could very easily use adb or an app to trigger them, but it seems unlikely that a stock trade app would expose those intents.
If you have access to the apk you could use Appium. I have had some decent success with that, it's a bit rough and unfinished for Python.
Or, you could look into adb documentation and write your own wrapper in Python. I haven't looked to see if someone had already done that.
Not sure about appium, but AndroidViewClient/culebra can easily do what you want.
Install it, run culebra with or without GUI (-G) and you will be able to automatically generate python tests or plain scripts.
For example, if you run
$ culebra -GuU --scale=0.5 -o chrome.py
while the device is on the home screen and click the Chrome icon to open it, this script will be generated
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2013-2018 Diego Torres Milano
Created on 2018-04-04 by Culebra v15.1.2
__ __ __ __
/ \ / \ / \ / \
____________________/ __\/ __\/ __\/ __\_____________________________
___________________/ /__/ /__/ /__/ /________________________________
| / \ / \ / \ / \ \___
|/ \_/ \_/ \_/ \ o \
\_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
"""
import re
import sys
import os
import unittest
from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
TAG = 'CULEBRA'
class CulebraTests(CulebraTestCase):
@classmethod
def setUpClass(cls):
cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
cls.kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
cls.options = {'start-activity': None, 'concertina': False, 'device-art': None, 'use-jar': False, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'glare': False, 'dictionary-keys-from': 'id', 'scale': 0.5, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'concertina-config': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'log-actions': False, 'use-regexps': False, 'null-back-end': False, 'auto-regexps': None, 'do-not-verify-screen-dump': True, 'verbose-comments': False, 'gui': True, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'install-apk': None, 'drop-shadow': False, 'output': 'chrome.py', 'unit-test-method': None, 'interactive': False}
cls.sleep = 5
def setUp(self):
super(CulebraTests, self).setUp()
def tearDown(self):
super(CulebraTests, self).tearDown()
def preconditions(self):
if not super(CulebraTests, self).preconditions():
return False
return True
def testSomething(self):
if not self.preconditions():
self.fail('Preconditions failed')
_s = CulebraTests.sleep
_v = CulebraTests.verbose
self.vc.dump(window=-1)
self.vc.findViewWithContentDescriptionOrRaise(u'''Chrome''').touch()
self.vc.sleep(_s)
self.vc.dump(window=-1)
if __name__ == '__main__':
CulebraTests.main()
then you can run it as
$ ./chrome.py
to achieve the same at any time
I think the problem is like mine...want to control an android device or emulator with Appium, may be Mayank Shivhare already try to learn culebra and found the fact that....culebra is still not support by python 3 yet (same problem like me that prefer python 3.7 than python 2.7).
I have the same problem..., what I understood right now is:
All environment should be set up first
The emulator should be run first...before type any code
then run Appium to make a gateway with IP and port.
the missing link is how to connect an emulator to appium via ip and port that provide by appium server.
I can detect the emulator element by run 'uiautomatorviewer.bat' from
sdk\tools\binfolder, the problem is the problem in number 3, is how to send a command to the emulator via appium. so the command to control emulator base on data that given by 'uiautomatorviewer.bat'
coding is about trial and error, so we need live interaction, to build source code. every developer has a personal interest in build a script or source code. so..to make easier, as beginner, we need live interaction that shows every error in every step our script or to make sure that every line of our script that we type is run smoothly in the android device or emulator.
The problem in learn appium is all manual already given finished script...without a really-really basic explanation...in my case, how to connect appium to emulator, send a command from python console, and see the script is work or not...
what I mean is... we need the step by step explanation of script...because every beginner developers need a live 'trial and error' experience to understand how it works...
