Is it possible to marry WSH (wscript) with nodejs
Asked Answered
G

3

8

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.

Granuloma answered 29/6, 2012 at 18:41 Comment(2)
Re I found NodeJS and all its abilities, which abilities, specifically, does Node.js have that you would like to "marry" with WSH/Javascript? What do you need WSH/Javascript to do, that it is not able to do?Kamal
github.com/idobatter/node-win32ole allow you to build such bridge.Stimulate
L
9

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();
Lovable answered 22/1, 2015 at 7:20 Comment(2)
Nice, this is definitely the right answer. This didn't exist when I answeredTosspot
this hasn't been updated in a while, and has a python 2 dependency.Circinate
T
3

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.
Tosspot answered 17/8, 2012 at 21:7 Comment(0)
S
0

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.

Suitable answered 29/11, 2012 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.