3

我在 TestCafe 中有以下测试:

import { Selector } from 'testcafe';

const TEST_URL = process.env.TEST_URL;

fixture('/').page(`${TEST_URL}/`);

test(`users should be able to view the '/' page`, async (t) => {
    await t
        .navigateTo(TEST_URL)
        .expect(Selector('H1').withText('All Users').exists).ok()
});

这个测试失败了:

/
 ✖ users should be able to view the '/' page

   1) AssertionError: expected false to be truthy

      Browser: Chrome 59.0.3071 / Mac OS X 10.12.5

          5 |fixture('/').page(`${TEST_URL}/`);
          6 |
          7 |test(`users should be able to view the '/' page`, async (t) => {
          8 |    await t
          9 |        .navigateTo(TEST_URL)
       > 10 |        .expect(Selector('H1').withText('All Users').exists).ok()
         11 |});
         12 |

问题是页面没有完全加载并且没有 h1 标签(从 React 组件呈现)但我不知道如何告诉 TestCafe 等待页面加载。

我试过这个:

import { Selector } from 'testcafe';

const TEST_URL = process.env.TEST_URL;

fixture('/').page(`${TEST_URL}/`);

test(`users should be able to view the '/' page`, async (t) => {
    await t.expect(Selector('H1').withText('All Users').exists).ok()
});

哪个有效,但我需要一些测试才能使用 navigateTo 所以我想知道要更改什么,以便使用 navigateTo 的测试版本也可以工作。

我试图为这样的每一行设置等待:

import { Selector } from 'testcafe';

const TEST_URL = process.env.TEST_URL;

fixture('/').page(`${TEST_URL}/`);

test(`users should be able to view the '/' page`, async (t) => {
    await t.navigateTo(TEST_URL)
    await t.expect(Selector('H1').withText('All Users').exists).ok()
});

但那个错误是一样的。

4

1 回答 1

4

问题出在TEST_URL变量中。如果它包含裸 IP 地址(例如,'xxx.xx.x.xxx'),navigateTo则将浏览器重定向到,'http://xxx.xx.x.xxx/xxx.xx.x.xxx'因为该navigateTo操作将此字符串视为相对路径。

要解决此问题,请在TEST_URL字符串中包含协议,例如'http://xxx.xx.x.xxx'.

于 2017-07-03T11:05:51.623 回答