Is there any way to optimize / speed up the sending of data to a UI with Protractor?
Potential Solution I think at least a little hackery is required no matter how you optimize it - protractor doesn't give you this out of the box. However would a small helper function like this suit your needs? What else do you need to speed up sides text
input
s with ng-model
s?
function setNgModelToString(element, value) {
return element.getAttribute('ng-model').then(function (ngModel) {
element.evaluate('$eval("' + ngModel + ' = \'' + value + '\'") && $digest()');
});
}
Solution Example:
describe('angularjs homepage', function() {
it('should have a title', function() {
browser.get('http://juliemr.github.io/protractor-demo/');
var inputString1 = '';
var inputString2 = '';
for (var i = 0; i < 1000; i++) {
inputString1 += '1';
inputString2 += '2';
}
/* Uncomment this to see it runs much much slower when you enter each key. */
//element(by.model('second')).sendKeys(inputString1);
setNgModelToString(element(by.model('second')), inputString2);
expect(element(by.model('second')).getAttribute('value')).toEqual(inputString2);
});
});
Why does the solution work?
You need to use $eval
to wrap the assignment and not just assignment as evaluate
does not evaluate side effects (a nested evaluation, though... heh). Assuming that's truthy in angular expressions then $digest()
is called from the &&
; this causes a digest to happen, which you need to update everything since you set a value from outside the digest cycle.
Thoughts about the solution:
The whole idea behind an E2E test is to "emulate" an end-user using your app. This arguably doesn't do that as well as sending the keys one-by-one, or copy-and-paste (since pasting into elements is a valid way of entering input; it's just hard to set up due to flash, etc., see below).
Other Potential Solutions:
Copy and Paste: Create an element, enter text, copy it, paste the text sending Ctrl + V to the target element. This may require doing a bunch of fancy footwork, like using Flash (exposing the system clipboard is a security risk) and having "copy it" click an invisible flash player. See
executeScript
to evaluate functions on the target so that you have access to variables likewindow
if you need that.Parallelizing your tests. Read the official doc here and search for "shard" and then "multiple". If you're mainly worried about the duration of your entire test collection and not individual tests, scaling out your browser count is probably the way to go. However there's a good chance you are TDD-ing or something, hence needing each test to run faster.
You can do following -
ExamPage.prototype.enterDetailsInputData = function (modifier) {
var arr=[
{id:this.modalExamName, text:'Test Exam ' + modifier},
{id:this.modalExamVersionId, text:'Test exam version ' + modifier },
{id:this.modalExamProductVersionId, text:'Test exam product version ' + modifier},
{id:this.modalExamAudienceId,text:'Test exam audience ' + modifier},
{id:this.modalExamPublishedId, text:'2014-06-1' + modifier},
{id:this.modalExamPriceId, text:'100' + modifier},
{this.modalExamDurationId, text:'6' + modifier}
];
var Q = require('q'),
promises = [];
for (var i = 0; i < arr.length; i++) {
promises.push(page.sendKeys(arr[i].id, arr[i].text));
}
Q.all(promises).done(function () {
//do here whatever you want
});
};
sendKeys returns promise by default. See here -https://github.com/angular/protractor/blob/master/docs/api.md#api-webdriver-webelement-prototype-sendkeys
sendKeys(id: string, text: string) {
return element(by.id(id)).sendKeys(text);
}
If you really want to speed up the process of manipulating DOM in any way (including filling up data forms) one option to consider is to use: browser.executeScript
or browser.executeAsyncScript
. In such a case the webdriver
let the browser execute the script on its own -- the only overhead is to send the script body to the browser, so I do not think there may be anything faster.
From what I see, you identify DOM elements by id
s so it should smoothly work with the approach I propose.
Here is a scaffold of it -- tested it and it works fine:
browser.get('someUrlToBeTested');
browser.waitForAngular();
browser.executeScript(function(param1, param2, param3){
// form doc: arguments may be a boolean, number, string, or webdriver.WebElement
// here all are strings: param1 = "someClass", param2 = "someId", param3 = "someModifier"
var el1 = document.getElementsByClassName(param1)[0];
var el2 = document.getElementById(param2);
el1.setAttribute('value', 'yoohoo ' + param3);
el2.setAttribute('value', 'hooyoo ' + param3);
// depending on your context it will probably
// be needed to manually digest the changes
window.angular.element(el1).scope().$apply();
},'someClass', 'someId', 'someModifier')
small remark: If you pass webdriver.WebElement
as one of your argument it will be cast down to the corresponding DOM element
.