September 15, 2016
This tutorial explains how to use Genymotion to simulate device location changes. It covers planning a route (e.g., a car trip), generating a script, and automating GPS movements.
What is automation and what can it bring to app development?
Automation is a great way to save time and improve product quality.
- Automated unit tests and functional tests are much faster than manual tests, helping ensure there are no regressions.
- Automation can improve product quality by running the same scripts in different environments, testing on many platforms and devices.
That’s where Genymotion comes into play. Genymotion offers two features to automate testing: GMTool and Genymotion Shell. By combining these two tools, many automation possibilities await.
What is GMTool and what can I do with it?
GMTool is a command line tool that allows you to use every command of Genymotion and virtual devices in order to automate a series of actions. It can be started by running gmtool from a command prompt, from the installation path of Genymotion.
Here are examples of the main commands:
- Look up the device templates list:
- gmtool admin templates
- Choose a template, then create and start a device:
- Creating a device:
- gmtool admin create
- Starting a device:
- gmtool admin start
- Your device is ready to be used!
- When you are done using it, you can stop and delete it using these commands:
- Stopping a device:
- gmtool admin stop
- Deleting a device:
- gmtool admin delete
What is Genymotion Shell and what can I do with it?
As the name indicates, Genymotion Shell is a shell which allows you to script the modification of sensor statuses and values. You can interact with different kinds of sensors, such as battery, GPS, Network, phone, baseband, etc. For example, you can use Genymotion Shell to activate the GPS and set your own values for latitude, longitude, and altitude:
- Activate the GPS:
- gps setstatus enabled
- Set latitude:
- gps setlatitude 25.2289
- Set longitude:
- gps setlongitude 145.4578
With these simple commands we can imagine multiple tests like calculating the distance between two points, simulating a location, simulating a trip step by step (by foot, by car, by train, etc.).
Let’s see step by step how we can create and emulate a trip by car
1. Choose a programming language
For this example we choose to use Python.
2. Plan your route
We can easily do that using Google Maps. So head over to Google Maps, select the car option, and start planning a route. Once your route is defined, copy the page URL link in the address bar to your clipboard.
3. You need to convert your route to a .gpx file
GPX is an XML based format for GPS tracks. To convert your route we’d propose you visit gpsvisualizer.com/convert_input. You can ignore most of the available options. Just select GPX and paste the Google Maps URL into the field labeled “provide the URL of a file on the Web” and press the Convert button. A new page is displayed which shows you the .gpx content and gives you the option to download it.
4. You need something to be able to manipulate your .gpx file
Using Python we can use the gpxpy library, which allows us to parse and manipulate GPX files. The main goal of this manipulation of a GPX file is to get all GPS coordinates of your route (latitude and longitude) to finally set them in a device using Genymotion Shell.
We can obtain something like this:
from time import sleep
import subprocess
import gpxpy.gpx
""" Parsing an existing file: """
gpx_file = open('sample_route.gpx', 'r')
gpx = gpxpy.parse(gpx_file)
proc = subprocess.Popen(
["genyshell"],
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT
)
sleep(2)
proc.stdin.write(bytes("gps setstatus enabled\n", "utf-8"))
proc.stdin.flush()
""" Convert 100km/hour in meter/second """
speed = (100 * 1000) / 3600
previous_point = None
try:
for track in gpx.tracks:
for segment in track.segments:
for i, point in enumerate(segment.points):
if previous_point is not None:
distance = point.distance_2d(previous_point)
duration = distance / speed
sleep(duration)
command = "gps setlatitude {}\ngps setlongitude {}\n".format(
point.latitude, point.longitude).replace(".", ",")
proc.stdin.write(bytes(command, "utf-8"))
proc.stdin.flush()
previous_point = point
finally:
proc.stdin.write(bytes("exit\n", "utf-8"))
proc.stdin.flush()
proc.terminate()
proc.wait()
Some explanations
- Line 8 and 9: open and parse the GPX file to create a GPX object.
- Lines 10 to 15: create a subprocess calling Genymotion Shell.
- Lines 16 to 33: activate the GPS and use the GPX object to get latitude and longitude points. Then set these values to Genymotion Shell.
- Line 28: Do a sleep during X seconds (more on this below).
- Line 34 to 38: we properly close our subprocess.
Why are we using a X second sleep line 28?
Without sleep the script would run almost instantly so as to emulate a certain speed (here about 100 km/h). Therefore we need to use the sleep() function to wait between each point.
This value (sleep = X) depends on:
- The distance between 2 points
- The speed you want
Considering:
- Speed = Distance / Duration
- Duration of a sleep() = distance between 2 points / Speed
To see the result, do the following:
1. Start Genymotion 2. Start a device 3. Install openGapps corresponding to your Android device version 4. Install and launch Google Maps 5. Run this script
You should see your current position moving on the map following your defined route.
[Image and code blocks showing example output and additional details are omitted in this cleaned version.]
Prev Android 7.0 Nougat – Preview Available in Genymotion Next Genymotion Cloud Is Out! – Here’s What You Need To Know
[End of content excerpted from the article; navigation and footer content omitted per instructions.]