【问题标题】:Angular2 + Protractor test running with no resultAngular2 + Protractor 测试运行没有结果
【发布时间】:2017-06-08 18:10:56
【问题描述】:

我正在为一个 Angular 2 应用程序开始简单的量角器测试,如下所示:

    import { browser, element, by } from 'protractor';

    describe('My Page', function() {
      it('should load news block', () => {
        browser.get('/my/page');

        element(by.id('news')).getText().then(function(text) {
          expect(text.length > 0).toBeTruthy();
        });
    });
});

结果系统运行浏览器,转到所需的页面,但我看不到结果 - 如果测试成功执行。在控制台中,我总是看到“规范已启动”消息。

我做错了什么?

量角器配置文件:

exports.config = {
    allScriptsTimeout: 2500000,
    specs: [
        './e2e/**/*.e2e-spec.ts'
    ],
    capabilities: {
        'browserName': 'chrome'
    },
    directConnect: true,
    baseUrl: 'http://localhost:4200/',
    framework: 'jasmine',
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 2500000,
        print: function() {}
    },
    useAllAngular2AppRoots: true,
    beforeLaunch: function() {
        require('ts-node').register({
            project: 'e2e'
        });
    },
    onPrepare: function() {
        jasmine.getEnv().addReporter(new SpecReporter());
    }
};

【问题讨论】:

  • 请提供您的量角器配置文件 ;-)
  • 添加了配置文件.. 注意 - 如果我检查 expect(true).toBe(true),一切正常且快速

标签: angular protractor


【解决方案1】:

我认为它应该记录一段通过测试的时间;但是,由于您使用的是SpecReporter,因此这可能会覆盖默认报告器以进行控制台。有两种调试方法:

  1. 您可以使用酷炫的新 BlockingProxy 功能。此功能应在获取文本之前突出显示元素。你可以在这里阅读如何做:how to debug the protractor files?

  2. 我相信在某些情况下 BlockingProxy 不会突出显示该元素。如果是这种情况,您可以尝试进行一些调试:


(更新答案以获取更多日志记录):

  console.log('we are going to find the news id');
  element(by.id('news')).getText().then(text => {
    // If we do have text, let's log it so we know we got to this block of code.
    console.log('Pass! We have text: ' + text);
    expect(text.length > 0).toBeTruthy();
  }).catch(err => {
    // Else we could not fid the 'news' id, so we should log an error.
    console.log('The id for news could not be found.);
    console.log(err);
  });

【讨论】:

  • 我尝试使用 console.log() 进行调试。但是当我按 F12 打开浏览器控制台时,我的量角器测试因此错误而关闭 - prntscr.com/fhotaj
  • console.log 应该打印到运行测试的区域。因此,在它显示Running 1 instance 之后,您应该能够看到一些日志记录。我更新了答案以增加一行日志记录。如果您在窗口中没有看到这一行,则说明有其他问题。
  • 所以,当我在量角器测试中执行 console.log 时,它应该在 Windows 控制台中打印消息,而不是在浏览器控制台中?
  • 是的,这就是它应该做的。我还将注释掉/删除量角器配置文件中的 onPrepare 插件。另外,最后一个问题。它是否真的导航到您的“/my/page”?
  • 刚刚执行了您的代码,至少可以看到消息“我们将找到新闻 id”。但没有别的:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多