问题描述
我有类似的代码:
ExamPage.prototype.enterDetailsInputData = function (modifier) {
page.sendKeys(this.modalExamName, 'Test Exam ' + modifier);
page.sendKeys(this.modalExamVersionId, 'Test exam version ' + modifier);
page.sendKeys(this.modalExamProductVersionId, 'Test exam product version ' + modifier);
page.sendKeys(this.modalExamAudienceId, 'Test exam audience ' + modifier);
page.sendKeys(this.modalExamPublishedId, '2014-06-1' + modifier);
page.sendKeys(this.modalExamPriceId, '100' + modifier);
page.sendKeys(this.modalExamDurationId, '6' + modifier);
};
这是 page.sendKeys 函数.请注意,目前这不会返回任何承诺或类似的东西.如果函数编码不好,欢迎评论:
Here's the page.sendKeys function. Note that currently this is not doing any return of promises or anything like that. If the function is not coded well then I welcome comments:
// page.sendkeys function
sendKeys(id: string, text: string) {
element(by.id(id)).sendKeys(text);
}
我看着它慢慢地填满我屏幕上的每个字段,然后在接下来的更多测试中一次又一次地重复它.
I watch as it slowly fills out each field on my screen and then repeats it again and again in more tests that follow.
有什么方法可以优化这个,还是我必须等待一个又一个字段填满,并且不得不忍受需要很长时间才能运行的测试?
Is there any way that this could be optimized or do I have to wait for one field after the other to fill and have to live with tests that take a long time to run?
我认为 sendKeys 是基于 Promise 的.例如,我可以使用 AngularJS $q 同时发出所有 sendKeys,然后使用 $q 等待它们完成吗?
I assume sendKeys is promise based. Could I for example use AngularJS $q to issue all the sendKeys at the same time and then use $q to wait for them to complete?
推荐答案
潜在解决方案 我认为无论你如何优化它,至少需要一点技巧 - 量角器不会给你这个的盒子.但是,像这样的小助手功能是否适合您的需求?你还需要什么来加速 text
input
s 和 ng-model
s ?
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()');
});
}
解决方案示例:
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);
});
});
为什么该解决方案有效?
您需要使用 $eval
来包装赋值,而不仅仅是赋值,因为 evaluate
不会评估副作用(尽管是嵌套评估......呵呵).假设在角度表达式中这是真的,那么 $digest()
从 &&
中调用;这会导致发生摘要,您需要更新所有内容,因为您在摘要周期之外设置了一个值.
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.
关于解决方案的想法:
端到端测试背后的整个想法是模拟"使用您的应用的最终用户.可以说,这与逐个发送密钥或复制粘贴(因为粘贴到元素中是输入输入的有效方式;只是由于闪存等原因很难设置)相比,并不能做到这一点.,见下文).
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).
其他可能的解决方案:
复制和粘贴: 创建一个元素,输入文本,复制它,将文本发送 Ctrl + V 粘贴到目标元素.这可能需要做一些花哨的工作,比如使用 Flash(暴露系统剪贴板是一种安全风险)和复制"点击一个不可见的 Flash 播放器.参见
executeScript
评估目标上的函数,以便在需要时可以访问诸如window
之类的变量.
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.
并行化您的测试.阅读官方文档 here 并搜索shard",然后搜索multiple".如果您主要担心整个测试集的持续时间而不是单个测试的持续时间,那么扩展浏览器数量可能是您要走的路.但是,您很有可能正在使用 TDD 或其他方法,因此需要每个测试运行得更快.
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.
这篇关于有什么方法可以使用量角器优化/加速向 UI 发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!