unit testing using phantomjs, selenium-webdriver,nodejs and chai-webdriver assertion lib - node.js

What I'm trying to do is to run small unit testing on my machine. I have all required node packages and phantomjs driver installed to my PATH.
var sw = require('selenium-webdriver');
var driver = new sw.Builder()
.withCapabilities(sw.Capabilities.phantomjs())
.build()
var chai = require('chai');
var chaiWebdriver = require('chai-webdriver');
chai.use(chaiWebdriver(driver));
driver.get("https://www.npmjs.org/package/chai-webdriver");
setTimeout(function(){
chai.expect('#content').dom.to.be.visible().then(function(){
console.log("content visible");
});
driver.quit();
},3000);
Running the above code,
node fileName.js
Gives me result like this.
/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/promise.js:1643
throw error;
^
Error: Selector #content matches nothing
at /Users/r558268/Documents/sandbox/node_modules/chai-webdriver/node_modules/webdriver-sizzle/lib/webdriver_sizzle.js:17:13
at /Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/goog/base.js:1243:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/promise.js:1539:20)
at notify (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/promise.js:362:12)
at notifyAll (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/promise.js:331:7)
at resolve (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/promise.js:309:7)
at reject (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/promise.js:439:5)
at /Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/goog/base.js:1243:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/promise.js:1539:20)
at notify (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/promise.js:362:12)
==== async task ====
WebDriver.call(function)
at webdriver.WebDriver.call (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:517:15)
at webdriver.WebDriver.findElementInternal_ (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:728:15)
at webdriver.WebDriver.findElement (/Users/r558268/Documents/sandbox/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:706:17)
at one (/Users/r558268/Documents/sandbox/node_modules/chai-webdriver/node_modules/webdriver-sizzle/lib/webdriver_sizzle.js:15:22)
at /Users/r558268/Documents/sandbox/node_modules/chai-webdriver/lib/index.js:58:18
at Assertion.<anonymous> (/Users/r558268/Documents/sandbox/node_modules/chai-webdriver/lib/index.js:72:16)
at Assertion.ctx.(anonymous function) [as visible] (/Users/r558268/Documents/sandbox/node_modules/chai/lib/chai/utils/addMethod.js:40:25)
at null._onTimeout (/Users/r558268/Documents/sandbox/assertionExample.js:30:43)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Now here is the thing.. if I save as the url npmjs.org/chai-webdriver onto my machine and run the same test, then it passes without any errors.
I'm not able to figure out why.. can anyone please help me out on this.. MUCH APPRECIATED! Thank you.

Related

Impossible to install spookyjs

I want to install spookyjs and find it impossible to do so. I've tried three different ways:
Run the standard spookyjs package.json provided in spookyjs github.
Then I try to run hello.js and I am greeted with this error.
C:\Users\User1\Desktop\test2>node hello.js
events.js:183
throw er; // Unhandled 'error' event
^
Error: spawn casperjs ENOENT
at _errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at onErrorNT (internal/child_process.js:372:16)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Installed phantomjs and casperjs globally and package.json installed spooky and tiny-jsonrpc. I get the same error message.
Installed these dependencies from package.json
"dependencies": {
"spooky": "^0.2.5",
"tiny-jsonrpc": "^2.0.1",
"phantom": "^4.0.12",
"casperjs": "^1.1.4"
I get the same error.
I came across this link:
Issue
and it detailed the solution. That is this chunk of code:
const
path = require('path'),
_ = require('underscore')
;
var
// close env to pass to spawn
env = _.clone(process.env),
// get the PATH - in my local Windows, it was Path
envPath = env.PATH || env.Path,
// get path to node_modules folder where casperjs and
// phantomjs-prebuilt are installed
// this will be different for you
binDir = path.join(__dirname, './../../node_modules/.bin'),
Spooky = require('spooky')
;
// update the path in the cloned env
env.PATH = env.Path = `${envPath};${binDir}`;
var spooky = new Spooky({
child: {
// spooky is trying to call casperjs.bat for windows
// .bat doesn't work, so call the update .cmd file
// this fixes issue 2 with the file
command: /^win/.test(process.platform) ? 'casperjs.cmd' : 'casperjs',
transport: 'http' ,
spawnOptions: {
// set the env using cloned version
// this fixes issue 1 with the path
env: env
}
},
...
So now the program runs without an error. Actually though it runs without anything, because while on the one hand no error pops up, on the other nothing pops up. I debbuged by putting console.log() inside spooky's callback function but nothing is displayed. That is for another question though...
But the error i was receiving has vanished and the interpreter runs the code.

Cannot find module '../dialog' (Electron fatal error)

In electron, I encounter the following error:
module.js:440
throw err;
^
Error: Cannot find module '../dialog'
at Module._resolveFilename (module.js:438:15)
at Function.Module._resolveFilename (/opt/App/resources/electron.asar/common/reset-search-paths.js:47:12)
at Function.Module._load (module.js:386:25)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object.get [as dialog] (/opt/App/resources/electron.asar/browser/api/exports/electron.js:35:14)
at process.<anonymous> (/opt/App/resources/electron.asar/browser/init.js:64:31)
at emitOne (events.js:96:13)
at process.emit (events.js:188:7)
at process._fatalException (node.js:276:26)
It happens on a child process spawn that fails in Linux. Strange because I do have a try catch block around that, yet it still results in an uncaughtexception, as seen in the code in browser/init.js from electron.asar:
// Don't quit on fatal error.
process.on('uncaughtException', function (error) {
// Do nothing if the user has a custom uncaught exception handler.
var dialog, message, ref, stack
if (process.listeners('uncaughtException').length > 1) {
return
}
// Show error in GUI.
dialog = require('electron').dialog
stack = (ref = error.stack) != null ? ref : error.name + ': ' + error.message
message = 'Uncaught Exception:\n' + stack
dialog.showErrorBox('A JavaScript error occurred in the main process', message)
}
As said, my code is in a try catch:
try {
server = childProcess.spawn(java, ["-jar", "App.jar"], {
"cwd": serverDirectory,
"detached": true
}, function(err) {
console.log("in callback");
});
} catch (err) {
console.log("here we are");
console.log(err);
}
But neither the callback nor the catch block is reached. Any ideas what is going on here and why the default dialog module cannot be found?
I found same error with electron 1.6.2
Figured it was due, when closing the application an error occur and electron want to display it in a dialog, maybe the close process has started and electron can't load this module, anyway I add:
const { dialog } = require('electron');
in main.js, no more error in console instead a dialog the error, I can fix it, After that I let the require just in case.
I hope I understand your question correctly...
If by "default dialog module", you mean the electron dialog API, then you can require that like so: const { dialog } = require('electron'). (Or in pre-1.0 versions simply require('dialog'). See https://github.com/electron/electron/blob/master/docs/api/dialog.md
Also the try/catch needs to be around the require in the child process. The try/catch you have is around the spawning of the child process in the parent. That require is failing in an entirely different node.js process, so it's not caught in the parent process that spawned it. It sounds like your child process code might work better if it looked like:
try {
const dialog = require('dialog');
} catch(e) {}
Also, if childProcess.spawn is referring to the core node module child_process, that doesn't accept a callback function. See https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
Can you share the code from your child process? That might help more.

browserify watchify produces errors

I am using gulp, browserify and watchify. Here is my 'scripts' task:
gulp.task('scripts', function() {
var b = watchify(browserify(paths.app.root + '/client.js', watchify.args));
b.on('update', bundle);
return bundle();
function bundle() {
return b
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest(paths.build.root + '/scripts'));
}
});
client.js looks like:
require('request');
alert('1');
where request is node module (http client): https://github.com/request/request
When I run this task and execute the code - everything is fine. The problem starts when I change client.js. When I comment alert('1'), watchify start it's work and bundle() runs again. When I reload the page on the browser, I get the following javascript exception:
Uncaught TypeError: Cannot read property 'version' of undefined
with the following stack trace:
/Users/me/project/node_modules/browserify/node_modules/crypto-browserify/node_modules/browserify-sign/node_modules/elliptic/lib/elliptic.js.../package.jsonbundle.js:19825
/Users/me/project/node_modules/browserify/node_modules/crypto-browserify/node_modules/create-ecdh/node_modules/elliptic/lib/elliptic.js.../package.jsonbundle.js:1
/Users/me/project/node_modules/browserify/node_modules/crypto-browserify/node_modules/create-ecdh/ecdh.js.bn.js
/Users/me/project/node_modules/browserify/node_modules/crypto-browserify/node_modules/create-ecdh/inject.js../ecdh
/Users/me/project/node_modules/browserify/node_modules/crypto-browserify/index.js.browserify-aes
/Users/me/project/node_modules/request/lib/helpers.js._process
/Users/me/project/node_modules/request/index.js../lib/cookies
./client.js.request
Any idea what is wrong with browserify / request?
It looks like it is an issue with elliptic, a dependency of browserify. See https://github.com/indutny/elliptic/issues/30
Hopefully this gets fixed up shortly.
Edit: It may actually be an issue with browserify. I would try reverting to 8.1.2 and seeing if that helps.

mongodb with nodejs

this is th code I am using inserting a document to mongodb.
var client = new Db('test', new Server("127.0.0.1", 27017, {}), {w: 1}),
test = function (err, collection) {
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
test.assertEquals(1, count);
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
test.assertEquals(1, results.length);
test.assertTrue(results[0].a === 2);
// Let's close the db
client.close();
});
});
};
client.open(function(err, p_client) {
client.collection('test_insert', test);
});
but while running I am getting error
xports, require, module, __filename, __dirname) { var client = new Db('test',
^
ReferenceError: Db is not defined
at Object. (C:\Users\Basic node\cheerio\mongonode.js:1:81
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
can you suggest me how to solve this problem
thanks in advance
Please import all the required modules, which you are using. Db is not defined points out that Db is defined in some other module or you have forgot to declare it.
You'll notice this exact code block posted in a number of different stackoverflow questions. The problem here is that this is a copy and pasted code block from mongodb's documentation, as is in fact the first example of a mongodb nodejs program.
https://npmjs.org/package/mongodb
You'll find this under "Introduction" as "A simple example of inserting a document."
It's clearly an incomplete example, but a lot of people are just trying it out to see if they've got everything installed correctly and immediately run into a wall.
Most people will have installed the mongodb driver, but will be missing something at the top like this:
var mongodb = require('mongodb');
var Db = mongodb.Db;
var Server = mongodb.Server;
I also fell into the copy-paste trap here and ran into another issue with the "assertEquals" method not existing. I've seen other people reference that function in other places on the web, but not really sure how it works.
In any case, to make it work for me, I required the assert module:
var assert = require('assert');
And then I replaced the assertEquals lines with something like this:
assert.equal(1, count, "Unexpected result");
Note that you're going to run into an issue if you've run this a couple of times; it's going to count the number of things in that table, and there is going to be more than one.
You'll have to figure out how to get into mongo's CLI and remove them to get it to run successfully.
Try to install mongodb native driver
npm install mongodb

Node.js callback not working

I have the following two Node.js files:
//test.js
var hash=require('./hash');
var sys=require('sys');
hash.hash("asdf",function(param){
sys.puts(param);
});
and
//hash.js:
var exec=require('child_process').exec;
var sys=require('sys');
exports.hash=function(data,callback){
exec("./a.out "+data,function callback(error,stdout,stderr){
callback(stdout);
});
}
As you can see, I'm trying to make stdout available in test.js
Trouble is, when I run node test.js, I get the following error:
eamorr#Compaq6000:~/Desktop/simple-hash$ node test.js
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
RangeError: Maximum call stack size exceeded
Anyone got any ideas as to what I might be doing wrong?
Many thanks in advance,
Edit: solution: rename the inner "callback" to "callback2" or something else... Thanks to Idan K for pointing this out.
When you call exec you are naming what should be an anonymous function, callback. This has the same name as the second argument to hash, hence the recursion.

Resources