問題描述
我正在嘗試使用 Protractor 在我的網(wǎng)站上模擬用戶故事.
I'm trying to emulate a user story on my website with Protractor.
用戶必須輸入使用自動(dòng)完成的輸入.在現(xiàn)實(shí)生活中,用戶必須在輸入中鍵入一些文本,然后使用鼠標(biāo)或更自然地使用向下箭頭鍵選擇正確的命題.
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 只是不允許您這樣做.我已經(jīng)嘗試了十幾種不同的方式,但充其量只能產(chǎn)生不可預(yù)測的結(jié)果.
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.有沒有辦法從量角器訪問元素的范圍并在其上調(diào)用函數(shù)/設(shè)置屬性?
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()
直接設(shè)置你的模型值:
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);
});
這篇關(guān)于使用 Protractor 設(shè)置 Angular 模型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!