2013-12-12 43 views
1

我很難做一個非常簡單的添加&在Protractor for Angularjs中獲取Cookie測試。這是測試塊:在量角器中添加餅乾以進行測試AngularJS

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不會在那裏。真的很感謝這個幫助!謝謝!

回答

4

如果您在實際代碼中使用角度$ cookie讀取cookie值,那麼在protoractor spec代碼中使用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 

    }); 
}); 
+0

這解決了我的問題,在一個捷飛! – Chris