Skip to content

Commit

Permalink
added bettery testing scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcLeclair committed May 20, 2024
1 parent 939f698 commit 6513ab6
Show file tree
Hide file tree
Showing 10 changed files with 1,298 additions and 0 deletions.
87 changes: 87 additions & 0 deletions battery_test/adb_browsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import subprocess
import time

PACKAGE = "com.android.chrome"
ACTIVITY = "com.google.android.apps.chrome.Main"
XML_FILE = "window_dump.xml"

# Test URLs
URLS = [
"https://facebook.com/",
"https://instagram.com",
"https://instagram.com/explore/",
"https://buzzfeed.com",
"https://cnn.com",
"https://tmz.com",
"https://perezhilton.com",
"https://wikipedia.org/wiki/Student%27s_t-test",
"https://searchfox.org/mozilla-central/source/toolkit/components/telemetry/Histograms.json",
]

def run_cmd(cmd):
return subprocess.run(cmd, shell=True, capture_output=True, text=True)

def extract_bounds(xml_file, resource_id):
cmd = f"xmllint --xpath \"string(//node[@resource-id='{resource_id}']/@bounds)\" {xml_file}"
result = run_cmd(cmd)
bounds = result.stdout.strip()
if not bounds:
raise ValueError(f"Could not find bounds for resource-id {resource_id}")

try:
left, top = map(int, bounds.split('][')[0][1:].split(','))
right, bottom = map(int, bounds.split('][')[1][:-1].split(','))
except ValueError:
raise ValueError(f"Failed to parse bounds '{bounds}' for resource-id {resource_id}")

x = (left + right) // 2
y = (top + bottom) // 2
return x, y


def tap(x, y):
run_cmd(f"adb -s R5CTB1NTHSF shellinput tap {x} {y}")
time.sleep(2)

def input_text(text):
for character in text:
run_cmd(f"adb -s R5CTB1NTHSF shellinput text '{character}'")
time.sleep(2)

def scroll_down():
run_cmd("adb -s R5CTB1NTHSF shellinput swipe 500 1500 500 300 500")
time.sleep(3)

def scroll_up():
run_cmd("adb -s R5CTB1NTHSF shellinput swipe 500 300 500 1500 500")
time.sleep(3)

def start_browser(url):
run_cmd(f"adb -s R5CTB1NTHSF shellam start -n {PACKAGE}/{ACTIVITY} -d {url}")
time.sleep(4)

def close_browser():
run_cmd(f"adb -s R5CTB1NTHSF shellam force-stop {PACKAGE}")

def setup():
run_cmd("adb -s R5CTB1NTHSF shelluiautomator dump /sdcard/window_dump.xml")
run_cmd("adb -s R5CTB1NTHSF pull /sdcard/window_dump.xml")

def browse_url(url):
start_browser(url)
setup()
toolbar_x, toolbar_y = extract_bounds(XML_FILE, 'com.android.chrome:id/search_box_text')
tap(toolbar_x, toolbar_y)
input_text(url)
run_cmd("adb -s R5CTB1NTHSF shellinput keyevent 66") # KEYCODE_ENTER
time.sleep(10)
scroll_down()
scroll_up()

def main():
for url in URLS:
browse_url(url)
close_browser()

if __name__ == "__main__":
main()
80 changes: 80 additions & 0 deletions battery_test/adb_navigation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import subprocess
import time

PACKAGE = "com.android.chrome"
ACTIVITY = "com.google.android.apps.chrome.Main"
XML_FILE = "window_dump.xml"

# Test URLs
URLS = [
"https://www.allrecipes.com/",
"https://www.amazon.com/",
"https://www.bing.com/search?q=restaurants+in+exton+pa+19341",
"https://www.booking.com/",
"https://www.dailymail.co.uk/sciencetech/article-9749081/Experts-say-Hubble-repair-despite-NASA-insisting-multiple-options-fix.html",
"https://m.imdb.com/",
"https://support.microsoft.com/en-us",
"https://stackoverflow.com/",
"https://en.m.wikipedia.org/wiki/Main_Page"
]

def run_cmd(cmd):
return subprocess.run(cmd, shell=True, capture_output=True, text=True)

def extract_bounds(xml_file, resource_id):
cmd = f"xmllint --xpath \"string(//node[@resource-id='{resource_id}']/@bounds)\" {xml_file}"
result = run_cmd(cmd)
bounds = result.stdout.strip()
# Parse the bounds "left,top][right,bottom" to get coordinates
left, top = map(int, bounds.split('][')[0][1:].split(','))
right, bottom = map(int, bounds.split('][')[1][:-1].split(','))
x = (left + right) // 2
y = (top + bottom) // 2
return x, y

def tap(x, y):
run_cmd(f"adb -s R5CTB1NTHSF shellinput tap {x} {y}")
time.sleep(2)

def input_text(text):
for character in text:
run_cmd(f"adb -s R5CTB1NTHSF shellinput text '{character}'")
time.sleep(2)

def scroll_down():
run_cmd("adb -s R5CTB1NTHSF shellinput swipe 500 1500 500 300 500")
time.sleep(3)

def scroll_up():
run_cmd("adb -s R5CTB1NTHSF shellinput swipe 500 300 500 1500 500")
time.sleep(3)

def start_browser(url):
run_cmd(f"adb -s R5CTB1NTHSF shellam start -n {PACKAGE}/{ACTIVITY} -d {url}")
time.sleep(4)

def close_browser():
run_cmd(f"adb -s R5CTB1NTHSF shellam force-stop {PACKAGE}")

def setup():
run_cmd("adb -s R5CTB1NTHSF shelluiautomator dump /sdcard/window_dump.xml")
run_cmd("adb -s R5CTB1NTHSF pull /sdcard/window_dump.xml")

def browse_url(url):
start_browser(url)
setup()
toolbar_x, toolbar_y = extract_bounds(XML_FILE, 'com.android.chrome:id/search_box_text')
tap(toolbar_x, toolbar_y)
input_text(url)
run_cmd("adb -s R5CTB1NTHSF shellinput keyevent 66") # KEYCODE_ENTER
time.sleep(10)
scroll_down()
scroll_up()

def main():
for url in URLS:
browse_url(url)
close_browser()

if __name__ == "__main__":
main()
144 changes: 144 additions & 0 deletions battery_test/auto-test-chrome.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/bin/bash
PACKAGE="com.android.chrome"
ACTIVITY="com.google.android.apps.chrome.Main"
XML_FILE="window_dump.xml"

# Test urls
URL_REDDIT="https://www.reddit.com/r/all"
URL_INSTAGRAM="https://www.instagram.com/"
URL_INSTAGRAM_EXPLORE="https://www.instagram.com/explore"
URL_FACEBOOK="https://www.facebook.com"
URL_TMZ="https://tmz.com"
URL_PEREZ="https://perezhilton.com"
URL_WIKI="https://wikipedia.org/wiki/Student%27s_t-test",
URL_SEARCH_FOX="https://searchfox.org/mozilla-central/source/toolkit/components/telemetry/Histograms.json",
URL_BUZZFEED="https://buzzfeed.com"
URL_CNN="https://cnn.com"

# script start
adb shell am start -n "$PACKAGE/$ACTIVITY"
sleep 4

adb shell uiautomator dump
adb pull /sdcard/window_dump.xml
sleep 1

## Using awk to extract the bounds attribute for the specified resource ID
TOOLBAR_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/search_box_text']/@bounds)" "$XML_FILE")
TABS_TRAY_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/tab_switcher_button']/@bounds)" "$XML_FILE")
sleep 1

# Extract and calculate center coordinates in a single line
TOOLBAR_X_COORDINATE=$((($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
TOOLBAR_Y_COORDINATE=$((($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))
TABS_TRAY_BUTTON_X_COORDINATE=$((($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
TABS_TRAY_BUTTON_Y_COORDINATE=$((($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))

# tap to open tabs tray
adb shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE
sleep 2

# take ss
adb shell uiautomator dump
adb pull /sdcard/window_dump.xml

ADD_TAB_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/new_tab_view_button']/@bounds)" "$XML_FILE")
sleep 1

ADD_TAB_BUTTON_X_COORDINATE=$((($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
ADD_TAB_BUTTON_Y_COORDINATE=$((($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))

rm window_dump.xml
adb shell input keyevent KEYCODE_BACK
sleep 1

function tapToFocusToolbar() {
# tap on the url toolbar
adb shell input tap $TOOLBAR_X_COORDINATE $TOOLBAR_Y_COORDINATE
sleep 2
}

function inputTextToToolbar() {
# input url
adb shell input text $1
sleep 2
}

function tapEnterAndWait5s() {
# press enter
adb shell input keyevent 66
sleep 5
}

function tapEnterAndWait10s() {
# press enter
adb shell input keyevent 66
sleep 10
}

function performScrollDown() {
# scroll down
adb shell input swipe 500 500 500 300
adb shell input swipe 500 500 500 300
adb shell input swipe 500 500 500 300
sleep 2
}

function performScrollUp() {
# scroll up
adb shell input swipe 500 300 500 500
adb shell input swipe 500 300 500 500
adb shell input swipe 500 300 500 500
sleep 2
}

function tapToOpenTabsTray() {
# tap to open tabs tray
adb shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE
sleep 2
}

function tapToAddTab() {
# tap to open another tab
adb shell input tap $ADD_TAB_BUTTON_X_COORDINATE $ADD_TAB_BUTTON_Y_COORDINATE
sleep 3
}

function addTab() {
tapToOpenTabsTray
tapToAddTab
}

function simple_browsing_single_site() {
tapToFocusToolbar
inputTextToToolbar $1
tapEnterAndWait10s
performScrollDown
performScrollUp
}

function simple_browsing_two_sites() {
tapToFocusToolbar
inputTextToToolbar $1
tapEnterAndWait10s
performScrollDown
performScrollUp
tapToFocusToolbar
inputTextToToolbar $2
tapEnterAndWait10s
performScrollDown
performScrollUp
}
# at this point our system is ready, the buttons' coordinates are generated

# test starts after this line
simple_browsing_single_site $URL_BOOKING
addTab
simple_browsing_single_site $URL_MOZILLA
addTab
simple_browsing_single_site $URL_IMDB
addTab
simple_browsing_single_site $URL_WIKIPEDIA

# uncomment this line if you want to stop the app
# adb shell am force-stop $PACKAGE
Loading

0 comments on commit 6513ab6

Please sign in to comment.