Getting element from pageobject in protractor - javascript

Im writing some Protractor testcases by using a pageobject. However im having trouble using elements directly from the Pageobject file, in the spec file.
Possibly im missing some JS nuance, since i havent written that much in JS.
i would like to use some elements defined in Pageobject like so:
var PageObject = function()
{
var loginUsername = element(by.id('loginusername'));
//other methods
};
module.exports = PageObject;
to be used in my spec file like so:
var PageObject = require('./pageObject.page.js');
describe( ' Login page ', function(){
it('type something in the usernamefield', function(){
var pageObject = new PageObject();
pageObject.get();
pageObject.loginUsername.sendKeys('Test');
});
});
Using methods (for example get but also others)works fine, but using elements directly causes some undefined errors.
Im trying to copy from something like this, where supposedly it should work.
https://ramonvictor.github.io/protractor/slides/#/33

Something you are missing,
bind your variable to object in pageObject.page.js, using this keyword
var PageObject = function()
{
this.loginUsername = element(by.id('loginusername'));
......
};
module.exports = PageObject;
In Spec first get the real application and do real work
it('type something in the usernamefield', function(){
browser.get('"https://....');
var pageObject = new PageObject();
pageObject.loginUsername.sendKeys('Test');
});
This works for me perfect,

Related

Intellisense for protractor?

I'm looking for any intellisense for protractor that will load required files.
For example I have this Page Object.
var example_page = function()
{
this.abcde = element(by.model('abc'));
};
module.exports = new example_page();
And this spec file
describe('angularjs homepage', function() {
var example_page = require('./example_po.js');
example_page.abcde // I want intellisense that finds this property.
});
So basically I want intellisense that finds page objects and finds every property and function in them to display for me, because I'm tired of going from file to file looking for properties...

Using external test data in protractor test

I am trying to reference test data using a JSON but it appears as undefined. I am trying to use a Page Object Model and reference external test data. When referencing the values in my test data and passing into a function they are appearing as undefined
I have a simple JSON like:
[{
"userNameValue": "mrUserUser",
"passwordValue": "i_am_correct"
}]
My tests then look like:
'use strict';
var testData = require('./testData/testData.json');
var AngularPage = require('./pages/logon.page.js');
describe('travel portal logon page', function () {
var logonPage;
beforeEach(function () {
logonPage = new AngularPage();
});
it('should fail to logon', function() {
logonPage.loginToSite(testData.userNameValue, testData.passwordValue);
expect(logonPage.errorMessageText).toEqual('Invalid Login');
});
});
Both testData.userNameValue and testData.passwordValue are being written as undefined. I have also tried referencing using the other format of testData['userNameValue'].
Am I missing something?
Please help, thanks
Make sure you have exported your testData.json file as required using module.exports function. Also your test data is not an object in the first place, its an array. In order for your test script to work, change the test data into an object. Here's how -
var testData = {
"userNameValue": "mrUserUser",
"passwordValue": "i_am_correct"
};
If you want to keep the testData the way it is now(an array), then change the way you access the data in your test steps. Here's how -
logonPage.loginToSite(testData[0].userNameValue, testData[0].passwordValue);
Hope it helps.

JavaScript - Instantiate from require

I have a Node program. This program is importing some code. That code looks like the following:
sample.js
function SampleModel() {
this.name = 'Test';
}
module.exports = SampleModel;
I am using SampleModel in another file. Currently, I am successfully using it like this:
another.js
var SampleModel = require('./sampleModel');
var model = new SampleModel();
For the purpose of education, is there a way to condense these two lines down to a single line of JavaScript? If so, how?
Thank you!
Like this:
var model = new (require('./sampleModel'))();
It's quite common to access the required module directly in the import line, but you'll mostly see property access (like require('events').EventEmitter).

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