Is it possible to marry WSH (wscript) with nodejs

As QA I use WSH scripts to do auto upload, deployment and some time Web testing in IE. WSH(wscript) with JavaScript can open IE window, activate it and access DOM model to do some actions or verify some expected results. It is kind of Selenium 1.0 approach but does not require JAVA and any envrionment configuration so can be executed on any developers/qa windows machine immidiately.

Recently I found NodeJS and all its abilities, except manipulating with Windows IE DOM. Cannot find the way on how to run my old WSH scripts to test IE DOM and at the same time use some NodeJS modules to parse XMLs or run test report server.

So question: is it possible to run WSH JavaScripts and Node.js and use all goodies from both worlds?
I am afraid, it is not, but hope somebody has workaround…

As workaround, maybe somebody found the way in NodeJS to start IE window access its DOM (…add own js script or run SendKeys to it)!?

I understand that NodeJS is not designed to do windows administrative tasks.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

While not actually marrying as the question requires, @o_nix in the comments made the suggestion for https://github.com/idobatter/node-win32ole.

I’d suggest that this module satisfies many issues for people arriving here from Google (as I did).
It is also available from npm here: https://www.npmjs.com/package/win32ole

The module also has quite a few examples, such as:
https://github.com/idobatter/node-win32ole/blob/dev0.1.3/examples/activex_filesystemobject_sample.js

  var win32ole = require('win32ole');
  . . . 
  var withReadFile = function(filename, callback){
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    var fullpath = fso.GetAbsolutePathName(filename);
    var file = fso.OpenTextFile(fullpath, 1, false); // open to read
    try{
      callback(file);
    }finally{
      file.Close();
    }
  };
  var withEachLine = function(filename, callback){
    withReadFile(filename, function(file){
//    while(file.AtEndOfStream != true) // It works. (without unary operator !)
//    while(!file.AtEndOfStream) // It does not work.
      while(!file.AtEndOfStream._) // *** It works. oops!
        callback(file.ReadLine());
    });
  };
  withEachLine(testfile, function(line){
    console.log(line);
  });

So, to me, this is as good as marrying old WSH scripts as anything. Tweaks will be involved of course, but then it’s goodbye WSH.

More specifically, to the question at hand, this is a snippet of a demo IE script:
https://github.com/idobatter/node-win32ole/blob/master/examples/ie_sample.js

  var win32ole = require('win32ole');
  . . . 
  var ie = new ActiveXObject('InternetExplorer.Application');
  ie.Visible = true;
  for(var i = 0; i < uris.length; ++i){
    console.log(uris[i]);
    ie.Navigate(uris[i]);
    win32ole.sleep(15000, true, true);
  }
  ie.Quit();

Solution 2

WSH is a different runtime and set of libraries from nodejs. The only simple solution I can think of for your use case is to use child_process to run your WSH scripts and capture the output and parse it.

The other options are:

  • Look at other browser automation modules – selenium is not your only option, there are also headless browsers, which may appease the situation: zombiejs, phantomjs etc
  • Write native bindings to the APIs used by WSH for nodejs
  • Merge the event loops of WSH and nodejs, and expose WSH’s API to nodejs: not a good idea for such a narrow use case.

Solution 3

The benefit of firing a child process is that WSH is able to issue HTTP requests. And Node, obviously, can serve HTTP.

One can imagine a Node.js library that would completely proxy ActiveXObject that way and give Node.js all the same powers as WSH.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply