最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding Cookies in Protractor for testing AngularJS - Stack Overflow

programmeradmin0浏览0评论

I'm having trouble doing a very simple adding & getting cookie test in Protractor for Angularjs. This is the test block:

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

    });
});

This test fails and says data is null. If I print getCookies() it'll print all the cookies but the test cookie will not be in there. Would really appreciate some help on this! Thanks!

I'm having trouble doing a very simple adding & getting cookie test in Protractor for Angularjs. This is the test block:

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

    });
});

This test fails and says data is null. If I print getCookies() it'll print all the cookies but the test cookie will not be in there. Would really appreciate some help on this! Thanks!

Share Improve this question asked Dec 12, 2013 at 19:53 wlingkewlingke 4,7994 gold badges37 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

if you use angular $cookies in your actual code to read cookie value, using ngCookies module inside protoractor spec code might be easier. The following spec code works for me.

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

  });
});

Answer to this question: https://github./angular/protractor/issues/341

Thanks to Julie for her help!!

发布评论

评论列表(0)

  1. 暂无评论