简体   繁体   English

使用Protractor设置角度模型

[英]Setting an Angular model using Protractor

I'm trying to emulate a user story on my website with Protractor. 我正在尝试使用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. 在现实生活中,用户必须在输入中键入一些文本,然后用鼠标或更自然地用她的向下键来选择正确的命题。

The problem is that I can't seem to simulate that with Protractor. 问题是我似乎无法用Protractor模拟它。 element.sendKeys just does not allow you to do that. element.sendKeys只是不允许你这样做。 I have tried in a dozen different manners and it yields unpredictable results at best. 我已经尝试了十几种不同的方式,它最多会产生不可预测的结果。

So I would like to manipulate the ng-model behing my input directly. 所以我想直接操纵ng-model behing我的输入。 Is there a way to access the scope of an element from Protractor and call functions/set properties on it? 有没有办法从Protractor访问元素的范围并调用函数/ set属性?

Here is a simplified version of my problem : 这是我的问题的简化版本:

View : 查看:

<div ng-controller="MyController"> 
  <input id="my-input" ng-model="myModel"/>
</div>

Controller : 控制器:

 myModule.controller('MyController', ['$scope', function($scope){
   $scope.myModel = "";
   //[...]
 }]);

e2e Protractor test : 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?
  });
});

You can use .evaluate() to set your model value directly: 您可以使用.evaluate()直接设置模型值:

var elm = element(by.model("obj.field"));
elm.evaluate("obj.field = 'test';");

To get the model value: 要获得模型值:

elm.evaluate("obj.field").then(function (value) {
    console.log(value);
});

In this answer: How to select option in drop down protractorjs e2e tests 在这个答案: 如何在下拉量子器e2e测试中选择选项

They use this: .sendKeys(protractor.Key.ARROW_DOWN); 他们用这个: .sendKeys(protractor.Key.ARROW_DOWN); for sending DOWN arrows. 用于发送向下箭头。

It worth a try. 值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM