如何从其他js文件执行selenium node js脚本

时间:2017-10-13 12:18:30

标签: javascript node.js selenium selenium-webdriver

我按照以下方式创建了文件:

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');
}

我想做的是:

  1. 当我运行文件Run.js时,它有一个名为“运行脚本”的按钮。
  2. 一旦我点击该按钮,它就应该运行存储在Demo.js中的自动化脚本。
  3. 目前对我而言,测试功能无法正常工作,无法在点击按钮时运行Demo.js

    错误

    Uncaught ReferenceError: test is not defined
        at HTMLButtonElement.onclick
    

1 个答案:

答案 0 :(得分:5)

您正在客户端调用函数char* barfoo = "variable_test"; //barfoo is a string storing 'variable_test' const char* my_pointer_to_const_char = barfoo; // This is type 2, constant pointer. Thus, my_pointer_to_const_char cannot //point to any other variable and will always store the address of barfoo barfoo = "changed!"; //this is perfectly valid as this statement will alter the value of string //barfoo. my_pointer_to_const_char will still store the address of barfoo. ,但该函数是在服务器端定义的。

您可以在客户端创建一个表单,该表单会在单击时向服务器发送一个发布请求。然后处理服务器端的test请求以执行所需的代码:

POST

您还可以在客户端发送带有Ajax请求的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'); }); 请求:
Simple button click example with Ajax and Node.js?