News from MathTran

Just another WordPress.com weblog

More on testing JavaScript code

with one comment

In a previous post I talked about how we would like to test ‘pure’ (i.e. DOM independent) JavaScript functions in an automated fashion.

Spidermonkey offers a command line interpreter and the ability to call scripts like js.exe -f "script1.js" -f "script2.js" -e "run_this();". Unfortunately, both of these features are not available for Windows cscript.exe, cscript can only execute one script file and terminates after it is done.

Koby Kahane has created a workaround to add command line interpreter abilities to cscript (although his code did not work for me). In order to be able to load different script files and execute commands using a single command, we wrote a litle python script which uses the same interface as spidermonkey’s js.exe, create a temporary file, and call cscript with this file.

Here is the code (no error checking) msjs.py:

 

import sys, os
import subprocess

def main(argv):

    tmp_filename = 'msjs_tmp_file.wsf'
    includes = ''
    executes = ''

    for i in range(len(argv)) :
        if argv[i]=='-f' :
            includes += '\n' % argv[i+1]
        elif argv[i]=='-e' :
            executes += '\n\t%s\n\n' % argv[i+1]

    code = '\n%s%s\n' % (includes, executes)

    tmp_file = open(tmp_filename,'w')
    tmp_file.write(code)
    tmp_file.close()

    subprocess.Popen('cscript.exe '+tmp_filename).communicate()[0]

    os.remove(tmp_filename)

if __name__ == '__main__' :
    main(sys.argv[1:])

 

The use of a real temporary file using the tempfile module is not possible, as in Windows you cannot open the temporary file a second time, which cscript would have to do.

Let us assume that we have a file add_def.js

 

// Provide 'print' command in Windows scripting - cscript.
if (typeof WScript == 'object'){
    function print(str){
        WScript.echo(str)
    }
}

add = function(a,b){return a+b;}

 

Now, we can do python msjs.py -f "add_def.js" -e "print(add(2,3));" and it will result in the same code being called by cscript as by js.exe for the same command. The generated temporary file looks like this:

 

<job>
<script language="JScript" src="add_def.js"/>
<script language="JScript">
	print(add(3,4));
</script>
</job>
Advertisements

Written by mystar22

July 8, 2008 at 5:24 pm

Posted in Uncategorized

One Response

Subscribe to comments with RSS.

  1. Hi,

    The interpreter code in my post somehow got the newlines (“\n”) garbled to just “n”. I’ve updated the post and that sample shell should now work for you, I believe.

    Koby Kahane

    July 9, 2008 at 5:16 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: