简体   繁体   English

在 Angular E2E 测试中使用 select(name).options

[英]Using select(name).options in Angular E2E test

I have a multi-value selector, which I am trying to write an E2E test for.我有一个多值选择器,我正在尝试为其编写 E2E 测试。 Using select(name).option works fine, but when I try to use select(name).options to select multiple values it doesn't work.使用select(name).option工作正常,但是当我尝试使用select(name).options选择多个值时它不起作用。

I tried to put together some code together to demonstrate, but can't get it working.我试图将一些代码放在一起进行演示,但无法使其正常工作。

HTML: HTML:

<html lang="en">
   <head>
      <title>End2end Test Runner</title>
      <script src="http://code.angularjs.org/1.0.1/angular-scenario-1.0.1.js"
      ng-autotest>
      </script>
   </head>
   <body ng-app="MyApp">
      <div ng-controller="MyAppCtrl">
         {{model.exampleValue}}
         <select id="a_selector"                 
            ng-model="model.selectedItems"
            ng-options="item.name for item in model.items"
            multiple="multiple">
         </select>
      </div>
   </body>

Javascript: Java脚本:

var app = angular.module('MyApp', ['ngResource'])
app.controller('MyAppCtrl', function($scope) {         
     $scope.model = {};
     $scope.model.exampleValue="an example value";
     $scope.model.items = [{"name": "Product1"}, {"name": "Product2"}];
});
app.config(['$routeProvider', function ($routeProvider, $scope) {
  $routeProvider.when('/', {controller:MyAppCtrl});
}]);
describe('my app', function() { 
  it('should display the home page', function() {                  
    browser().navigateTo('/');
    // this should work (when I can fix this code!)
    select("model.selectedItems").option("Product1");
    expect(element("#a_selector option:selected").count()).toEqual(1)

    // this doesn't, nothing is selected
    select("model.selectedItems").options("Product1", "Product2");
    expect(element("#a_selector option:selected").count()).toEqual(2)
  });
});

The line线路

select("model.selectedItems").option("Product1");

fails with error message:失败并显示错误消息:

Selector select[ng\:model="model.selectedItems"] did not match any elements.

I'd appreciate it if someone can (1) help me identify why the above code isn't working at all, and (2) help me understand why select(name).options isn't working.如果有人可以 (1) 帮助我确定为什么上面的代码根本不起作用,并且 (2) 帮助我理解为什么select(name).options不起作用,我将不胜感激。 (I know there are other techniques I could use to achieve the same thing, but the real select in the production code also has an ng-change attribute which does not fire when I try the workarounds). (我知道我可以使用其他技术来实现同样的事情,但生产代码中的真正select也有一个ng-change属性,当我尝试变通方法时它不会触发)。

Thanks,谢谢,

Graeme Ludwig.格雷姆·路德维希。

I am still trying to figure out why the select(name).option() is not working but I could get your example working by the following modifications:我仍在尝试弄清楚为什么select(name).option()不起作用,但我可以通过以下修改让您的示例工作:

  1. Include angular.js before angular-scenario.js在 angular-scenario.js 之前包含 angular.js
  2. Take out ngResource dependency - you don't need it here去掉 ngResource 依赖——你在这里不需要它
  3. Put <span>{{model.selectedItems}}</span> after your <select> tag to see what you have chosen.<span>{{model.selectedItems}}</span>放在<select>标签之后以查看您选择的内容。

I will update once I figure out the second part.一旦我弄清楚第二部分,我会更新。

Shouldn't select("model.selectedItems").option("Product1");不应该select("model.selectedItems").option("Product1"); be select("#a_selector").option("Product1");select("#a_selector").option("Product1"); instead?反而?

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

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