2013-05-13 25 views
2

我在嘗試E2E測試我的angularjs應用程序。它有一個select2選擇框,其中填充了來自API的項目。這個select2選擇框有一個將被添加到搜索中的過濾器列表。在angularjs E2E測試中打開select2選擇框

<input id="filter-select2" ui-select2="dataArray" ng-model="selected"/> 

我遇到的問題是,我似乎無法打開我正在做的E2E測試中的選擇框。如果我加載應用程序,我可以在Chrome Javascript Console中執行$('#s2id_filter-select2').select2('open');以打開select2選擇框。我也可以做angular.element('#s2id_filter-select2').select2('open');

但是,如果我嘗試在E2E測試中做到這一點,該元件似乎並沒有.select2()

我在E2E測試都嘗試element('#s2id_filter-select2').select2('open');

element('#s2id_filter-select2').query(function (e, done) { 
    e.select2('open'); 
    done(); 
}); 

。我可以在後面的示例中調用.fadeOut()element().query()),但不能調用.select2()

我試着在E2E測試配置中包括select2.js和angularui.js(我使用angular-ui中的指令),但是這並沒有改變任何東西。

有沒有人從E2E測試中調用某些jQuery庫(如select2)時有成功?

我也考慮過直接跳過select2方塊,也就是直接設置「selected」模型,因爲我有一個$ watch,它會在filter select上更新應用程序,但我似乎無法找到方法通過測試本身設置模型。

端到端配置是使用以下文件:

files = [ 
    ANGULAR_SCENARIO, 
    ANGULAR_SCENARIO_ADAPTER, 
    'tests/students/scenarios/*.scenario.js', 
    'dev/students/js/script.js', 
    'dev/students/js/*.js' 
]; 

我試圖具有SELECT2,jQuery和角度的UI包括之前和之後測試/和DEV /文件。

編輯:我可以使用input('selected').enter('sth');設置所選模型,但我似乎無法將其設置爲模型。我可能必須爲此編寫一個自定義DSL?

回答

1

您確實可以使用自定義DSL在角度e2e測試中打開select2框。我不採取任何信用下面的代碼塊,我發現它在: How to execute jQuery from Angular e2e test scope?

通過添加:

angular.scenario.dsl('jQueryFunction', function() { 
    return function(selector, functionName /*, args */) { 
     var args = Array.prototype.slice.call(arguments, 2); 
     return this.addFutureAction(functionName, function($window, $document, done) { 
      var $ = $window.$; // jQuery inside the iframe 
      var elem = $(selector); 
      if (!elem.length) { 
       return done('Selector ' + selector + ' did not match any elements.'); 
      } 
      done(null, elem[functionName].apply(elem, args)); 
     }); 
    }; 
}); 

到您的端對端測試文件的頂部,然後你可以打開,像這樣一個選擇2盒:

jQueryFunction('#s2id_roleselect', 'select2', 'open'); 

用你自己的id替換roleselect。所有功勞歸功於用戶Jeroen V.提供DSL。

+0

對不起,對於遲到的回答,我在你回答之前不久就停止了這個項目的工作。我現在只是有一些死亡時間,並決定嘗試一下,它完美的工作。非常感謝你!能夠在代碼中使用除醜陋的解決方法之外的東西感覺很好。 – 2013-07-17 20:39:08

相關問題