跳至主要內容

避免為排除的規格檔啟動會期

·閱讀時間 2 分鐘

使用標籤、grep 或任何其他技術來篩選規格檔是一種常見的方法,但我們之前在這裡有一個陷阱 – 每個規格檔都會建立一個新的會期,這需要一些時間,尤其是對於行動測試。

我們新增了一個功能,允許在會期啟動之前篩選規格檔。此功能預設僅為 Cucumber 框架啟用,為了避免破壞性變更,預設為 Mocha 和 Jasmine 框架停用。若要使用此功能,必須在 wdio.conf.js 中使用功能標記啟用,並且所有 browser 函式呼叫(例如 browser.addCommand() 或任何其他呼叫)都必須移出根範圍。您仍然可以像以前一樣使用環境標記、組態或功能。

以下僅適用於想要使用此功能的 Mocha 和 Jasmine 使用者

  • 使用 wdio.conf.js 中的標記啟用此功能
// wdio.conf.js
exports.config
// ...
featureFlags: {
specFiltering: true
},
}
  • 如果有的話,將自訂命令宣告移至 before hook,例如
// wdio.conf.js
exports.config
// ...
mochaOpts: {
/**
* all the files that interacts with `browser` object in a root scope
* have to be required in `before` hook if `specFiltering` feature is enabled.
*/
require: [
"@babel/register", // if you have any transpilers leave them as is
"./src/wdio/commands" // remove from here
]
},
before (capabilities, specs) {
require("./src/wdio/commands") // add here
},
}
  • 如果有的話,將自訂命令宣告從根範圍移至套件層級 (或將它們移至另一個檔案並在 before hook 中需要它,請參閱 2.1),例如
// my.spec.js

/**
* move `browser.addCommand()` as well as other browser functions calls
* from root scope to suite level (or another file)
*/
browser.addCommand('myCommand', () => {}) // remove!

// it's still possible to use config, capabilities or env flags as before.
describe('my suite in ' + browser.capabilities.browserName, () => {
// add it to suite/test scope
browser.addCommand('myCommand', () => {})

it('my test', () => {
browser.myCommand()
})
})

我們很樂意回答任何問題,並期待您的回饋。

請注意,此功能將在 v6 中為所有測試框架啟用,因此建議提前開始準備。

謝謝!

歡迎!我能幫上什麼忙?

WebdriverIO AI Copilot