Running 'casperjs test' in phantom - node.js

So I have a file running in node which runs a local copy of PhantomJS as below shows:
phantom.casperPath = 'node_modules/casperjs';
phantom.injectJs('node_modules/casperjs/bin/bootstrap.js');
var casper = require('casper').create({
viewportSize: config.viewportSize
});
casper.test.begin('Runing tests here', 5, function suite(test) {
// Tests here
});
Without the casper.test.begin() my tests function fine. I have the correct version 1.1.0 which can use this test suite but I get the following error in my console:
CasperError: casper.test property is only available using the `casperjs test` command
The CasperJS docs mentions this too: http://docs.casperjs.org/en/latest/testing.html. My question is how do I run this Casper under this command in the above code so I can use these tests?
Thanks!

CasperError: casper.test property is only available using the casperjs test command
problem solved.
You have to include this line at the top of your script in your xyz.js, so that the .test property becomes true;
phantom.casperTest = true;
Then you should have no problem Launching from the terminal:
casperjs xyz.js

you can also call casperjs test xyz.js
For more info, check the doco here : http://docs.casperjs.org/en/latest/testing.html

Related

How to get wdio running through IntelliJ run/debug configurations?

I can run the tests from the command line using
> ./node_modules/.bin/wdio wdio.conf.js
But if I try to run this from IntelliJ's run/debug configuration I get various different errors.
Featurefile or directory: /path_to_my_feature_file/myfeature.feature
Cucumber.js arguments: wdio.conf.js
Executable path: /path_to_my_project/node_modules/.bin/wdio
gives me
more than one config file specified
If I remove the Cucumber Arguments, it just runs indefinitely. If I stop it running I get the error
Failed loading configuration file
It looks like there's some kind of issue with loading the config file, but I don't know how to fix it. Any suggestions? wdio.conf.js exists and is in the project root.
WebStorm doesn't provide any special support for wdio test runner. But you can still run/debug the tests using Node.js run configuration like the following:
But this doesn't work out of the box due to problems related to using non-tty environment (Node.js run console in IDEA is non-tty). As a workaround, please try commenting out if (process.stdin.isTTY) and else branch in node_modules\webdriverio\build\lib\cli.js:
//if (process.stdin.isTTY) {
launch();
/*
} else {
var stdinData = '';
/!*
* get a list of spec files to run from stdin, overriding any other
* configuration suite or specs.
*!/
var stdin = process.openStdin();
stdin.setEncoding('utf8');
stdin.on('data', function (data) {
stdinData += data;
});
stdin.on('end', function () {
if (stdinData.length > 0) {
args['specs'] = stdinData.trim().split(/\r?\n/);
}
launch();
});
}*/
see WEB-31745
To get this to work with webdriverio v5 and higher you need to set in the debug configurations screen above
the path to the runner as the javascript file to execute:
node_modules#wdio\cli\bin\wdio.js
and the conf file as the application parameters:
wdio.conf.js

WebStorm debugging not stopping on breakpoints

My app is a node.js app which I can run through command line using: npm test inside the working directory. In WebStorm, I created a new configuration that looks like this:
Node interpreter: /usr/local/bin/npm
Node parameters: test
Working directory ~/dev/project
When I hit the run button, I get the correct output:
/usr/loca/bin/npm test
> app#1.0.0 test ~/dev/project
something else
Process finished with exit code 0
But since I have breakpoints set, it should have stopped on a breakpoint instead of getting to the Process finished part. I set my code to be pretty simple just so I can test breakpoints, so it looks like this:
"use strict";
let foo = false; (breakpoint)
let bar = true;
if (foo === bar) { (breakpoint)
console.log('something');
} else {
console.log('something else'); (breakpoint)
}
process.exit(1);
I also tried to make this work through command line. In my site settings, I set my Built-in server to port 12345. Can accept external connections, and allow unsigned requests.
when I run it through command line, I use:
npm --debug-brk=12345 test
I get the same result: it runs all the way to the exit point without stopping on the breakpoints.
Any ideas what I need to do to get this to work?
/usr/local/bin/npm is definitely not a Node interpreter, it's NPM package manager that is a Node.js application itself (i.e. it's run with Node.js interpreter). And test is a name of npm script, not a Node.js parameter.
If you like to debug your .js file that is run via npm test, you need to modify your npm script to include the debug options and then use NPM run configuration for debugging. Or, just create a Node.js configuration, set a valid path to Node.js execuitable there (/usr/bin/node or whatever it looks like on your system), then specify your .js file as JavaScript file:. See https://blog.jetbrains.com/webstorm/2017/09/debugging-node-js-apps-in-webstorm for more info

Run several Appium Tests after another with nodejs

I'm trying to build a testsuite in which are eg. 5 testfiles named Test1.js - Test5.js.
I want to execute the testsuite which then runs Test1.js.
After Test1.js is complete it should run Test2.js.
I tried to do it with require()
.fin(function() {
console.log("Test succeeded")
require('./Test2.js')
return browser.quit();
But if the require is before the browser.quit() I can't execute the second test because the Appium server is still up and running, and after browser.quit() the require() doesn't get called at all.
I am fairly new to Appium, nodejs and Javascript in general so maybe I have overlooked something.
Figured it out myself. The problem wasn't the calling of the function but the server arguments.
I used "sessionOverride":false instead of "sessionOverride":true.

phantomjs cannot find module webpage in node's child process

I'm trying to run phantomjs in a node child process. Phantomjs complains it can not find module 'webpage'. Running the script from the cli works fine:
phantomjs script.js
I stripped the problem down to these two files:
main.js:
var script = require('./script');
var cp = require('child_process');
cp.exec('/usr/bin/phantomjs script.js');
script.js
var page = require('webpage').create();
page.open("http://www.google.com", function(status) {
console.log("opened google? ", status);
var title = page.evaluate(function(s) {
return s;
}, 'Hello');
console.log(title);
phantom.exit();
});
Running the following command fails:
node main.js
with error:
module.js:340
throw err;
^
Error: Cannot find module 'webpage'
I've tried to specify the working directory in cp.exec as an option without effect. Is there a way to set the node search path for modules in a global context so it can be found in a new node process? Or am I doing something else wrongly?
PhantomJS is not a node.js module and cannot be directly used from node. I think you understand this, because you try to invoke it via child_process.
The problem is the line var script = require('./script');. As I understand, it is the phantomjs script. Since node.js doesn't have a webpage module, but phantomjs does the require fails and everything with it. Simply remove the line. It doesn't seem like you use it anyway.

Using require with casperjs returns "Can't find module" error

I'm following the documentation on writing a module with casperjs, but I'm getting an error.
The code, straight from the docs:
// my module, stored in universe.js
// patching phantomjs' require()
var require = patchRequire(require);
// now you're ready to go
var utils = require('utils');
var magic = 42;
exports.answer = function() {
return utils.format("it's %d", magic);
};
And the calling script:
var universe = require("./universe");
var casper = require("casper").create();
console.log(universe.answer());
casper.test.begin("Home Page", 1, function suite(test) {
});
But I get the following error:
casperjs test testStage.js
Test file: testStage.js
CasperError: Can't find module ./universe
/usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214 in patchedRequire
/Users/smosk/Google Drive/source/nest/testStage.js:1
FAIL CasperError: Can't find module ./universe
# type: error
# file: testStage.js
# subject: false
# error: "CasperError: Can't find module ./universe"
# stack: in patchedRequire() in /usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214
in anonymous() in testStage.js:1
FAIL 1 test executed in 0.031s, 0 passed, 1 failed, 0 dubious, 0 skipped.
Details for the 1 failed test:
In testStage.js
Untitled suite in testStage.js
error: CasperError: Can't find module ./universe
Also tried defining casper first:
var casper = require("casper").create();
var universe = require("./universe");
But that just resulted in
Test file: testStage.js
CasperError: Can't find module ./universe
/usr/local/Cellar/casperjs/1/libexec/bin/bootstrap.js:214 in patchedRequire
/Users/smosk/Google Drive/source/nest/testStage.js:2
You should try to upgrade to the lastest master build of CasperJS as this pull request that I made should fix the problem that you're having.
However, if you don't want to upgrade, you could also try running CasperJS with the following:
casperjs test ./testStage.js
for others who might be an idiot like me, you may want to make sure you are using casperjs in your cmd and not phantomjs
Had a similar problem. Mine seems to have been caused by the merge of https://github.com/n1k0/casperjs/issues/587 . Going back to 1d1e83ba3c9241623 solved my problems
I just spent the last 2 hours trying to debug this issue:
C:\casperjs>casperjs
CasperError: Can't find module cli
C:\casperjs\bin\bootstrap.js:262 in patchedRequire
C:\casperjs\bin\bootstrap.js:375
C:\casperjs\bin\bootstrap.js:395
I was using PhantomJS 1.9.0 but I found I had to upgrade to 1.9.2 and it started working immediately. I hope this is helpful to someone else.

Resources