Getting Started

Bash


#!/bin/bash

DEVICE=LGH400XXYZ

APP=com.google.youtube

EMAIL=hello@example.com



gb --record $DEVICE $APP $EMAIL --autosync



sleep 10



gb --stop $DEVICE



This example will collect data for the app com.google.youtube for 10 seconds and will automatically upload the session to the GameBench Web Dashboard when the --stop command is executed




Python


#!/usr/bin/python

import sys
import atexit
import base64
import logging
import os
import re
import subprocess
from time import sleep
import argparse

#Setup command-line arguments

parser = argparse.ArgumentParser(description='Start the GameBench Automation Interface and start recording.')
parser.add_argument('--email', required=True, help='Enter the email of the account to use for recording.')
parser.add_argument('--app', required=True, help='Enter the app package name to record')
parser.add_argument('--host', help='GameBench Server Address')
parser.add_argument('--port', type=int, help='GameBench Server Port')
parser.add_argument('--time', required=True, type=int, help='Enter the number of seconds to record for')

args = parser.parse_args()

namespace = parser.parse_args(sys.argv[1:])

#Required functions


#Split a multi-line string into an array
def split_lines(s):
    return re.split(r'[\r\n]+', s.rstrip())

#Set the Host Address for GBA to point to
def set_host():
    with open(os.devnull, 'wb') as devnull:
	process = subprocess.Popen(GBPath + ' --setprop serverHost' + namespace.host, shell=True, stdout=subprocess.PIPE)
	process.wait()
	if process.returncode == 0:
		print "Updated Server Host to: " + namespace.host
	else:
		print "There's been an issue updating the Server Host. Status Code: " + str(process.returncode)

#Set the Host Port for GBA to point to
def set_port():
    with open(os.devnull, 'wb') as devnull:
	process = subprocess.Popen(GBPath + ' --setprop serverPort' + namespace.port, shell=True, stdout=subprocess.PIPE)
	process.wait()
	if process.returncode == 0:
		print "Updated Server Port to: " + namespace.port
	else:
		print "There's been an issue updating the Server Port. Status Code: " + str(process.returncode)

#Gets an array of devices
def get_devices(adb_path='adb'):
    with open(os.devnull, 'wb') as devnull:
        subprocess.check_call([adb_path, 'start-server'], stdout=devnull,
                              stderr=devnull)
    out = split_lines(subprocess.check_output([adb_path, 'devices']))
    # The first line of `adb devices` just says "List of attached devices", so
    # skip that.
    devices = []
    for line in out[1:]:
        if not line.strip():
            continue
        if 'offline' in line:
            continue
        serial, _ = re.split(r'\s+', line, maxsplit=1)
        devices.append(serial)
    return devices

#Runs an app
def run_app(packageName, adb_path='adb'):
    with open(os.devnull, 'wb') as devnull:
        process = subprocess.Popen(adb_path + ' shell monkey -p '+ packageName + ' -c android.intent.category.LAUNCHER 1', shell=True, stdout=subprocess.PIPE)
	process.wait()
	print process.returncode
    
#Start Server
def start_server():
    with open(os.devnull, 'wb') as devnull:
	process = subprocess.Popen(GBPath + ' --start-server', shell=True, stdout=subprocess.PIPE)
	process.wait()
	if process.returncode == 0:
		print "Server Started"
	else:
		print "There's been an issue. Status Code: " + str(process.returncode)

#Start testing
def start_test():
    with open(os.devnull, 'wb') as devnull:
	process = subprocess.Popen(GBPath + ' --record '+ DEVICES[0] + ' ' + APP + ' ' + EMAIL, shell=True, stdout=subprocess.PIPE)
	process.wait()
	if process.returncode == 0:
		print "Recording Started"
	elif process.returncode == 201:
		print "The GBA Server hasn't been started. Status Code: " + str(process.returncode)
	elif process.returncode == 200:
		print "Recording is already started on this device. Status Code: " + str(process.returncode)

#Stop testing
def stop_test():
    with open(os.devnull, 'wb') as devnull:
	process = subprocess.Popen('gb --stop ' + DEVICES[0], shell=True, stdout=subprocess.PIPE)
	process.wait()
	if process.returncode == 0:
		print "Recording Finished"
	else:
		print "There's been an issue. Status Code: " + str(process.returncode)


#Start the process:


#Check for which GBA binary to use
if os.name == 'nt':
	GBPath = 'gba'
else:
	GBPath = 'gb'

#Get all devices
DEVICES = get_devices()

#Check if GBA is installed and accessible
if os.path.isfile(GBPath):
	#Check if there are any connected devices
	if not DEVICES:
		#If No devices are connected
		print("No Devices Connected.")
	else:
		#If there are connected devices, check for host/port options.
		if namespace.host:
			set_host
		if namespace.port:
			set_port

		#Print out the first device in the list
		print "Testing on: %s" % DEVICES[0]

		#Set APP based on argument
		APP = namespace.app

		#Set EMAIL based on argument
		EMAIL = namespace.email
		print "Testing for %s" % EMAIL

		#Start the server
		start_server()

		#Run the application
		run_app(APP)

		#Start the recording
		start_test()

		#Wait for the appropriate amount of time
		sleep(namespace.time)

		#Stop the recording
		stop_test()
else:
	#If GBA isn't installed, then print message
	print "GameBench Automation Interface is not installed."

This Python script takes several options, and checks to ensure that devices are connected and the Automation Interface is installed.

Still need help? Get in touch!
Last updated on 23 Jul 2019