作為測驗運行程式初始化的一部分,我正在啟動一個 Web 服務器。服務器選擇任何空閑埠,因此埠號在多次測驗運行中不是恒定的。如何在我的測驗中訪問埠號?
我的 testcafe 配置
module.exports = {
hooks: {
testRun: {
before: async ctx => {
let appServerPort = await startLocalServer();
// ???
}
}
}
}
一個測驗
test('my test', async (t) => {
await t.expect(true).ok();
}).page(`localhost:${ ??? }`);
uj5u.com熱心網友回復:
您至少有兩種方法可以做到這一點:
- 您可以創建一個全域變數并將服務器埠設定為它。之后,您可以在需要的任何地方使用此變數。
- 將埠設定為
ctx,從 獲取它t.testRun.testRunCtx,然后使用 導航到所需的頁面t.navigateTo。例如:
配置
module.exports = {
hooks: {
testRun: {
before: async ctx => {
const appServerPort = await startLocalServer();
ctx.port = appServerPort;
}
}
}
}
測驗
test('my test', async (t) => {
await t.navigateTo(\`localhost:${ t.testRun.testRunCtx.port }\`)
await t.expect(true).ok();
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507617.html
