CleanupTotal 服務
透過用於 webdriver.io 的 cleanup-total
服務,您可以輕鬆確保每次測試後都進行適當的清理。此服務提供一種系統化的方式,可在建立後立即標記要刪除的實體。當測試涉及建立複雜結構時,例如具有投資計畫和存款的銀行帳戶,這特別有用。如果沒有適當的清理,嘗試刪除帳戶可能會導致錯誤,例如由於帳戶不為空而拒絕刪除。然而,透過 cleanup-total,實體會以正確的順序刪除,確保測試在執行後會自行清理,並且不會互相干擾。
安裝
安裝此模組作為(開發)相依性的最簡單方法是使用以下命令npm install wdio-cleanuptotal-service --save-dev
用法
將 wdio-cleanuptotal-service 新增至您的 wdio.conf.js
exports.config = {
// ... other options
services: ['cleanuptotal']
// ... other options
};
或使用服務選項
exports.config = {
// ... other options
services: [
[
'cleanuptotal',
{
// Use a custom logger function to write messages to the test report
customLoggerMethod: console.log(), // TODO: replace with your own logger function if needed
// Only write to the log when an error occurs to reduce clutter
logErrorsOnly: false, // TODO: consider changing to 'true' if you have too many messages in the report
}
]
]
// ... other options
};
在測試中使用
您可以根據需要在任何地方導入 cleanuptotal 服務,無論是在您的測試檔案中還是任何其他類別中。
import { cleanuptotal } from "wdio-cleanuptotal-service";
it("should keep things tidy", () => {
// ...
// Create an account and add it to the cleanup list for deletion after the test
const accountId = createAccount("John Blow");
cleanupTotal.addCleanup(async () => {
await deleteAccount(accountId);
});
// Add an investment plan to the account and add it to the cleanup list
addInvestmentPlan(accountId, "ModRisk");
cleanupTotal.addCleanup(async () => {
await removeInvestmentPlan(accountId);
});
// Deposit funds into the account and add it to the cleanup list
deposit(accountId, 1000000);
cleanupTotal.addCleanup(async () => {
await undoDeposit(accountId);
});
// ...
});
// Note that the actual cleanup code will be executed after the test is complete
Typescript 支援
此外掛程式支援 Typescript。