Scripting Pearson's TestNav on Windows Using Python

Getting Pearson’s Web-based TestNav to work on OS X was challenging enough, but getting it to work on Windows is also difficult.  Fortunately, Windows can run Python–even though it isn’t installed by default like OS X.  And Python is great for scripting and automating mundane tasks.  Pair that with some Group Policy Objects (GPOs), and you have saved yourself a lot of time!

The Problem

If you try to run any of Pearson’s TestNav sites, you get inundated with Java warning dialogs.  In addition, you need to disable pop-ups, and add the TestNav URLs to whitelists.  Challenging if you have to do this to hundreds of computers.

Requirements

  • Install Python (3.4.2 in my setup)
  • Install pywin32 (for simulating mouse clicks and keystrokes)
  • Create GPOs to add all TestNav sites to the Java exception list
  • Create GPOs to disable pop-ups
  • Write scripts to open the URLs
  • Write scripts to dismiss the annoying Java dialogs
  • Write scripts to click the Start Test button on the TestNav system check site
  • Write scripts to keystroke user credentials into the sample test site

How I Prepared My Windows Machines For Pearson’s TestNav

First, I needed to install Python and pywin32.  I’m familiar with Python, so despite some trouble getting it installed, it was faster to write the scripts in this language than trying to learn a Windows one.  This would allow me to automate a lot of the mundane tasks Pearson wants me to do.

We use Altiris 6.9 DS, which is an oldie, but a goodie.  Using it, I copied the installers to the machines and then manually installed them on each machine.  I did not want to do this, but I already spent a whole day trying to get Altiris to install them properly, but pywin32 doesn’t have a /silent  option and the Python .msi  would only install halfway if using Altiris (not uncommon with this older desktop management software).

[UPDATE]: I wish I had known about Chocolatey before going through these manual steps.  It could have been this easy:

Install Chocolatey

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "(iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))) >$null 2>&1" && SET PATH=%PATH%;%ALL-- USERSPROFILE%\chocolatey\bin

Install Python

choco install python choco install pywin32

The Scripts (To Do The Stuff That Would Take A Human “Forever”)

Once Python was installed, I began working on the scripts.  Below are the three that I came up with.  You can download them or create your own based off the information provided below:

Building The Scripts

Open URLs

(System Checker, sample test site, and the real test site)

To open a URL in the default Web browser, use this script:

# Open a URL once a user is logged in
import webbrowser
new = 2 # open in a new tab, if possible

system_check = "https://proctorcaching.pearsonaccess.com/ems/systemCheck/systemCheck.jsp?acc=mn"
sample_test = "https://testnav.com/mnqc/testnav-7.5.22.36/"
testnav = "http://testnav.com/mn/testnav-7.5.22.31/selfRegistration.htm"

webbrowser.open(system_check,new=new)

Replace system_check  in the last line (highlighted), with whichever variable (URL) you want to use.

Make The Browser Full-screen

(So Clicking Coordinates Is Consistent Across Devices)

To make the browser full screen (after it is launched), run this script.  Note the two highlighted lines–there are two ways to go full screen: pushing F11 or Alt+Space+X.  Comment out or uncomment whichever one works for your browser:

# Keystrokes
import win32api
import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
# Alt+Space+X or F11 makes it full screen
win32api.Sleep(2500)
#shell.SendKeys("% X")
shell.SendKeys("{F11}")

Click Checkboxes And Dismiss Java Dialogs

Use this script to click the checkboxes and “Run” buttons of the annoying dialogs that pop-up.  The coordinates are assuming the browser is full-screen and the resolution of the monitor is 1920 x 1080.

# Click coordinates after the browser is full screen
import win32api, win32con
from sleep import time

def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MO-- USEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MO-- USEEVENTF_LEFTUP,x,y,0,0)

# 1920 x 1080 monitor
# Check "Do not show this"
click(728,560)
sleep(1)
# Click "Run"
click(1095,608)
sleep(5)

Finding The Coordinates

You will need to download Mofiki’s Coordinate Finder in order to locate where the coordinates are.  Once the app is running, just click the spacebar on the area you want to click and it will give you the coordinates.  Plug those into the script and you are all set.

Keystroke Username And Password Fields

Finally, if you want to enter the username and testcode credentials into the sample test site, use this script:

# Click Username field
click(1058,278)
sleep(1)
shell.SendKeys("username")
sleep(1)
shell.SendKeys("{TAB}")
sleep(1)
shell.SendKeys("testcode")
sleep(1)
shell.SendKeys("{ENTER}")

These scripts can be run locally by saving them as .py  files.  Students could just double-click them on test day.  While setting this up, I put them on the Public Desktop folder so I could log in, double-click them, and make sure everything was working as expected.

Creating The GPOs

Much to my behest, I decided to use Internet Explorer.  Fortunately, my network administrator was able to create a DeploymentRuleset.jar with exceptions for the TestNav sites.  It took some trial and error, but we were eventually able to disable the prompts for those pages.  What’s nice is that the DeploymentRuleset.jar can be used on OS X as well.  Below are the locations to put it:

  • Windows: C:\Windows\Sun\Java\Deployment\DeploymentRuleSet.jar
  • OS X: /Library/Application Support/Oracle/Java/Deployment/DeploymentRuleSet.jar

This saved me from having to “click” each popup.  That’s not to say the Python scripts won’t come in useful; I can deploy them to the machines and just have students double-click them to start the test.  No need to type in a URL or navigate to a bookmark.

Troubleshooting

First, check what what Windows thinks .py  files are with this command:

assoc .py

If it returns

.py=Python.File

that is good.  Next, check how Windows will execute these type of files with this command:

ftype Python.File

In my case, I had Python 3.4 installed and it returned this:

Python.File="c:\Windows\py.exe" "%1" %*

If these commands above didn’t work, Python probably did not get installed properly.

Resources

  • http://pythonconquerstheuniverse.wordpress.com/2010/10/16/how-to-open-a-web-browser-from-python/
  • http://stackoverflow.com/questions/1934675/how-to-execute-python-scripts-in-windows
  • http://community.logmein.com/t5/Free/Chrome-Java-plugin-Always-having-to-click-quot-Always-allow-quot/td-p/63314
  • http://sourceforge.net/p/pywin32/feature-requests/86/
  • http://stackoverflow.com/questions/1181464/controlling-mouse-with-python
  • http://stackoverflow.com/questions/3698635/getting-cursor-position-in-python
  • http://bytes.com/topic/python/answers/38702-python-win32-silent-install#post145233
  • http://docs.activestate.com/activepython/2.4/installnotes.html#install_silent
  • http://www.softpedia.com/get/Desktop-Enhancements/Other-Desktop-Enhancements/Mofiki-s-Coordinate-Finder.shtml#download
  • http://stackoverflow.com/questions/11150966/simulating-keystrokes-in-python-using-pywin32
  • http://msdn.microsoft.com/en-us/library/8c6yea83
  • http://magervalp.github.io/2014/02/11/creating-and-signing-deploymentruleset-jar.html
  • http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/security/deployment_rules.html
  • http://windowsitpro.com/systems-management/how-can-i-configure-default-internet-browser
  • http://www.robvanderwoude.com/regedit.php