问题描述
我正在尝试使用 Protractor 在我的网站上模拟用户故事.
I'm trying to emulate a user story on my website with Protractor.
用户必须输入使用自动完成的输入.在现实生活中,用户必须在输入中键入一些文本,然后使用鼠标或更自然地使用向下箭头键选择正确的命题.
The user has to type in an input that uses auto-completion. In real life, the user has to type some text in the input, then select the right proposition with either her mouse or more naturally her downarrow key.
问题是我似乎无法用 Protractor 模拟它.element.sendKeys 只是不允许您这样做.我已经尝试了十几种不同的方式,但充其量只能产生不可预测的结果.
The problem is that I can't seem to simulate that with Protractor. element.sendKeys just does not allow you to do that. I have tried in a dozen different manners and it yields unpredictable results at best.
所以我想直接操作我输入的 ng-model.有没有办法从量角器访问元素的范围并在其上调用函数/设置属性?
So I would like to manipulate the ng-model behing my input directly. Is there a way to access the scope of an element from Protractor and call functions/set properties on it?
这是我的问题的简化版本:
Here is a simplified version of my problem :
查看:
<div ng-controller="MyController">
<input id="my-input" ng-model="myModel"/>
</div>
控制器:
myModule.controller('MyController', ['$scope', function($scope){
$scope.myModel = "";
//[...]
}]);
e2e 量角器测试:
describe("setting myModel to a fixture value", function(){
it("should set myModel to 'a test value'", function(){
var myInput = element('my-input');
// Now what?
});
});
推荐答案
你可以使用.evaluate()
直接设置你的模型值:
You can use .evaluate()
to set your model value directly:
var elm = element(by.model("obj.field"));
elm.evaluate("obj.field = 'test';");
获取模型值:
elm.evaluate("obj.field").then(function (value) {
console.log(value);
});
这篇关于使用 Protractor 设置 Angular 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!