如何接受与AngularJS的Jasmine / Protractor的confirm()对话框

时间:2014-08-20 13:46:36

标签: angularjs selenium-webdriver jasmine protractor

我一直在努力为我当前的应用程序添加端到端测试,但由于我的代码中出现了confirm()对话框,我遇到了无法完成测试的问题(如果他们确定他们想要删除这个东西,做一个完整性检查)。问题是我在使用量角器/茉莉花时不知道如何确认警报中的动作。

//Set up variables for use
var columnNameInput = element(by.model('ColumnNameToOverride'));
var columnDataTypeSelecter = element(by.model('ColumnDataTypeToOverride'));
var addColumnButton = element(by.id('addColumnOverride'));
var columnOverrideRepeater = element.all(by.repeater("i in columnsToOverride"));

//Create first selection
columnNameInput.sendKeys('Protractor Column Name');
columnDataTypeSelecter.sendKeys('double');
addColumnButton.click()
expect(columnOverrideRepeater.count()).toBe(1);

//Create second selection
columnNameInput.sendKeys('Protractor Column Name 2');
columnDataTypeSelecter.sendKeys('double');
addColumnButton.click()
expect(columnOverrideRepeater.count()).toBe(2);

//Attempt to delete second created column
var removeColumnButton1 = element(by.id('removeColumn1'));
removeColumnButton1.click();
//This is where I am having trouble. I can't confirm a confirm() dialogue here <-------
spyOn(window, 'confirm').and.returnValue(true);
expect(columnOverrideRepeater.count()).toBe(1);

var removeColumnButton0 = element(by.id('removeColumn0'));
removeColumnButton0.click();
expect(columnOverrideRepeater.count()).toBe(0);

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

你需要使用一个模拟框架 - 像sinon http://sinonjs.org/。要模拟确认功能 - 那么您可以确认在测试中调用了此功能。

我还建议使用$ window.confirm而不是仅仅确认,因为这会使模拟过程更简单。

这样的事情(警告:实际上没有运行,代码不在我的头顶)。

(function (angular, sinon) {
'use strict';

describe('namespace.someService', function () {
    var sandbox, $rootScope, someService, $window;

    beforeEach(module(my.module));

    describe('someService', function () {

        beforeEach(inject(function ($injector) {
            sandbox = sinon.sandbox.create();
            $rootScope = $injector.get('$rootScope');
            $window = $injector.get('$window');
            someService = $injector.get('someService');
        }));

        afterEach(function () {
            sandbox.restore();
        });

        describe('someMethod', function () {
           it('should notify the user via the alert API', function () {
                sandbox.stub($window, 'confirm').returns(true);

                var result = someService.someMethod();

               expect($window.confirm.calledOnce).to.equal(true);
           });
        });
    });
});
}(angular, sinon));