^ Character in Node Script Arguments - javascript

I have a node script that is receiving the following argument: ^PPP.
I am calling the script as following: npm run scriptName ^PPP.
However inside the script if I do a console.log(process.argv), the output shows my argument as PPP.
I tried escaping the character as npm run scriptName \^PPP and npm run scriptName "^PPP" but to no avail.
Please help how can I receive the original string from the arguments.
PowerShell to run command and v10.16.2 node version

It depends on your terminal.
I made a simple script called args.js with
console.log(process.argv);
Using git bash on windows or WSL (Ubuntu), calling
$node args.js ^PPPP
Outputs
[ 'node/path',
'path/to/args.js',
'^PPPP' ]
Using windows cmd terminal, calling
>node args.js ^PPPP
Outputs
[ 'C:\Program Files\nodejs\node.exe',
'C:\workspace\tests\args.js',
'PPPP' ]
and calling
>node args.js ^^PPPP
Outputs
[ 'C:\Program Files\nodejs\node.exe',
'C:\workspace\tests\args.js',
'^PPPP' ]
So if you are using Windows's terminal, you need to double the ^ character (What does the single circumflex in the windows cmd shell mean: More?). On other terminals, it seems to work fine.
Edit: To add arguments to your node script from npm run, you need to separate them with -- like so:
>npm run scriptName -- ^^PPP

Related

Having trouble passing in --node-args with pm2

I make my scripts on windows then put them on my ubuntu server. This is my first time trying to pass args into the script on start up.
It works on windows I run
node file.js --variable=value
Then in my code I grab it with
const passedIn = require(argv.variable);
I cant figure out how to make this work with pm2.
I have been trying
pm2 start file.js --name myScript --node-args="value"
I didnt expect this to work. What do I need to call to access my passed in arg correctly from the code instead of argv.variable? and can I still do something like
--node-args="variable=value"
Try this:
pm2 start file.js --name myScript --node-args="--variable=value -port 12345"
Alternatively, add --, followed by your script args:
pm2 start file.js --name myScript -- --variable=value -port 12345
Details: https://futurestud.io/tutorials/pm2-how-to-start-your-app-with-node-js-v8-arguments
I was able to do it like this
pm2 start script.js --name myScript -- -v=value
and access it in code with
var argv = require('minimist')(process.argv.slice(2));
console.log(argv.v);
it only seemed to allow 1 letter variables.

How to run node.js application from script?

I am trying to run my node.js application from a script I have written:
echo "Starting node application"
sudo node /home/pi/PPBot/bot.js
exit 0
I run the script like this: sudo /etc/init.d/botscript
The output when running the script is:
Start node application
sudo: node: command not found
I have also tried replacing node by /home/pi/.nvm/versions/node/v.8.11.3/bin/node but this resulted in the same output.
I have already installed NodeJS through NVM. Simply using the command node bot.js works from the command line. However as can be seen above it does not work through the script.
if you want to run a simple node js file then use the command
-> node filename
ex.: node server.js
if you want to run a node js file with nodemon then use the command
-> nodemon filename
ex.: nodemon server.js

Cannot run my JMeter webdriver sampler script via command prompt

When I try to run my JMeter WebDriver sampler script via command prompt, the error below shows up. Is there any solution for this?
F:\apache-jmeter-3.2\bin\TestScriptRecorder.jmx is not a valid Win32 application.
It seems you are trying to execute .jmx file directly, it won't work this way, you need to launch jmeter.bat file and pass the .jmx file via -t command-line argument like:
F:\apache-jmeter-3.2\bin\jmeter.bat -n -t F:\apache-jmeter-3.2\bin\TestScriptRecorder.jmx -l result.jtl
or
java -jar F:\apache-jmeter-3.2\bin\ApacheJMeter.jar -n -t F:\apache-jmeter-3.2\bin\ -l result.jtl
References:
Non-GUI Mode (Command Line mode)
Full list of command-line options
How Do I Run JMeter in Non-GUI Mode?
If you want run your Jmx through command line follow the below steps.
Open command prompt
go to the path of where your jmeter.bat or jmete.sh file is placed(ex: C:\Users\ABC\apache-jmeter-3.3\bin)
Write the command as below
For Windows: jmeter -n -t C:\your_testScript_path\yourscript.jmx
For ubuntu/linux: ./jmeter.sh -n -t C:\your_testScript_path\yourscript.jmx
you can aslo pass any values to the jmx files using command line using command line paremeters like -JUsers=10 where in it should be defined in jmx like ${__P(Users,1)}

How to access chromedriver logs for Protractor test

I have seen that chromedriver can output a logfile (https://sites.google.com/a/chromium.org/chromedriver/logging)
This page shows how to set this up when executing the exe directly:
chromedriver.exe --verbose --log-path=chromedriver.log
I cannot figure out how to set this up in Protractor however
My current protractor.conf.js
require('babel/register');
exports.config = {
framework: 'jasmine2',
seleniumServerJar: './node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar'
};
From #alecxe's answer below and protractor's browser setup docs I tried adding the following (with and without --s) but with no apparent effect:
capabilities: {
browserName: "chrome",
chromeOptions: {
args: [
"--verbose",
"--log-path=chromedriver.log"
]
}
}
I also tried specifying an absolute path (log-path=/chromedriver.log) which also didn't work.
You can always start up your own instance of chromedriver in a separate process and tell Protractor to connect to that. For example, if you start chromedriver with:
chromedriver --port=9515 --verbose --log-path=chromedriver.log
Then you could use a configuration file for Protractor like so:
exports.config = {
seleniumAddress: 'http://localhost:9515',
capabilities: {
'browserName': 'chrome'
},
specs: ['example_spec.js'],
};
We use a shell script to add chromedriver logging, among other checks. You can then point protractor at the shell script:
protractor config:
// When running chromedriver, use this script:
chromeDriver: path.resolve(topdir, 'bin/protractor-chromedriver.sh'),
bin/protractor-chromedriver.sh
TMPDIR="/tmp"
NODE_MODULES="$(dirname $0)/../node_modules"
CHROMEDRIVER="${NODE_MODULES}/protractor/selenium/chromedriver"
LOG="${TMPDIR}/chromedriver.$$.log"
fatal() {
# Dump to stderr because that seems reasonable
echo >&2 "$0: ERROR: $*"
# Dump to a logfile because webdriver redirects stderr to /dev/null (?!)
echo >"${LOG}" "$0: ERROR: $*"
exit 11
}
[ ! -x "$CHROMEDRIVER" ] && fatal "Cannot find chromedriver: $CHROMEDRIVER"
exec "${CHROMEDRIVER}" --verbose --log-path="${LOG}" "$#"
According to the protractor's source code, chromedriver service is started without any arguments and there is no direct way to configure the arguments. Even though the chromedriver's Service Builder that protractor uses actually has an ability to specify the verbosity and the log path:
var service = new chrome.ServiceBuilder()
.loggingTo('/my/log/file.txt')
.enableVerboseLogging()
.build();
Old (incorrect) answer:
You need to set the chrome arguments:
capabilities: {
browserName: "chrome",
chromeOptions: {
args: [
"verbose",
"log-path=chromedriver.log"
]
}
},
See also:
Viewing outstanding requests
Since, the previous answer by #P.T. didn't work for me on Windows 7, I started with his suggestions and got it working on Windows. Here is a working solution for Windows 7 users.
STEP 1: Install BASH and JQ and confirm they are working on your Windows box
Download bash (for Windows 10
https://itsfoss.com/install-bash-on-windows/ ; for Windows 7
download latest here:
https://sourceforge.net/projects/win-bash/files/shell-complete/latest/ ; for Windows Server 2012 or any Windows OS that already has Git installed on it, you already have a bash.exe and sh.exe installed at C:\Program Files\Git\usr\bin or C:\Program Files (x86)\Git\usr\bin already
)
Install bash - For Windows 7/ download it and extract the zip files to a directory.
Download jq (https://stedolan.github.io/jq/) and install it in the same directory location as bash
Make SURE that you add your above directory (for Windows 7- where you extracted the bash zip files to; for other applicable OSes that have git, the path it is installed at) to your PATH system environment variable.
Once the above is installed and added to your PATH, close ALL and reopen Webstorm and any CMD windows you wish to run your work in.
Test that bash is actually installed by simply typing it on a windows command prompt
C:\git\> bash .
Doing so should produce a bash cmd prompt like this
bash$
STEP 2: Add Custom Files for Redirecting Chromedriver to user Debug Logging
Add the following files to the top level of the project (wherever your protractor-conf.js is located). These files allow us to add custom debug switches to the chromedriver.exe execution.
Note that this is necessary because these switches are not exposed through protractor and cannot be done directly in the protractor.conf.js file via the chromeOptions/args flags as you would normally expect
chromedriver.cmd -- exact source shown below:
bash protractor-chromedriver.sh %*
protractor-chromedriver.sh -- exact source shown below:
TMPDIR="$(dirname $0)/tmp"
NODE_MODULES="$(dirname $0)/node_modules"
SELENIUM="${NODE_MODULES}/protractor/node_modules/webdriver-manager/selenium"
UPDATECONFIG="${SELENIUM}/update-config.json"
EXEFILENAME="$(cat ${UPDATECONFIG} | jq .chrome.last | tr -d '""')"
CHROMEDRIVER="${SELENIUM}/${EXEFILENAME##*'\\'}"
LOG="${TMPDIR}/chromedriver.$$.log"
fatal() {
# Dump to stderr because that seems reasonable
echo >&2 "$0: ERROR: $*"
# Dump to a logfile because webdriver redirects stderr to /dev/null (?!)
echo >"${LOG}" "$0: ERROR: $*"
exit 11
}
[ ! -x "$CHROMEDRIVER" ] && fatal "Cannot find chromedriver: $CHROMEDRIVER"
exec "${CHROMEDRIVER}" --verbose --log-path="${LOG}" "$#"
/tmp -- create this directory at the top level of your project (same as the location of the protractor.conf.js file.
STEP 3: Update protractor.conf.js file.
In the protractor.conf.js file, add the following line as a property in the exports.config object. As in:
exports.config = {
.. ..
chromeDriver: 'chromedriver.cmd',
.. ..
STEP 4: Launch your tests
your test should now run and if the chrome driver outputs any log information it will appear in a file called chromedriver.???.log in the tmp directory under your project.
Important caveats
This script set up assumes you install and run protractor (and the chrome driver under it) within the local node_modules directory inside your project. That is how I run my code, because I want it complete self-contained and re-generated in the build process/cycle. If you have protractor/chromedriver installed globally you should change the CHROMEDRIVER variable within the protractor-chromedriver.sh file to match your installation of protractor/chrome driver.
hope that helps.
If you're using the seleniumServerJar, in protractor.conf.js set the logfile path to wherever you want it to write the file:
seleniumArgs: [
'-Dwebdriver.chrome.logfile=/home/myUsername/tmp/chromedriver.log',
]
If you're using webdriver-manager start to run a local selenium server, you'll need to edit the webdriver-manager file:
// insert this line
args.push('-Dwebdriver.chrome.logfile=/home/myUsername/tmp/chromedriver.log');
// this line already exists in webdriver-manager, add the push to args before this line
var seleniumProcess = spawnCommand('java', args);
In case you use webdriver-manager: webdriver manager has the chrome_logs option (you can find it in its source code (in opts.ts or opts.js in the compiled code)), so you can use it something like:
webdriver-manager start --chrome_logs /path/to/logfile.txt
I'm using this as a global afterEach hook (mocha):
afterEach(() => {
browser.manage().logs().get('browser').then(function(browserLog) {
if(browserLog && browserLog.length) {
console.log('\nlog: ' + util.inspect(browserLog) + '\n');
}
});
});

Where can I find the node.js file when node is installed?

I've tried to remove this message from node:
(node) warning: Recursive process.nextTick detected
because nothing else works. I've downloaded the source of node from the Ubuntu repository (I use the binary from npm but it should be almost the same, right?) and there is a node.js file containing this:
function maxTickWarn() {
// XXX Remove all this maxTickDepth stuff in 0.11
var msg = '(node) warning: Recursive process.nextTick detected. ' +
'This will break in the next version of node. ' +
'Please use setImmediate for recursive deferral.';
if (process.throwDeprecation)
throw new Error(msg);
else if (process.traceDeprecation)
console.trace(msg);
else
console.error(msg);
}
Where can I find this file when node is installed as a binary?
Node's .js files are compiled into the node binary, so if you want to change this, you will need to check out the git repo, modify the file containing maxTickWarn and then compile Node from source.
Have you tried running node with --no-deprecation?
Usage: node [options] [ -e script | script.js ] [arguments]
node debug script.js [arguments]
Options:
-v, --version print node's version
-e, --eval script evaluate script
-p, --print evaluate script and print result
-i, --interactive always enter the REPL even if stdin
does not appear to be a terminal
--no-deprecation silence deprecation warnings
--trace-deprecation show stack traces on deprecations
--v8-options print v8 command line options
--max-stack-size=val set max v8 stack size (bytes)

Categories