使用来自终端的输出的writeFile函数

时间:2018-12-15 17:33:50

标签: javascript node.js selenium-webdriver writefile

运行时,以下代码在独立项目中运行。但是,如果我想用终端中的输出而不是代码中已经存在的数组替换变量“ sample_text”,我将如何修改代码?例如,在另一个文件中运行节点时,说输出看起来像“学生:丹尼,年龄:28,体重:148”。

 // writefile.js

 const fs = require('fs');

 let sample_text = 'student: danny, age: 28, weight: 148'

 // write to a new file named sample.json

 fs.writeFile('sample.json', sample_text, (err) => {  
                if (err) throw err;
            console.log('Text saved!');
            });

2 个答案:

答案 0 :(得分:0)

类似这样的东西:

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('Enter data', (data) => {

  fs.writeFile('2pac.txt', data, (err) => {  
            if (err) throw err;
        console.log('Text saved!');
        });

  rl.close();
});

答案 1 :(得分:0)

您需要做两件事:

  1. 将Selenium Webdriver的输出插入节点程序
  2. 在process.stdin上监听“数据”事件以捕获硒的输出

您的节点程序应如下所示:

null

然后您应该通过从终端运行以下命令来启动selenium和该节点程序: private static string CreateSpecialFormattedDateXml(DateTime? theDate) { StringBuilder sb = new StringBuilder(); if (theDate != null) { sb.Append(CreateFieldNodeXml("year", theDate.Year.ToString())); sb.Append(CreateFieldNodeXml("month", theDate.Month.ToString())); sb.Append(CreateFieldNodeXml("day", theDate.Day.ToString())); } return sb.ToString(); } private static string CreateFieldNodeXml(string fieldname, string fieldValue) { if (!string.IsNullOrEmpty(fieldValue)) { return "<" + fieldname + ">" + fieldValue + "</" + fieldname + ">"; } return null; }