CasperJS 1.1.0-beta2 test example resulting in UncaughtError - javascript

I would love to use CasperJS to run tests against the PhantomJS headless browser. There is great documentation, but this example I would like to follow does not work. Has anyone been able to get this to work, or can someone find a problem with my implementation? I am referencing this "Advanced Technique" in the CasperJS Documentation:
//cow-test-ref.js
function Cow() {
this.mowed = false;
this.moo = function moo() {
this.mowed = true; // mootable state: don't do that at home
return 'moo!';
};
}
casper.test.begin('Cow can moo', 2, {
setUp: function(test) {
this.cow = new Cow();
},
tearDown: function(test) {
this.cow.destroy();
},
test: function(test) {
test.assertEquals(this.cow.moo(), 'moo!');
test.assert(this.cow.mowed);
test.done();
}
});
My Result is the following error:
m#mPro marketing_qa $ casperjs --version
1.1.0-beta2
m#mPro tests $ casperjs test cow-test-ref.js
Test file: cow-test-ref.js
# Cow can moo
PASS Subject equals the expected value
PASS Subject is strictly true
FAIL TypeError: 'undefined' is not a function (evaluating 'this.cow.destroy()')
# type: uncaughtError
# file: cow-test-ref.js:14
# error: 'undefined' is not a function (evaluating 'this.cow.destroy()')
# TypeError: 'undefined' is not a function (evaluating 'this.cow.destroy()')
# at cow-test-ref.js:14
# at done (/usr/local/Cellar/casperjs/1.1-beta2/libexec/modules/tester.js:1067)
# at cow-test-ref.js:20
# at /usr/local/Cellar/casperjs/1.1-beta2/libexec/modules/tester.js:986
# at /usr/local/Cellar/casperjs/1.1-beta2/libexec/bin/bootstrap.js:51
# at begin (/usr/local/Cellar/casperjs/1.1-beta2/libexec/modules/tester.js:1015)
# at cow-test-ref.js:21
# stack: not provided
FAIL 3 tests executed in 0.023s, 2 passed, 1 failed, 0 dubious, 0 skipped.
Has anyone encountered this, or worked around this successfully? I'd love to use this test pattern.

The destroy method is used as an example to highlight the concept of the principle behind tearDown. The method is not implemented in the original code, so the error you get is normal.

Related

Why does rake jasmine pass but rake jasmine:ci fails with TypeError: undefined is not a function?

With rake jasmine all my tests pass in the browser.
With rake jasmine, 2 specs fail with:
TypeError: undefined is not a function (evaluating 'expect(player).not.toBePlaying(song)') in http://localhost:36091/__spec__/PlayerSpec.js (line 28)
I've configured my spec/javascripts/support/jasmine.yml file so that it has
src_files:
- src/Player.js
- src/Song.js
spec_files:
- '**/*[sS]pec.js'
src_dir:
spec_dir: spec
src/Song.js has:
function Song() {
...
Why is rake jasmine:ci failing for those two examples?
The code for the first failing one is:
it("should be able to play a Song", function() {
player.play(song);
expect(player.currentlyPlayingSong).toEqual(song);
//demonstrates use of custom matcher
expect(player).toBePlaying(song); # <-- error here
});
Song.js does seem to be loaded because if I remove it all 5 examples fail.
I needed the custom matcher toBePlaying
I was able to get all the specs passing by changing this in the spec/javascripts/support/jasmine.yml file:
src_files:
- src/Player.js
- src/Song.js
- spec/SpecHelper.js # <--- Added to include the custom helper 'toBePlaying'
I had tried adding it to the spec_files section but it needs to be listed in src_files

NativeImage not working in setOverlayIcon() in Electron

I'm trying to make a numeric badge for my app's taskbar icon (Windows 10). I've used this code as a starting point and modified it a bit. After creating the badge, I've used the following to set it in the renderer process:
var electron=require('electron'),
remote=electron.remote,
nativeImage=electron.nativeImage;
...
var win=remote.getCurrentWindow();
...
var badgeDataURL=canvas.toDataURL();
var img=nativeImage.createFromDataURL(badgeDataURL);
win.setOverlayIcon(img,''+n);
Running this gives me the following error:
Uncaught Error: Could not call remote function 'setOverlayIcon'. Check that the function signature is correct. Underlying error: Error processing argument at index 0, conversion failure from #<Object>
Error: Could not call remote function 'setOverlayIcon'. Check that the function signature is correct. Underlying error: Error processing argument at index 0, conversion failure from #<Object>
at callFunction (A:\electron\resources\electron.asar\browser\rpc-server.js:235:11)
at EventEmitter.<anonymous> (A:\electron\resources\electron.asar\browser\rpc-server.js:342:5)
at emitMany (events.js:127:13)
at EventEmitter.emit (events.js:201:7)
at WebContents.<anonymous> (A:\electron\resources\electron.asar\browser\api\web-contents.js:231:13)
at emitTwo (events.js:106:13)
at WebContents.emit (events.js:191:7)metaToValue # A:\electron\resources\electron.asar\renderer\api\remote.js:217remoteMemberFunction # A:\electron\resources\electron.asar\renderer\api\remote.js:113electronSetBadge # app.js:81updateBadge # app.js:156initClick # app.js:183(anonymous function) # app.js:203dispatch # jquery-1.12.4.min.js:3r.handle # jquery-1.12.4.min.js:3
I've tried the following:
different versions of Electron (1.4.13 and 1.2.8)
testing the contents of badgeDataURL and img and it's a valid image
testing setOverlayIcon with a static image: win.setOverlayIcon(__dirname+'/pics/badge.png',''+n) (and it works)
win.setOverlayIcon(null,'') also works
Although the documentation says that setOverlayIcon expects the first parameter to be of the NativeImage type I haven't been able to find a working example anywhere. Any ideas?
For me, setOverlayIcon needed to be run from the main process. Here is what fixed it on my side:
In my renderer process:
ipcRenderer.send('update-badge', canvas.toDataURL())
And in my main process:
ipcMain.on('update-badge', (event, data) => {
let img = nativeImage.createFromDataURL(data)
win.setOverlayIcon(img, 'description')
}

How to use the aurelia-dialog plugin with Aurelia?

I'm having trouble setting up the aurelia-dialog plugin (0.2.0) with my test Aurelia app.
Unfortunately, the README.MD file that details how to accomplish this has some serious holes. First, it doesn't mention having to inject the aureliaDialog into your class, so I tried this first:
#inject(HttpClient, DialogService)
export class MyClass{
constructor(http, dialogService) {
this.http = http;
this.dialogService = dialogService;
}
...
}
I tried to invoke the dialog box using:
this.dialogService.open({ viewModel: Prompt, model: 'Good or Bad?' })
The above results in the following errors:
Unhandled promise rejection ReferenceError: info is not defined
at Container.invoke (http://127.0.0.1:9000/jspm_packages/github/aurelia/dependency-injection#0.10.0/aurelia-dependency-injection.js:401:30)
at Array.<anonymous> (http://127.0.0.1:9000/jspm_packages/github/aurelia/dependency-injection#0.10.0/aurelia-dependency-injection.js:272:44)
at Container.get (http://127.0.0.1:9000/jspm_packages/github/aurelia/dependency-injection#0.10.0/aurelia-dependency-injection.js:329:24)
at http://127.0.0.1:9000/jspm_packages/github/aurelia/templating#0.15.1/aurelia-templating.js:3685:75
at run (http://127.0.0.1:9000/jspm_packages/npm/core-js#0.9.18/modules/es6.promise.js:91:43)
at http://127.0.0.1:9000/jspm_packages/npm/core-js#0.9.18/modules/es6.promise.js:105:11
at module.exports (http://127.0.0.1:9000/jspm_packages/npm/core-js#0.9.18/modules/$.invoke.js:6:25)
at queue.(anonymous function) (http://127.0.0.1:9000/jspm_packages/npm/core-js#0.9.18/modules/$.task.js:40:9)
at Number.run (http://127.0.0.1:9000/jspm_packages/npm/core-js#0.9.18/modules/$.task.js:27:7)
at listner (http://127.0.0.1:9000/jspm_packages/npm/core-js#0.9.18/modules/$.task.js:31:9) Unhandled promise rejection ReferenceError: info is not defined(…)(anonymous function) # es6.promise.js:139module.exports # $.invoke.js:6queue.(anonymous function) # $.task.js:40run # $.task.js:27listner # $.task.js:31
Next, I tried to add the configuration of the plugin to my main.js file:
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin('aurelia-animator-css')
.plugin('aurelia-dialog'); // <----- this is what I added
aurelia.start().then(a => a.setRoot());
}
Now my app doesn't start at all and produces the following error while trying (and failing) to start:
DEBUG [aurelia] Configured plugin aurelia-dialog. aurelia-logging-console.js:38
DEBUG [templating] importing resources for undefined [] es6.promise.js:139
Unhandled promise rejection Error: Cannot read property 'querySelectorAll' of undefined(…)
(anonymous function) # es6.promise.js:139module.exports # $.invoke.js:6queue.
(anonymous function) # $.task.js:40run # $.task.js:27listner # $.task.js:31
I am now at a loss of what to try next. Thanks for any insight you can offer with this issue.
I'm also hopeful that the maintainer of the aurelia-dialog plugin revises the docs to make the setup process less painful.
Thanks,
Greg
In the end, the problem turned out to be a bug in aurelia-dialog 0.2.0. Version 0.2.1 fixes the issue I couldn't resolve on my own: https://github.com/aurelia/dialog/pull/24.
The other steps that I had to follow are still necessary - you will need to inject the DialogService class and modify your main.js file to add the configuration.

Protractor tests in CoffeeScript producing "SyntaxError: unexpected by"?

I am trying to write the following Protractor test in CoffeeScript:
describe "tests", ->
browser.get "/"
it "should display Login page", ->
expect(element(by.css("h1")).getText()).toBe "Login"
However, CoffeeScript spits out this error:
SyntaxError: unexpected by
Solutions?
Like #meagar said it is reserved, you can alias it in your protractor config in the onPrepare block:
require('coffee-script/register');
exports.config = {
....
// by is reserved in coffee script
onPrepare: function() {
global.By = global.by;
}
}
then
expect(element(By.css("h1")).getText()).toBe "Login"
by is a reserved word in CoffeeScript, used in specifying loop increments:
evens = (x for x in [0..10] by 2)
Use a different variable name.

Importing other .js files in Buster.js tests

I'm making my first attempt at Javascript testing, with Buster.js
I've followed the instructions at the Buster site to run "states the obvious" test. However, I haven't been able to import any of my existing .js files into the tests.
For instance, I have a file js/testLibrary.js, containing:
function addTwo(inp) {
return inp+2;
}
and a file test/first-test.js, containing:
// Node.js tests
var buster = require("buster");
var testLibrary = require("../js/testLibrary.js");
var assert = buster.referee.assert;
buster.testCase("A module", {
"Test The Library": function() {
result = addTwo(3);
console.log(result);
assert(true, 'a message for you');
}
});
Running buster-test gives:
Error: A module Test The Library
ReferenceError: addTwo is not defined
[...]
Replacing result = addTwo(3); with result = testLibrary.addTwo(3); gives:
Error: A module Test The Library
TypeError: Object #<Object> has no method 'addTwo'
[...]
I'm probably missing something really basic, but at present, I'm completely stumped. Can someone point me in the right direction?
That is because you are not exporting this function from the module.
Take a look at that:
http://nodejs.org/api/modules.html#modules_module_exports

Categories

Resources