Tom*_*lar 2 angularjs angularjs-factory protractor
经过大量的搜索和尝试如何在e2e测试中使用工厂,我决定最后在这里问.
我有一个工厂定义,有一些功能,如下:
regFormApp.factory('myFactory', ['$moment', function($moment) {
return {
title: 'Super Title 2014!',
dates: [
{
date: '2014-10-21',
time: '08:30',
timezone: 'America/Mexico_City'
},
{
date: '2014-10-22',
time: '08:30',
timezone: 'America/Mexico_City'
}
],
prices: {
earlyBird: 1250,
twoMonths: 1375,
last: 1700
},
salesEndDates: function(ticketType) {
var endDate = this.dates[0].date;
switch (ticketType) {
case 'earlyBird':
endDate = '2014-07-19';
break;
case 'twoMonths':
endDate = $moment(endDate)
.subtract('months', 2)
.format('YYYY-MM-DD');
break;
default:
break;
}
return endDate;
},
getSalesPeriod: function(_format) {
var format = _format || 'YYYY-MM-DD';
var now = $moment().format(format);
if (now <= this.salesEndDates('earlyBird')) {
return 'earlyBird';
} else if (now > this.salesEndDates('earlyBird')
&& now <= this.salesEndDates('twoMonths')) {
return 'twoMonths'
}
return 'last';
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
我想getSalesPeriod()
在我的e2e测试中使用量角器,以测试是否启用了选择.
所以,我的表单看起来像这样:
...
<select id="earlyBirdSelect" class="form-control" ng-model="qty.earlyBird" ng-change="updateTotal()" ng-disabled="disableEarlyBird()">
<option>1</option>
...
</select>
<select id="twoMonthsSelect" class="form-control" ng-model="qty.twoMonths" ng-change="updateTotal()" ng-disabled="disableTwoMonths()">
<option>1</option>
...
</select>
<select id="lastSelect" class="form-control" ng-model="qty.last" ng-change="updateTotal()" ng-disabled="disableLast()">
<option>1</option>
...
</select>
...
Run Code Online (Sandbox Code Playgroud)
用于启用或禁用选择的内容是:
regFormControllers.controller('MainCtrl', ['$scope', '$http', '$filter', 'myFactory',
function($scope, $http, $filter, myFactory) {
...
$scope.disableEarlyBird = function() {
if (myFactory.getSalesPeriod() === 'earlyBird') {
return false;
}
return true;
};
// and analogously for twoMonths and last
...
};
}]);
Run Code Online (Sandbox Code Playgroud)
所以,我想做这样的事情:
describe('form', function() {
beforeEach(function() {
browser.get('app/#/'); // here is the form
});
it('should be enabled only earlyBird select', function() {
var earlyBirdSelect = element(by.css('#earlybirdSelect'));
var twoMonthsSelect = element(by.css('#earlybirdSelect'));
var lastSelect = element(by.css('#earlybirdSelect'));
// here I want to get 'earlyBird' from getSalesPeriod()
var period = myFactory.getSalesPeriod(); // it depends to current date
if (period === 'earlyBird') {
expect(earlyBirdSelect.getAttribute('disabled')).toBeFalsy();
expect(twoMonthsSelect.getAttribute('disabled')).toBeTruthy();
expect(lastSelect.getAttribute('disabled')).toBeTruthy();
}
});
});
Run Code Online (Sandbox Code Playgroud)
我不知道如何测试它.任何帮助非常感谢!
非常感谢你提前!
没有简单的方法可以做到这一点,除非你接触getSalesPeriod
到$scope
,在这种情况下你可以做lastSelect.evaluate('getSalesPeriod()')
如果您不想更改应用程序以公开要调用的函数,则必须通过执行JavaScript来访问Angular模块:
browser.executeScript(function() {
return angular.element(document).injector().get('myFactory').getSalesPeriod();
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1308 次 |
最近记录: |