在Protractor中添加Cookie以测试AngularJS

Adding Cookies in Protractor for testing AngularJS

本文关键字:测试 AngularJS Cookie 添加 Protractor      更新时间:2023-09-26

我在做一个非常简单的添加&在Angularjs的Protractor中获取cookie测试。这是测试块:

describe("Homepage", function () {
    var ptor;
    beforeEach(function () {
        ptor = protractor.getInstance();
        browser.get('/');
        ptor.manage().addCookie("test", "testValue");
    });

    it('should have cookie with name test and value textValue', function () {
        ptor.manage().getCookie("test").then(function(data){
            expect(data.value).toBe("testValue");
        });
    });
});

此测试失败并显示数据为空。如果我打印getCookies(),它会打印所有的cookie,但测试cookie不会在那里。非常感谢在这方面的帮助!谢谢

如果您在实际代码中使用angular$cookie来读取cookie值,那么在protoractor规范代码中使用ngCookies模块可能会更容易。以下规范代码适用于我。

describe('angularjs test', function() {
  it('should do something with cookie', function() {
    var mock_code = function () {
      angular.module('httpBackendMock', ['ngMockE2E','ngCookies'])
      .run(function ($httpBackend, $cookies) {
        $cookies.foo = 'bar';
      });
    };
    browser.addMockModule('httpBackendMock', mock_code);
    browser.get('/');
    // test code
  });
});

此问题的答案:https://github.com/angular/protractor/issues/341

感谢Julie的帮助!!