簡體   English   中英

在 Angular E2E 測試中使用 select(name).options

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

我有一個多值選擇器,我正在嘗試為其編寫 E2E 測試。 使用select(name).option工作正常,但是當我嘗試使用select(name).options選擇多個值時它不起作用。

我試圖將一些代碼放在一起進行演示,但無法使其正常工作。

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>

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)
  });
});

線路

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

失敗並顯示錯誤消息:

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

如果有人可以 (1) 幫助我確定為什么上面的代碼根本不起作用,並且 (2) 幫助我理解為什么select(name).options不起作用,我將不勝感激。 (我知道我可以使用其他技術來實現同樣的事情,但生產代碼中的真正select也有一個ng-change屬性,當我嘗試變通方法時它不會觸發)。

謝謝,

格雷姆·路德維希。

我仍在嘗試弄清楚為什么select(name).option()不起作用,但我可以通過以下修改讓您的示例工作:

  1. 在 angular-scenario.js 之前包含 angular.js
  2. 去掉 ngResource 依賴——你在這里不需要它
  3. <span>{{model.selectedItems}}</span>放在<select>標簽之后以查看您選擇的內容。

一旦我弄清楚第二部分,我會更新。

不應該select("model.selectedItems").option("Product1"); select("#a_selector").option("Product1"); 反而?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM