1

I have a simple angular / requirejs / node project that loads correctly when viewed from a browser. I'm trying to get e2e tests with karma set up.

I've copied all of the e2e configurations and directory structures from the angular-require-js seed into my own project. Unfortunately, the tests in my own project give bizarre (and ever-changing!) results. Here's the stripped-down test I'm trying to run:

describe('My Application', function() {
    beforeEach(function() {
        browser().navigateTo('/');
        sleep(0.5);
    });

    it('shows an "Ask a Question" button on the index page', function() {

        expect(element('a').text()).toBe('Ask a Question');

    });
});

Sometimes the test fails

Executed 1 of 1 (1 FAILED) (0.785 secs / 0.614 secs)

Firefox 22.0 (Mac) My Application shows an "Ask a Question" button on the index page FAILED
element 'a' text
http://localhost:9876/base/test/lib/angular/angular-scenario.js?1375035800000:25397:     Selector a did not match any elements.

(but there ARE a elements on the page!)

Sometimes the test hangs

Executed 0 of 0! In these cases the test-runner browser does show that it's trying to run a test, but it never completes:

It just stays like this forever

It just stays like this forever. My app IS displayed in the browser during this hang.

Without element('a') it always passes

The only way to get consistent results is to avoid element(). If I expect(true).toBe(true) then 1 out of 1 tests always pass.

How can I debug this?

I'm at a loss for how to move forward. The test browser is correctly displaying my app, with the relevant 'a' element and everything. The test runner itself seems to only sometimes recognize that it should be running something and NEVER finds the a element. Is there a way to step through the test running process? Is this a common problem that happens when [x] is misconfigured?

Thanks for any suggestions!

karma-e2e.conf.js

basePath = '../';

files = [
  'test/lib/angular/angular-scenario.js',
  ANGULAR_SCENARIO_ADAPTER,
  'test/e2e/**/*.js'
];

autoWatch = false;

browsers = ['Firefox'];

singleRun = true;

proxies = {
  '/': 'http://localhost:3000/'
};

urlRoot = "__karma__";

junitReporter = {
  outputFile: 'test_out/e2e.xml',
  suite: 'e2e'
};
4

1 回答 1

0

页面上有多少个锚标签?

您可能没有引用您期望的实际锚点。将 id 标签添加到锚点并再次测试。如果它是页面中唯一的锚标记,请尝试匹配文本而不是期望它是。IE:

expect((element('#anchor-tag-id').text()).toMatch(/Ask a question/);

如果您使用 chrome 打开 a 元素上的开发工具来检查实际值,这可能会有很大帮助。

编辑:

应该

expect(element('#anchor-tag-id').text()).toMatch(/Ask a question/);

抱歉添加了额外的(在第一个示例中

于 2013-08-14T10:35:21.000 回答