Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Major refactoring; Now handles test-running, too

In order to prevent existing Android-browser instances from being
  recaptured, the script now kills any browser instances running on the
  emulator and now ALWAYS starts a fresh JsTestDriver server instance.
  This means the script will now ALWAYS fail if a JTD server is
  already running on the specified port.
The script now handles test-running in addition to JTD / Android
  initialization. The JTD server will be killed automatically when the
  tests are complete.
The ability to start only the JTD server has been removed.
  • Loading branch information...
commit a4c8585bf5f87970b30dc44ed1a970a9549685b1 1 parent 7d19f49
@eromba authored
Showing with 76 additions and 85 deletions.
  1. +14 −10 README.md
  2. +62 −75 jtd-android-init.py
View
24 README.md
@@ -1,7 +1,8 @@
# JsTestDriver / Android Emulator Initialization Script
Automates the initialization of JsTestDriver with an Android emulator,
-capturing the emulator's web browser in the JsTestDriver server instance.
+capturing the emulator's web browser in the JsTestDriver server instance
+and running the tests specified on the command line.
## Dependencies
@@ -16,18 +17,21 @@ capturing the emulator's web browser in the JsTestDriver server instance.
## Usage
-To initialize JsTestDriver and the Android emulator:
-`jtd-android-init.py [Path to JTD] [Port] [Path to SDK] [AVD] [Delay]`
+`jtd-android-init.py [Path to SDK] [AVD] [Delay] [Path to JTD] [JTD Port] [JTD Test] [JTD Args]...`
-To initialize only JsTestDriver:
-`jtd-android-init.py [Path to JTD] [Port]`
-
-- **Path to JTD** : The path to your JsTestDriver JAR file
-- **Port** : The port on which to start the JsTestDriver server
- **Path to SDK** : The path to your local Android SDK directory
-- **AVD** : The name of the Android Virtual Device to start
-- **Delay** : The number of seconds to wait for the Android browser to
+- **AVD** : The name of the Android Virtual Device to start
+- **Delay** : The number of seconds to wait for the Android browser to
finish loading the JsTestDriver test page
+- **Path to JTD** : The path to your JsTestDriver JAR file
+- **JTD Port** : The port on which to start the JsTestDriver server
+- **JTD Test** : The JsTestDriver tests to run
+ (This should be a proper value for the JsTestDriver "[--tests](http://code.google.com/p/js-test-driver/wiki/CommandLineFlags#--tests)" option)
+- **JTD Args** : Additional arguments to pass to JsTestDriver when running tests
+
+###Example
+
+`jtd-android-init.py /usr/bin/android-sdk/ Android233 10 JsTestDriver.jar 9876 all --browser /usr/bin/Firefox/firefox.exe`
## Current Limitations
View
137 jtd-android-init.py
@@ -3,7 +3,8 @@
# JsTestDriver / Android Emulator Initialization Script
#
# Automates the initialization of JsTestDriver with an Android emulator,
-# capturing the emulator's web browser in the JsTestDriver server instance.
+# capturing the emulator's web browser in the JsTestDriver server instance
+# and running the tests specified on the command line.
#
# Project Homepage: https://github.com/eromba/JsTestDriver-Android-Init
#
@@ -61,48 +62,38 @@ def AndroidBrowserIsCaptured():
#
-numArgs = len(sys.argv)
-
-if numArgs != 3 and numArgs != 6:
- print("Usage: jtd-android-init.py [Path to JTD] [Port] [Path to SDK] [AVD] [Delay]")
+if not len(sys.argv) >= 7:
+ print("Usage: jtd-android-init.py [Path to SDK] [AVD] [Delay] [Path to JTD] [JTD Port] [JTD Test] [JTD Args]...")
exit(1)
-JTD_PATH = sys.argv[1]
-JTD_PORT = sys.argv[2]
-
-if numArgs == 6:
- SDK_PATH = sys.argv[3]
- AVD_NAME = sys.argv[4]
- DELAY = sys.argv[5]
- ADB_PATH = os.path.abspath( os.path.join(SDK_PATH, "platform-tools/adb") )
- EMULATOR_PATH = os.path.abspath( os.path.join(SDK_PATH, "tools/emulator") )
+SDK_PATH = sys.argv[1]
+AVD_NAME = sys.argv[2]
+DELAY = sys.argv[3]
+JTD_PATH = sys.argv[4]
+JTD_PORT = sys.argv[5]
+JTD_TEST = sys.argv[6]
+ADB_PATH = os.path.abspath( os.path.join(SDK_PATH, "platform-tools/adb") )
+EMULATOR_PATH = os.path.abspath( os.path.join(SDK_PATH, "tools/emulator") )
#
-# Start the JsTestDriver server in a new window if it is not already running
+# Start the JsTestDriver server
#
try:
print("Detecting JsTestDriver server on port " + JTD_PORT + "...")
f = openUrl("http://localhost:" + JTD_PORT)
+
+ # If the server is not running, an exception will be thrown before this code is executed
print("Server detected")
- newServerInstance = False
+ print("\nPlease stop the JsTestDriver server to flush any existing captured browsers")
+ sys.exit(1)
+
except (urlError):
print("Server not detected\nStarting JsTestDriver server...")
- newServerInstance = True
- if os.name == 'nt':
- subprocess.Popen(["cmd", "/c", "start", "java", "-jar", JTD_PATH, "--port", JTD_PORT])
- else:
- subprocess.Popen(["xterm", "-e", "java", "-jar", JTD_PATH, "--port", JTD_PORT])
+ jtdProc = subprocess.Popen(["java", "-jar", JTD_PATH, "--port", JTD_PORT])
print("JsTestDriver started")
- if numArgs == 3:
- print("Waiting 1 second for the server to start...")
- time.sleep(1)
-
-if numArgs == 3:
- print("\nInitialization complete")
- exit(0)
#
@@ -118,44 +109,27 @@ def AndroidBrowserIsCaptured():
output = adbProc.communicate()[0].decode("utf-8")
if output.find("emulator") == -1:
-
- print("Emulator not detected")
-
- # If we are starting a new emulator instance but have an existing server instance,
- # we must restart the server in order to flush any previously-captured browsers.
- if not newServerInstance:
- print("\nPlease close JsTestDriver to flush any existing captured browsers.")
- sys.exit(1)
-
- print("Starting emulator...")
- newEmulatorInstance = True
- emulatorProc = subprocess.Popen([EMULATOR_PATH, "-avd", AVD_NAME], stdout=subprocess.PIPE)
- print("Emulator started\n")
+ print("Emulator not detected\nStarting emulator...")
+ subprocess.call([EMULATOR_PATH, "-avd", AVD_NAME], stdout=subprocess.PIPE)
+ print("Emulator started")
else:
-
print("Emulator detected\n")
- # If the emulator's browser is NOT already captured, we treat this as a new
- # emulator instance.
- print
- if AndroidBrowserIsCaptured():
- newEmulatorInstance = False
- else:
- newEmulatorInstance = True
-
- # If we are starting a new server instance but have an existing emulator
- # instance, we open a blank page in the emulator's browser. This will cause a
- # hard-refresh when we open http://10.0.2.2:.../capture later on, which in
- # turn will cause the new server instance to re-capture the browser.
- if newServerInstance:
- subprocess.call([ADB_PATH, "shell", "am", "start", "-a",\
- "android.intent.action.VIEW", "-d", "about:blank"], stdout=subprocess.PIPE)
-
- # Press the "Home" button to return to the home screen
- # This prevents the browser's menu from appearing when we try to unlock
- # the screen by pressing the Menu button.
- subprocess.call([ADB_PATH, "shell", "input", "keyevent", "3"])
+ # Kill any existing Android browser instances
+ # This is necessary to prevent old JsTestDriver pages from being recaptured.
+ print("Killing any existing Android browser instances...")
+ psProc = subprocess.Popen([ADB_PATH, "shell", "ps"], stdout=subprocess.PIPE)
+ output = psProc.communicate()[0].decode("utf-8")
+ killed = False
+ for line in output.splitlines():
+ if line.find("browser") != -1:
+ pid = line.split()[1]
+ subprocess.call([ADB_PATH, "shell", "kill", pid], stdout=subprocess.PIPE)
+ killed = True
+ print("Browser instance " + pid + " killed")
+ if not killed:
+ print("No browser instances detected")
#
@@ -186,22 +160,35 @@ def AndroidBrowserIsCaptured():
"android.intent.action.VIEW", "-d", "http://10.0.2.2:" + JTD_PORT + "/capture"],\
stdout=subprocess.PIPE)
-# Wait until the Android browser is captured (if necessary)
+# Wait until the Android browser is captured
# We check the server's status page every second until it reports that there
# is at least 1 captured Android browser.
-if newEmulatorInstance:
- print("Capturing Android browser...")
- while not AndroidBrowserIsCaptured():
- time.sleep(1)
- print("Browser captured")
-else:
- print("Browser already captured")
+print("Capturing Android browser...")
+while not AndroidBrowserIsCaptured():
+ time.sleep(1)
+print("Browser captured")
-# If the browser is newly-captured, we must wait for the JavaScript on the
-# JsTestDriver page to initialize.
-if newServerInstance or newEmulatorInstance:
- print("Waiting " + DELAY + " seconds for the browser to finish loading...")
- time.sleep(float(DELAY))
+# We must wait for the JavaScript on the JsTestDriver page to initialize.
+print("Waiting " + DELAY + " seconds for the browser to finish loading...")
+time.sleep(float(DELAY))
print("\nInitialization complete")
+
+#
+# Run JsTestDriver tests
+#
+
+
+print("\nRunning tests:\n")
+
+# Invoke JsTestDriver with the last arguments given on the command-line
+testArgs = ["java", "-jar", JTD_PATH, "--tests", JTD_TEST] + sys.argv[7:]
+
+testProc = subprocess.Popen(testArgs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+testResults = testProc.communicate()[0].decode("utf-8")
+print(testResults)
+
+# Kill the JsTestDriver server
+jtdProc.kill()
+
Please sign in to comment.
Something went wrong with that request. Please try again.