【发布时间】:2019-04-11 19:40:01
【问题描述】:
使用TestCafe [1],我想做以下事情:
- 导航到一个 URL。
- 执行客户端 JavaScript 以检索其他 URL 列表。
- 为每个 URL 生成测试。
- 同时执行测试。
使用单个测试并串行执行的工作示例
下面的简化测试用例有效,但不是我所需要的,它执行以下操作:
- 执行测试。
- 导航到测试“之前”挂钩中的 URL。
- 执行客户端 JavaScript 以检索测试的“之前”挂钩中的 URL 列表。
- 串行在测试正文中为每个 URL 运行断言。
我通过 TestCafe 运行的测试文件包含以下内容:
import { ClientFunction, Selector } from 'testcafe';
const getSiteMapFromClientSide = ClientFunction(
() =>
new Promise(resolve => {
// this example looks like it could be synchronous, or that the data could
// be defined in the test but this data comes from an async source which
// is only reachable client side.
resolve(['https://localhost:6007/some/url1', 'https://localhost:6007/some/url2']);
})
);
fixture('Storybook').page('https://localhost:6007');
const modalError = Selector('#error-message');
test.before(async t => {
t.ctx.siteMap = await getSiteMapFromClientSide();
})('Smoke Test all Stories for Full Screen Errors or Blank Screens', async t => {
// assert we have URLs or we'll have a passing suite that looped over nothing
await t.expect(t.ctx.siteMap.length > 0).ok('Site Map is Empty');
// assert each story has no full screen error message
await Promise.all(
t.ctx.allStories.map(async url => {
await t
.navigateTo(url)
.expect(modalError.exists && modalError.visible)
.notOk(`Full Screen Error found at ${url}`);
})
);
});
上下文
在实际应用中,我正在为 Storybook [2] 编写烟雾测试。要获取所有 URL 的列表,我需要调用结果
require('@storybook/react').getStorybook(),仅在运行时在 Storybook 客户端应用程序中可用。
我已将这些细节从简化的测试用例中删除,因为它们与 TestCafe 无关。
[1] http://devexpress.github.io/testcafe [2] https://storybook.js.org
【问题讨论】:
标签: testing automated-tests e2e-testing testcafe storybook