WireMock 服務
此服務可協助您在使用 WebdriverIO 執行測試時,順暢地執行 WireMock。它使用廣為人知的 Maven 儲存庫為您下載 WireMock jar,然後自動安裝、啟動和停止。若要獲得協助和支援,請加入 Gitter 的社群,隨時掌握最新資訊。
安裝
npm i -D wdio-wiremock-service
關於如何安裝 WebdriverIO
的說明,請參閱此處。
使用方式
在根目錄(預設為 ./mock
)中,您會找到兩個子目錄,__files
和 mappings
,它們分別用於您的夾具和模擬。
如需更多資訊,請查看 WireMock 的官方文件。
設定
為了將此服務與 wdio 測試執行器搭配使用,您需要將其新增至您的服務陣列中
// wdio.conf.js
export.config = {
// ...
services: ['wiremock'],
// ...
};
當使用 webdriverio 獨立版本時,您需要新增服務並手動觸發 onPrepare
和 onComplete
鉤子。您可以在 這裡找到範例(此範例使用 Jest)
選項
下列選項可以新增至服務中。
port
WireMock 應該在其上執行的埠。
類型:Number
預設值:8080
範例
// wdio.conf.js
export const config = {
// ...
services: [['wiremock', { port: 8181 }]],
// ...
};
rootDir
WireMock 將尋找檔案的路徑。
類型:String
預設值:./mock
範例
// wdio.conf.js
export const config = {
// ...
services: [['wiremock', { rootDir: './mock' }]],
// ...
};
version
要下載和使用的 WireMock 版本。
類型:String
預設值:3.3.1
範例
// wdio.conf.js
export const config = {
// ...
services: [['wiremock', { version: '2.25.1' }]],
// ...
};
skipWiremockInstall
告知服務略過下載 WireMock。
類型:Boolean
預設值:false
範例
// wdio.conf.js
export const config = {
// ...
services: [['wiremock', { skipWiremockInstall: true }]],
// ...
};
binPath
本機 Wiremock 二進位檔的自訂路徑(通常與 skipWiremockInstall 搭配使用)。
類型:String
預設值:'./wiremock-standalone-3.0.0.jar'(相對於服務)
範例
// wdio.conf.js
export const config = {
// ...
services: [['wiremock', { binPath: './my-custom/example-path/wiremock-standalone-3.0.0.jar' }]],
// ...
};
silent
用於記錄 WireMock 輸出的靜音模式(包括來自服務本身的額外記錄)。
類型:Boolean
預設值:false
範例
// wdio.conf.js
export const config = {
// ...
services: [['wiremock', { silent: true }]],
// ...
};
mavenBaseUrl
Maven 的基本下載網址。
類型:String
預設值:https://repo1.maven.org/maven2
範例
// wdio.conf.js
export const config = {
// ...
services: [['wiremock', { mavenBaseUrl: 'https://repo1.maven.org/maven2' }]],
// ...
};
args
您可以在其中傳遞所有支援引數以設定 WireMock 的清單
注意:您不能在這裡傳遞選項(port
、rootDir
、stdio
、mavenBaseUrl
),因為它們會被忽略。
類型:Array
範例
// wdio.conf.js
export const config = {
// ...
services: [
[
'wiremock',
{
args: ['--verbose', '--match-headers'],
},
],
],
// ...
};
撰寫測試
撰寫您的第一個測試真的非常簡單
使用 WDIO 測試執行器
import fetch from 'node-fetch'; // you can use any HTTP client you like
import { equal } from 'node:assert'; // you can use any assertion library you like
describe('example', () => {
it(`should assert the mock data`, async () => {
const body = await fetch('https://127.0.0.1:8080/api/mytest');
equal(body.text(), 'Hello world!');
});
});
使用 WebdriverIO 獨立版本
import fetch from 'node-fetch'; // you can use any HTTP client you like
import { equal } from 'node:assert'; // you can use any assertion library you like
import { remote } from 'webdriverio';
import { launcher } from 'wdio-wiremock-service';
const WDIO_OPTIONS = {
capabilities: {
browserName: 'chrome',
},
};
describe('example', () => {
let wiremockLauncher;
let client;
beforeAll(async () => {
wiremockLauncher = new launcher(); // create instance of the service
await wiremockLauncher.onPrepare(WDIO_OPTIONS); // run the onPrepare hook
client = await remote(WDIO_OPTIONS);
});
afterAll(async () => {
await client.deleteSession();
await wiremockLauncher.onComplete(); // run the onComplete hook
});
test('should showoff a mocked api response', async () => {
const body = await fetch('https://127.0.0.1:8080/api/mytest');
equal(body.text(), 'Hello world!');
});
});
如需更多關於 WebdriverIO 的資訊,請參閱首頁。