3

I have created files as per following:

Demo.js

var webdriver = require('selenium-webdriver');     

var driver = new webdriver
    .Builder()
    .withCapabilities(webdriver.Capabilities.chrome())
    .build();

driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('simple programmer');
driver.findElement(webdriver.By.name('btnK')).click();
driver.quit();

Run.js

var express = require('express');

var app = express(); 
app.get('/', function (req, res) {
    res.send('<button type="button" onclick="test();">Run Script</button>');
});

var server = app.listen(8081, function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log("Example app listening at http://%s:%s", host, port);
});

function test() {
    var fork = require('child_process').fork;
    var child = fork('./Demo');
}

What I want to do is :

  1. When I run file Run.js, it has button called "Run script".
  2. As soon as I click on that button, It should run automation script which is stored in Demo.js.

Currently for me above test function is not working and unable to run Demo.js on click of button.

Error

Uncaught ReferenceError: test is not defined
    at HTMLButtonElement.onclick

1 Answer 1

5
+50

You are calling the function test on the client side, but the function is defined on the server side.

You could create a form on the client side which would send a post request to the server upon click. Then handle the POST request on the server side to execute the desired code:

app.get('/', function (req, res) {
   res.send(
    '<form action="/run" method="POST">' +
    '  <input type="submit" name="run" value="Run Script" />' +
    '</form>');
});

app.post('/run', function (req, res) {
  var fork = require('child_process').fork;
  var child = fork('./Demo');
  res.send('done');
});

You could also send the POST request with an Ajax request on the client side:
Simple button click example with Ajax and Node.js?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.