使用量角器检查角度ui路线状态

Check angular ui route state using protractor

本文关键字:ui 状态 量角器 检查      更新时间:2023-09-26

我正在使用量角器框架来测试我的角度应用程序。

我可以在e2e测试中检查角度状态吗?

当然,这个想法是使用browser.executeAsyncScript()来:

  • 定位定义了ng-app的元素
  • 初始化angular.element并获取injector实例
  • 获取$state服务
  • 使用$state.current.name获取当前状态的名称

使用UI路由器演示页面的示例工作测试:

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

深受这个答案的启发。