ChanGan
ChanGan

Reputation: 4318

How to run from the real device monkeyrunner script?

I have written this code which is working in the emualator. How do I get it to work in a real device? Is just connecting the device and changing the location (device location of apk) enough?

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

# Connect to the current device
device = MonkeyRunner.waitForConnection()

# Install package
device.installPackage('C:/android-sdk-windows/tools/lib/purchase.apk')

# Run activity
device.startActivity(component='com.mobilenetwork.purchase/.StartPage ')

# Importing time
import time

# Waiting for 10 secs to be launched on the emulator:
time.sleep(10)
device.press('KEYCODE_BUTTON_SELECT','DOWN','')

# Screenshot
time.sleep(10)
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('G:\\Screenshot\\screen_shot.png','png')

Upvotes: 0

Views: 2210

Answers (2)

BlackHatSamurai
BlackHatSamurai

Reputation: 23493

You might also try configuring the package and activity before you connect to the device.

Upvotes: 0

n8schloss
n8schloss

Reputation: 2781

This looks like it should work on a real device just the way it is right now. If you are experiencing issues, you might want to try adding "MonkeyRunner.sleep(n)" (where n is a number) statements. Some devices do not wait for the last task to finish before asking for the next one.

Upvotes: 2

Related Questions