1

I'm using protractor framework to test my angular application.

Can I check for the angular state in the e2e test?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Dana Zelniker
  • 19
  • 1
  • 2

1 Answers1

4

Sure, the idea is to use browser.executeAsyncScript() to:

Sample working test using the UI router demo page:

describe("Current Angular UI router state", function () {
    beforeEach(function () {
        browser.get("https://angular-ui.github.io/ui-router/sample/#/");
    });

    it("should get the current state", function (){
        var currentStateName = browser.executeAsyncScript(function(callback) {
            var el = document.querySelector("html");  // ng-app is defined on html element in this case
            var injector = angular.element(el).injector();
            var service = injector.get('$state');

            callback(service.current.name);
        });

        expect(currentStateName).toEqual("home");
    });
});

Heavily inspired by this answer.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195