2013-05-06 69 views
2

是否可以從Angular e2e場景執行jQuery命令?如何從Angular e2e測試範圍執行jQuery?

例如,如果我想執行:$('.picker-col-id-id').attr('class'); 我得到一個錯誤:

TypeError: Property '$' of object [object Object] is not a function

+0

因爲jQuery不存在。你確定你正確地引用了它嗎? – SomeShinyObject 2013-05-06 14:31:07

+0

嘗試'jQuery('。picker-col-id-id')'... – 2013-05-06 14:46:24

+0

該解決方案沒有幫助,jQuery無法識別e2e範圍 – Igal 2013-05-07 06:28:02

回答

1

正如它看起來像jQuery缺少註釋中提到。您需要將其添加到您的業力配置文件或直接添加到runner.html。

如果你想將它添加到你的人緣配置文件增加了files條目添加引用jQuery的,例如,

files = [ 
    ANGULAR_SCENARIO, 
    ANGULAR_SCENARIO_ADAPTER, 
    'app/components/jquery/jquery.js', // <== This should point to jQuery 
    'test/e2e/**/*.js' 
]; 

另外,如果你從runner.html運行的端到端的測試,你可以加載它在那裏,后角和腳本,如前:

<!doctype html> 
<html lang="en"> 
    <head> 
     <title>End2end Test Runner</title> 
     <script src="../lib/angular/angular-scenario.js" ng-autotest></script> 
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
     <script src="scenarios.js"></script> 
    </head> 
    <body> 
    </body> 
</html> 
11

這裏的問題是,AngularJs情景測試運行運行在一個iframe應用程序。跑步者本身沒有加載jQuery。

最好使用角度場景dsl。從e2e testing docs

element(selector, label).{method}(key, value)

Executes the method passing in key and value on the element matching the given jQuery selector, where method can be any of the following jQuery methods: attr, prop, css. The label is used for test output.

雖然不是從文檔清晰,你也可以使用「ATTR」的方法只有1參數來獲得屬性的值。

element('.picker-col-id-id').attr('class'); 

如果需要其他的jQuery的功能,象對焦(),你可以這樣來做:

element('.picker-col-id-id').query(function(elements, done) { 
    elements.focus(); 
    done(); 
}); 

或延長角DSL

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

而且使用這種方式:

jQueryFunction('.picker-col-id-id', 'focus'); 

或一般:

jQueryFunction(selector, jQueryFunctionName, arg1, arg2, ...); 
+0

謝謝。浪費了幾天的時間尋找這些信息。 – 2013-07-19 15:04:07