使用量角器检查角度 ui 路由状态 [英] Check angular ui route state using protractor

查看:38
本文介绍了使用量角器检查角度 ui 路由状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

当然,想法是使用 browser.executeAsyncScript() 到:

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

  • locate the element having the ng-app defined
  • initialize the angular.element and get the injector instance
  • get the $state service
  • use $state.current.name to get the current state's name

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

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

深受这个答案的启发.

这篇关于使用量角器检查角度 ui 路由状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆