I try to extend ElementFinder library. I wondering how I can require different methods with the same names?
I want to make something like:
// spec.js
var ef1 = require('./ef_extend1.js');
var ef2 = require('./ef_extend2.js');
expect(column_resizer.ef1.getWidth()).toEqual(18);
expect(column_resizer.ef2.getWidth()).toEqual(18);
Now I have an error:
TypeError: Cannot read property 'getWidth' of undefined
My required libraries:
// ef_extend1.js
var ElementFinder = $('').constructor;
ElementFinder.prototype.getWidth = function() {
return this.getSize().then(function(size) {
return size.width + 1;
});
};
And the second one:
// ef_extend2.js
var ElementFinder = $('').constructor;
ElementFinder.prototype.getWidth = function() {
return this.getSize().then(function(size) {
return size.width;
});
};
I guess you've used a solution from Protractor issue #1102, but now it can be accomplished a bit easier after PR#1633, because ElementFinder is now exposed in protractor global variable:
protractor.ElementFinder.prototype.getWidth = function () {
return this.getSize().then(function (size) {
return size.width;
});
};
expect($('body').getWidth()).toBe(100);
Update:
As I said in the comment, ElementFinder can only be extended again and again. If you already had a method getWidth, and you extend ElementFinder with one more getWidth implementation, then the first one will be overriden, there should not be any conflict. But you'll have to keep them in strict order depending on when do you want to use appropriate set of methods:
require('./ef_extend1.js');
expect(column_resizer.getWidth()).toEqual(18);
require('./ef_extend2.js');
expect(column_resizer.getWidth()).toEqual(18);
Actually I've came with some alternative approach, but I do not think it will be nice to use, but anyway. Here is a sample module with extension methods:
// ef_extend1.js
// shortcut
var EF = protractor.ElementFinder;
// holds methods you want to add to ElementFinder prototype
var extend = {
getWidth: function () {
return this.getSize().then(function (size) {
return size.width;
});
}
};
// will hold original ElementFinder methods, if they'll get overriden
// to be able to restore them back
var original = {};
// inject desired methods to prototype and also save original methods
function register() {
Object.keys(extend).forEach(function (name) {
original[name] = EF.prototype[name]; // save original method
EF.prototype[name] = extend[name]; // override
});
}
// remove injected methods and return back original ones
// to keep ElementFinder prototype clean after each execution
function unregister() {
Object.keys(original).forEach(function (name) {
if (typeof original[name] === 'undefined') {
// if there was not such a method in original object
// then get rid of meaningless property
delete EF.prototype[name];
} else {
// restore back original method
EF.prototype[name] = original[name];
}
});
original = {};
}
// pass a function, which will be executed with extended ElementFinder
function execute(callback) {
register();
callback();
unregister();
}
module.exports = execute;
And you will use them like that, being able to run protractor commands in "isolated" environments, where each of them has it's own set of methods for ElementFinder:
var ef1 = require('./ef_extend1.js');
var ef2 = require('./ef_extend2.js');
ef1(function () {
expect(column_resizer.getWidth()).toEqual(18);
});
ef2(function () {
expect(column_resizer.getWidth()).toEqual(18);
});
I'm not quire sure about it, maybe I am over-engineering here and there are solutions much easier than that.
Related
In view I have 4 buttons. In controller I have references to those buttons. There is the complicated logic to enable/disable each button, unfortunatelly scattered into several function - event listeners. Each listener has different parameters. Example:
listener1: function(data1) {
button1.setDisabled(data1.status = ProcessStatus.running);
button2.setDisabled(data1.status = ProcessStatus.running);
}
listener2: function(upload) {
button2.setDisabled(!upload.enabled);
}
I want to refactor the code: create single function in controller updateButtonsState. Then each listener would set somewhere a value and call updateButtonsState() without parameters.
listener2: function(upload) {
saveUploadEnabled(upload.enabled);
updateButtonsState();
button2.setDisabled(!upload.enabled);
}
updateButtonsState: function() {
var uploadEnabled = loadUploadEnabled();
var data1StatusProcessRunning = loadData1Status();
button1.setDisabled(data1StatusProcessRunning);
button2.setDisabled(uploadEnabled || data1StatusProcessRunning);
}
How to implement save* and load* methods? Where should I store those data?
Two major possibilities: You can store the state directly in the controller, or use a closure to create a "private" variable and not expose it to anything except the getter/setter functions that are your interface.
controllers have an init method that you could use for the closure:
init: function(application) {
var myPrivateVariable = {};
this.saveUploadEnabled = function(uploadEnabled) {
myPrivateVariable.uploadEnabled = uploadEnabled;
}
this.saveStatus = function(status) {
myPrivateVariable.status = status;
}
this.updateButtonState = function() {
button1.setDisabled(myPrivateVariable.status == ProcessStatus.Running || !myPrivateVariable.uploadEnabled)
}
}
Right to it. I have a global variable which I would like to export, so I can use the value in following specs. But since protractor is not working synchronously, export happens before the variable gets updated to the right value.
The action is a click on button where player gets created and I need the username to be exported.
Console.log contains the right value and also export happens as it should, only thing, that it exports the hardcoded value or undefined if I set globalUsername = "";
Anyone can help on how to make this export sinchronized, so it will wait for all describes to finish up or the variable to get updated.
describe ("Quick add player", function() {
it ("New player is created and credentials are displayed", function() {
browser.ignoreSynchronization = true;
var playerData1 = playerData.getText().then(function(text) {
console.log("1: ", text);
return text.split("\n")[0];
//console.log(text.split(" ")[1]);
});
var globalUsername = "1234";
playerData1.then(function(text) {
globalUsername = text.split(" ")[1];
//expect(globalUsername).not.toEqual("");
console.log("*****************\n" + globalUsername);
});
});
});
module.exports = globalUsername;
Because you are using browser ignore synchronization, your tests will probably have nested then statements because you need to wait for promises to resolve. Instead of creating multiple then statements, you could put them into a single then statement. Also, you could tie your variable to the global namespace with global.username = username or add it to the global browser object with browser.username = username
describe ("Quick add player", function() {
it ("New player is created and credentials are displayed", function() {
browser.ignoreSynchronization = true;
global.username = "1234";
playerData.getText().then(function(text) {
console.log("1: ", text);
global.username = text.split("\n")[0].split(" ")[1];
console.log("*****************\n" + global.username);
});
});
});
// access using global.username tied to the global namespace
// instead of var username = require('thisfile');
So this solved my issue, where I export a string and not the variable....searched google with wrong tags.
This is placed outside the first describe at the bottom.
module.exports = {
exportUsername: function () {
return globalUsername;
}
};
I am writing tests using Jasmine for my angular application. All the tests are passing. My class looks like follows:
class xyz implements ng.IComponentController {
private myList: ng.IPromise<MyList[]> ;
//declare necessary variables
/* #ngInject */
constructor(private ListService: ListService,
) {
this.myList = this.ListService.getList();
}
public onChange(): void {
this.isNameUnique(this.name).then(function(unique){
scope.isUnique = unique;
scope.errorNameInput = !reg.test(scope.name) || !scope.isUnique;
scope.myFunction({
//do something
});
});
}
public isNameUnique(name: string): ng.IPromise<boolean> {
return this.myList
.then(
(names) => {
_.mapValues(names, function(name){
return name.uuid.toLowerCase();
});
return (_.findIndex(names, { uuid : uuid.toLowerCase() }) === -1) ? true : false;
});
}
}
Here, I am using ListService to pre-populate my list in the constructor itself (so it calls the service only once). Then, in my onChange method, I am checking
if a name is unique or not. The isNameUnique is returning a boolean promise.
Now, I'm trying to get 100% coverage for my test. I'm getting confused about testing isNameUnique method here. My first test is:
(Assuming myList is a json similar to response I will get from service)
this.$scope.myFunction = jasmine.createSpy('myFunction');
it('should ...', function() {
this.view.find(NAME_INPUT).val('blue').change(); // my view element.
this.getList.resolve(myList);
this.controller.isNameUnique('blue').then(function (unique) {
expect(unique).toEqual(false); //since blue is already in my json
expect(this.controller.errorNameInput).toEqual(true); //since its not unique, errornameinput will be set to true
expect(this.$scope.myFunction).toHaveBeenCalled();
});
});
I would expect this test to cover the line: scope.errorNameInput = !reg.test(scope.name) || !scope.isUnique and invocation of myFunction() but it still shows uncovered. Not sure why.
Please let me know if you see anything else wrong since I'm quite new to Angular and Jasmine. Thanks.
You need to call $scope.$digest() to cause your promise to resolve in your test. There is a handy tutorial that discusses this in depth here
Hope that helps!
I'm refactoring my PageObject for my tests. Currently I'm checking the labels present in 2 different buttons in a modal.
/* home-spec.js */
it('Some test', function(){
expect(homePage.getButton1Label()).toEqual(expectations.btn1);
expect(homePage.getButton2Label(true)).toEqual(expectations.btn2);
});
Although this currently works I need to pass a variable indicating if the modal is open or not. That's the bit I'm trying to fix.
/* home-page.js */
var HomePage = function () {
function getModalContent(modalName, isModalOpen){
/* isModalOpen = element(by.css('.modal-content')).isPresent(); */
if(!isModalOpen){
var manageProductsView = getUiView('SOME_VIEW');
var btn = getButton(manageProductsView);
btn.click();
browser.waitForAngular();
}
return element(by.css('.modal-content'));
}
function getButtonLabel(buttonBinding, isModalOpen){
/* isModalOpen = element(by.css('.modal-content')).isPresent(); */
var modalcontent = getModalContent('MODAL_NAME', isModalOpen);
var modalFooter = modalcontent.element(by.css('.modal-footer'));
var btn = modalFooter.element(by.binding(buttonBinding));
return btn.getText();
}
return {
getButton1Label: function(isModalOpen){
return getButtonLabel('btn1', isModalOpen);
},
getButton2Label: function(isModalOpen){
return getButtonLabel('btn2', isModalOpen);
}
}
}
What I would like to do is remove that isModalOpen dependency but I don't seem to find the correct way to do it. The comments indicate what I've tried and seemed to be the way to go. Also tried to wrap it in the then block.
EDIT
Based on Vlad answer I edited my getButtonLabel function so it checks if the modal is open
function getButtonLabel(buttonBinding){
return element(by.css('.modal-content')).isPresent().then(function(isModalOpen){
var modalcontent = getModalContent('MODAL_NAME', isModalOpen);
var modalFooter = modalcontent.element(by.css('.modal-footer'));
var btn = modalFooter.element(by.binding(buttonBinding));
return btn.getText();
});
}
Was trying to avoid handling promises manually but I guess in some cases it's unavoidable
Your commented part is the way to go:
function getModalContent(modalName){
var modalContent = element(by.css('.modal-content'));
var isModalOpen = modalContent.isPresent();
return isModalOpen.then(function(open) {
if(!open){
var manageProductsView = getUiView('SOME_VIEW');
var btn = getButton(manageProductsView);
return btn.click()
.then(function(){
return modalContent;
});
}
return modalContent;
});
}
function getButtonLabel(buttonBinding){
var modalcontent = getModalContent('MODAL_NAME');
var btnText = modalcontent
.then(function(content) {
return content
.element(by.css('.modal-footer'))
.element(by.binding(buttonBinding))
.getText();
});
return btnText;
}
Beware that the modal remains open after the test, you might want to add something to close it if it's open to maintain consistent state throughout tests.
It's much better to know for sure if it's open before doing operations on it, so that you can open it yourself if it isn't and you need it to be, instead of doing conditionals like these - they are expensive because of promise chaining.
My general feeling is that the logic looks a bit too complicated, you might want to refactor some of the stuff in the future :p
I want to edit my collection using jeditable, where modifyCollection is a function associated with the event dblclick. I have the following code:
initialize : function(options) {
view.__super__.initialize.apply(this, arguments);
this.collection = this.options.collection;
this.render();
},
render : function() {
var template = _.template(tpl, {
collectionForTemplate : this.collection ,
});
this.el.html(template);
return this;
},
modifyCollection : function (event){
$('#name').editable(function(value, settings) {
return (value);
}
,
{ onblur: function(value) {
this.modelID=event.target.nameID;
this.collection = this.options.collection;
console.log("This Collection is: " + this.collection); //Shows : undefined
//
this.reset(value);
$(this).html(value);
return (value);
}
});
The idee is to update the model and subsequently, the collection by means of jeditable. The in place editing works fine, but the problem is, I am not able to pass the collection into the function. I want to save all the changes to my collection locally and send them to the server at a later time. What am I doing wrong here?
Moved the comment to a formal answer in case other people find this thread.
The this inside your onblur() function is not pointing to this collection. Try adding var self = this; inside your modifyCollection() function then in your onblur() change this.collection to self.collection like so:
modifyCollection : function (event) {
var self = this; // Added this line
// When working with functions within functions, we need
// to be careful of what this actually points to.
$('#name').editable(function(value, settings) {
return (value);
}, {
onblur: function(value) {
// Since modelID and collection are part of the larger Backbone object,
// we refer to it through the self var we initialized.
self.modelID = event.target.nameID;
self.collection = self.options.collection;
// Self, declared outside of the function refers to the collection
console.log("This Collection is: " + self.collection);
self.reset(value);
// NOTICE: here we use this instead of self...
$(this).html(value); // this correctly refers to the jQuery element $('#name')
return (value);
}
});
});
UPDATE - Foreboding Note on self
#muistooshort makes a good mention that self is actually a property of window so if you don't declare the var self = this; in your code, you'll be referring to a window obj. Can be aggravating if you're not sure why self seems to exist but doesn't seem to work.
Common use of this kind of coding tends to favor using that or _this instead of self. You have been warned. ;-)