diff --git a/docs/commands.md b/docs/commands.md index efd79b6..f9a7cb7 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -134,6 +134,32 @@ Given a mockId return during addMock command, will remove the mock configuration // authorizationMock will not be active after this point and the test will proceed with normal flow ``` +### interceptor: removeAllMocks +Removes all registered mock configurations from the proxy server at once. + +#### Example: + +```javascript + await driver.execute("interceptor: addMock", { + config: { + url: "**/api/users/**", + statusCode: 400 + } + }); + + await driver.execute("interceptor: addMock", { + config: { + url: "**/api/login/**", + statusCode: 401 + } + }); + + // Perform actions under mock state... + + // Remove all mocks at once to return to normal flow + await driver.execute("interceptor: removeAllMocks"); +``` + ### interceptor: startListening Start listening for all network traffic (API calls) made by the device during a session diff --git a/src/plugin.ts b/src/plugin.ts index cfcfc7d..17805b7 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -37,6 +37,9 @@ export class AppiumInterceptorPlugin extends BasePlugin { command: 'removeMock', params: { required: ['id'] }, }, + 'interceptor: removeAllMocks': { + command: 'removeAllMocks', + }, 'interceptor: disableMock': { command: 'disableMock', params: { required: ['id'] }, @@ -178,6 +181,12 @@ export class AppiumInterceptorPlugin extends BasePlugin { proxy.removeMock(id); } + async removeAllMocks(_next: any, driver: any) { + const proxy = this.getSessionProxy(driver.sessionId); + log.debug(`[${driver.sessionId}] Removing all mock rules`); + proxy.removeAllMocks(); + } + async disableMock(_next: any, driver: any, id: string) { const proxy = this.getSessionProxy(driver.sessionId); log.debug(`[${driver.sessionId}] Disabling mock rule with ID: ${id}`); diff --git a/src/proxy.ts b/src/proxy.ts index 9613991..07d0e9f 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -201,6 +201,14 @@ export class Proxy { this.mocks.delete(id); } + public removeAllMocks(): void { + this.mocks.clear(); + } + + public getMockCount(): number { + return this.mocks.size; + } + public enableMock(id: string): void { this.mocks.get(id)?.setEnableStatus(true); } diff --git a/test/plugin.spec.js b/test/plugin.spec.js index f14e8ed..729cacc 100644 --- a/test/plugin.spec.js +++ b/test/plugin.spec.js @@ -127,6 +127,43 @@ describe('Plugin Test', () => { expect(page.includes('Error')).to.be.true; }); + it('Should be able to remove all mocks at once', async () => { + const mockId = await driver.execute('interceptor: addMock', { + config: { + url: '/api/users?.*', + responseBody: JSON.stringify({ + page: 1, + per_page: 1, + total: 1, + total_pages: 1, + data: [ + { + id: 1, + email: 'allmocksremoved.test@reqres.in', + first_name: 'Removed', + last_name: 'Mocks', + }, + ], + }), + }, + }); + expect(mockId).to.not.be.null; + + // Verify mock is active + const el1 = await driver.$('xpath://android.widget.TextView[@text="Get User List"]'); + await el1.click(); + let page = await driver.getPageSource(); + expect(page.includes('allmocksremoved.test@reqres.in')).to.be.true; + + // Now remove all mocks + await driver.execute('interceptor: removeAllMocks'); + + // Click again and verify mock is no longer applied + await el1.click(); + page = await driver.getPageSource(); + expect(page.includes('allmocksremoved.test@reqres.in')).to.be.false; + }); + afterEach(async () => { await driver.deleteSession(); });