Verifying amount of elements using expect - javascript

So I have an element identified
var errorAlert = element.all('[id*="type-alert"]');
I am trying to create a function that will verify there are 2 alerts displayed..
this.isAlertsDisplayed = function() {
return expect(errorAlert.count()).toEqual(2);
};
I keep gettin a friken typeError : .toEqual is not a function when I have seen this example work for others.. What am I doing wrong?

#Danny is on the right track. Except that you are simply missing the by.css() part, replace:
var errorAlert = element.all('[id*="type-alert"]');
with:
var errorAlert = element.all(by.css('[id*="type-alert"]'));
Or with:
var errorAlert = $$('[id*="type-alert"]');
Note that you can spot this kind of problems much earlier in the process - statically even before executing your tests and trying to figure out what went wrong. If you would use ESLint and eslint-plugin-protractor plugin (>=1.29.0), the valid-locator-type rule would warn you if you have passed a literal to element() or element.all(), or if you have passed a "by" locator to $() or $$().
And, if you have ESLint configured in your IDE of choice, here is how you would get the warning during the live-coding (example from PyCharm):

your locator is wrong. Your locator should have by.css() or by.id or something like this.
Try var errorAlert = $$('[id="type-alert"]') or
var errorAlert = element.all(by.id('type-alert'))

Related

Using require('...') with a variable vs. using a string in webpack

I have a problem that doesn't make much sense to me.
I'm mapping an array of objects that have a "name" and a "href" property.
let appleIcons = _.map(appleIcons, appleIcon => {
appleIcon.href = require(appleIcon.href);
return appleIcon;
});
Inside of the loop I want to require the image but it throws an error ".*$:11 Uncaught Error: Cannot find module".
When I print the value of appleIcon.href and i try to put it directly into the require('') it works.
appleIcons = _.map(appleIcons, appleIcon => {
appleIcon.href = require('./../../mobile-config/apple-icon-57x57.png');
return appleIcon;
});
So can you explain me why the second example works and the first one throws an error? How do i put a variable inside of require('')?
Thanks!
Since Webpack is running in build-time, it can't figure out which modules to bundle when the name is a dynamic variable. You can give it hints by specifying part of the path (for example, if you know all the modules are in a single directory).
This answer can help:
https://stackoverflow.com/a/33048000
(Also check require.context by Webpack. Another example is karma tests, here.)
Alternatively - if you know the filenames in advanced, it's better to add another build step to output them a strings to the file, that way Webpack can bundle them.
Adding an empty string fixed the problem for me. So, below code should work:
let appleIcons = _.map(appleIcons, appleIcon => {
appleIcon.href = require('' + appleIcon.href);
return appleIcon;
});

Testing JavaScript libraries with nodeunit - FileApi, substr undefined

I wan to test my library with nodeunit, and I use File Object in it, on website everything is working (FileAPI is implemented there) but when I'm trying to test it with nodeunit i get an error:
Fatal error: File is not defined
I assume that I have to add:
var FileAPI = require('file-api');
var File = FileAPI.File;
at the begging of the code, but I don't need that when I include that library to website, how to deal with that?
To use nodeunit I had to add module.exports at the end, is it necessary as well? (code sample on github)
What's more, when I'm trying to test this code:
https://github.com/GeoSmartCity-CIP/gsc-client/blob/feature/upload-data-file/src/upload/upload.js
with this test:
var gsc = require('../../src/upload/upload');
var FileAPI = require('file-api');
var File = FileAPI.File;
var exports = exports || {};
exports.isFileTypeCorrect = function(test) {
var file = new File('test.geojson');
var asd = new gsc.upload.Data(file);
test.ok(asd.isFileTypeCorrect(), 'this assertion should pass');
test.done();
};
I'm getting Fatal error: Cannot read property 'substr' of undefined error, what's the problem?
EDIT:
problem with substr is propably from isShapefileCorrect function, but still dont know why?
var asd = new gsc.upload.Data(file); Isn't it asynchronous function? Then probably asd.isFileTypeCorrect() should be called inside upload callback. Also you defining isFileTypeCorrect function as module exports and call itself inside itself. Isnt it an infinite loop?

How can i get xpath count in selenium webdriver using jasmine javascript?

var sample1 = browser.findElements(driver.By.xpath('//somenode')).getXpathCount();
console.log( sample1.intValue() );
while printing the count I am getting error:
error occuredTypeError: undefined is not a function
Like #alecxe stated, the syntax for getXpathCount() is browser.getXpathCount("//somenode").
I saw you opened an issue on the selenium git and had more code there. What isn't showing here is you have just the following.
var browser = require('selenium-webdriver');
var sample1 = browser.findElements(driver.By.xpath('//somenode')).getXpathCount();
console.log( sample1.intValue() );
I haven't used WebDriverJs, so someone please correct me if I am wrong, but I think you need to create a browser object. Right now you only have created a driver object named browser.
Can you try the following snippet?
var webdriver = require('selenium-webdriver');
var browser = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
browser.get('http://en.wikipedia.org/wiki/Wiki');
browser.getXpathCount('//*[#id="www-wikipedia-org"]/div[1]/div');
findElements method returns an Array promise, so you have to do something like this:
browser.findElements(driver.By.xpath('//somenode')).then(function(elements) {
var count = elements.length;
...
})
I think you are not using getXpathCount() correctly. You should do it this way:
browser.getXpathCount("//somenode");

How can I run two files in javascript with node?

I am new to javascript and Node.js and having problems testing some code I wrote recently. I am trying to test code written in a file called "compareCrowe.js" with "testCrowe.js" using Node.js.
Here are the contents of testCrowe.js:
var compareCrowe = required['./compareCrowe'];
console.log('begin test');
var connection = {Type:1, Label:"label", linkTo:null};
var table1 = {name:"table1", body:"description1", out:[connection]};
var table2 = {name:"table2", body:"description2", out:null};
connection.linkTo = table2;
var crowe = [table1, table2];
var result = compareCrowe.compareCrowesFoot(crowe, crowe);
console.log(result.feedback);
where the function "compareCrowesFoot" is defined in compareCrowe.js. From the console on an Ubuntu virtual machine I ran:
node compareCrowe.js testCrowe.js
however, nothing was printed. There were no errors or warnings or explanation of any kind. It didn't even print the "begin test" line I placed at the top of testCrowe.js. If I run the command:
node testCrowe.js
it complains that compareCrowesFoot is undefined. How can I test the contents of compareCrowe.js?
Welcome to the party of JS.
I'm not sure where you're learning from, but a few of the resources that have helped me and many others are superherojs.com, nodeschool.io, the MDN developer docs, Node.js API docs, and Youtube (seriously).
The basic idea of Node.js is that it operates with modules (chunks of reusable code), which is what NPM is made up of. These can then be included in other modules and used anywhere else in your application.
So for a basic example, say you had compareCrowe.js, to make it includable/reusable in another file, you could write something like:
module.exports = function() {
var compareCrowesFoot = function(crowe1, crowe2) { /* compare crows feet and return something here */ }
return { compareCrowesFoot: compareCrowesFoot };
// return an object with a property of whatever you want to access it as , and the value as your function name
// e.g. - you could return { compare: compareCrowesFoot };
}
Then in testCrowe.js you could require compareCrowe like this:
var compareCrowe = require("./compareCrowe");
/* your code here... */
var result = compareCrowe.compareCrowesFoot(crowe1, crowe2);
// if your returned object was { compare: compareCrowesFoot };
// this would be compareCrowe.compare(crowe1, crowe1);
And to run your tests, you could then run node testCrowe.js from the command line.
In your case it seems like you've got your syntax a little messed up. It should be more like:
var compareCrowe = require('./compareCrowe.js');
That would make any methods you've exported in compareCrowe.js, such as your compareCrowe.compareCrowesFoot function, available to testCrowe.js.
Then, in your terminal, you would run the following:
node testCrowe.js
And that should do the trick provided you don't have any further errors in your code.

cannot denodeify methods in node-ftp module

I am new to both node.js and promise style function call. By looking at an denodeify example at http://runnable.com/Ulatc0QnzUgUAAAK/adapting-node-js-with-q-for-promises, I am trying to denodeify the methods of the node.js node-ftp module as following:
var ftp = require('ftp');
var q = require('q');
var ftpClient = new ftp();
ftpClient.on('ready', function() {
var ftpList = q.denodeify(ftpClient.list);
ftpList().then(function(list) {
console.log(list);
}.then(null, function(err) {
console.log(err);
}).done(function() {
ftpClient.end();
});
});
ftpClient.connect();
However, when running that code with node, it shows the error "list error: TypeError: Object # has no method '_pasv'"
I am not sure what's wrong with that piece of code. Does anyone know what's wrong with that? Can you point me some way to debug/troubleshoot the cause of that error message?
Thanks.
When you pass
ftpClient.list
to Q.denodefiy, you are getting the function object, list from the ftpClient object. It will be just a function and the relationship with the parent is lost. This is important because, the bound function list might be dependent on the ftpClient object. So, you must make sure that link is not broken.
Quoting from the Q.denodeify docs,
Note that if you have a method that uses the Node.js callback pattern,
as opposed to just a function, you will need to bind its this value
before passing it to denodeify, like so:
var Kitty = mongoose.model("Kitty");
var findKitties = Q.denodeify(Kitty.find.bind(Kitty));
The better strategy for methods would be to use Q.nbind, as shown below.
So, you can fix your code in two ways,
Using Q.denodeify and Function.prototype.bind, like this
var ftpList = q.denodeify(ftpClient.list.bind(ftpClient));
Using Q.nbind, like this
var ftpList = q.nbind(ftpClient.list, ftpClient);
you need to use q.nbind
q.nbind(ftpClient.list, ftpClient);

Categories

Resources