0

I am using node.js and selenium to build automated tests. So, when starting the script in one terminal, I use Commander to open another terminal and start selenium when I launch my automated tests. I do that like this when I want to do the testing with Safari:

if(myArgs.indexOf("LOCAL_SAF") != -1)
{   console.log('Starting selenium');
        /* use Commander to open a shell script which opens a terminal and starts Selenium */
        exec('open -a Terminal.app startSeleniumForSafari.sh',
            function (error, stdout, stderr) {
                console.log('stdout: ' + stdout);
                console.log('stderr: ' + stderr);
                if (error !== null) {
                     console.log('exec error: ' + error);
                }
            });
        wait(2500); 
        console.log('Selenium should be started');
        /* get the selenium webdriver executable for safari*/
        safari = require('selenium-webdriver/safari');
        browserUnderTest = new webdriver.Builder()
        .forBrowser('safari')
        .usingServer('http://localhost:4444/wd/hub')
        .build();
    }
    /* 
        THIS IS NOT MY CODE.  This came from
        https://stackoverflow.com/questions/14226803/javascript-wait-5-seconds-before-executing-next-line
        and was simple enough to understand, and suited my purposes.    
    */
    function wait(ms){
        var start = new Date().getTime();
        var end = start;
        while(end < start + ms) {
            end = new Date().getTime();
        }
    }

Rather then have a wait time and then print to the console that 'Selenium should be started', I'd like to somehow detect that it has indeed started, or if there is an error.

Is there a way to check the current status of the selenium server if it was launched in another terminal?

I know that calling http://localhost:4444/wd/hub/status will result in a response with JSON like {"state":"success","sessionId":null,"hCode":1982803459,"value":{"build":{"version":"2.52.0","revision":"4c2593c","time":"2016-02-11 19:06:42"},"os":{"name":"Mac OS X","arch":"x86_64","version":"10.12.6"},"java":{"version":"1.8.0_121"}},"class":"org.openqa.selenium.remote.Response","status":0}

But I know neither how to get that, nor what to do with it once gotten.

QualiT
  • 1,934
  • 2
  • 18
  • 37

2 Answers2

1

I do not have experience with Selenium or Commander so there might be a better solution, but this may be of help or point someone in the right direction.

For one, you should be able to make a cURL request, like: curl http://localhost:4444/wd/hub/status

It is possible to send messages back and forth between processes, as described here.

You could play around with this premise, like so:

From the Parent:

const child_process = require('child_process');

let options = {
    stdio: [process.stdin, process.stdout, process.stderr, 'pipe', 'pipe']
};

let child = child_process.spawn('command', ['parameters', 'here'], options);

child.stdio[3].write('A message to the new terminal');
child.stdio[4].pipe(process.stdout);

From the child process:

const fs = require('fs');
fs.createWriteStream(null, {fd: 4}).write('A message from new terminal.');

--
Also, this npm package seems like it might solve this problem for you:

https://www.npmjs.com/package/wd

Adam Patterson
  • 958
  • 7
  • 13
0

Thought I do like Adam Patterson's recommendation, I had already started down a particular path and wanted to see it through. I may try Here is how I solved the problem, for better or for worse:

if(myArgs.indexOf("LOCAL_SAF") != -1)
{
    startSelenium('open -a Terminal.app startSeleniumForSafari.sh');
    /* get the selenium webdriver executable for safari*/
    safari = require('selenium-webdriver/safari');
    browserUnderTest = new webdriver.Builder()
    .forBrowser('safari')
    .usingServer('http://localhost:4444/wd/hub')
    .build();
}

function startSelenium(terminalCommand){
    console.log('Starting selenium');

    var seleniumRunning = getSeleniumStatus();

    if (seleniumRunning != 'success' ) {     
        /* use Commander to open a shell script which opens a terminal and starts Selenium */
        exec(terminalCommand,
            function (error, stdout, stderr) {
            //  console.log('stdout: ' + stdout);
            //  console.log('stderr: ' + stderr);
                if (error !== null) {
                     console.log('exec error: ' + error);
                }
            });
    }   
    wait(2500); 
}
/* 
    THIS IS NOT MY CODE. 
    I needed a way, using javascript to just wait while Selenium starts up. This came from
    https://stackoverflow.com/questions/14226803/javascript-wait-5-seconds-before-executing-next-line
    was simple enough to understand, and suited my purposes.    
*/
function wait(ms){
    var start = new Date().getTime();
    var end = start;
    while(end < start + ms) {               
        end = new Date().getTime(); 
    }
}
/*
    THIS IS ALSO NOT MY CODE. I made some slight modifications to...
    https://stackoverflow.com/questions/5643321/how-to-make-remote-rest-call-inside-node-js-any-curl
*/

function getSeleniumStatus(){
     http.get('http://localhost:4444/wd/hub/status', function (res) {
         var json = '';
         res.on('data', function (chunk) {
             json += chunk;
         });
         res.on('end', function () {
             if (res.statusCode === 200) {
                 try {
                     var data = JSON.parse(json);
                     // data is available here:
                    // console.log(data.state);
                     if (data.state === 'success'){
                        console.log( data.state + "! Selenium is running.")
                     }
                     else {
                        console.log( data.state + " problem starting selenium.")
                     }
                     // return it
                     return data.state;
                 } catch (e) {
                     console.log('Error parsing JSON!');
                 }
             } else {
                 console.log('Status:', res.statusCode);
             }
         });
     }).on('error', function (err) {
           console.log('Error:', err);
     });
}

 /* 

 END https://stackoverflow.com/questions/14226803/javascript-wait-5-seconds-before-executing-next-line 
 - & - 
 https://stackoverflow.com/questions/5643321/how-to-make-remote-rest-call-inside-node-js-any-curl

 */
QualiT
  • 1,934
  • 2
  • 18
  • 37