Skip to content

Commit 9400c11

Browse files
authored
fix: ability to manually start and stop proxy (#86)
- Add new commands startProxy + stopProxy - Fix duplicate imports of ./logger - Refactor plugin.ts - Update readme + docs/commands
1 parent de8cb8b commit 9400c11

3 files changed

Lines changed: 274 additions & 166 deletions

File tree

README.md

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# appium-interceptor-plugin
22

33
This is an Appium plugin designed to intercept API response and mocking easy.
4-
This plugin uses mitmproxy
4+
This plugin uses `mitmproxy`.
55

66
## Prerequisite
77

88
1. Appium version 3.0
99
2. Intercepting API requests from android requires CA certificate to be installed on the device. Follow the instructions in [How to install CA certificate on android](./docs/certificate-installation.md) section and install the CA certificate.
1010

11-
## Installation - Server
11+
## Installation - server
1212

1313
Install the plugin using Appium's plugin CLI, either as a named plugin or via NPM:
1414

@@ -22,15 +22,25 @@ The plugin will not be active unless turned on when invoking the Appium server:
2222

2323
`appium server -ka 800 --use-plugins=appium-interceptor -pa /wd/hub`
2424

25-
## Custom certificate
25+
## What does this plugin do?
26+
27+
The **Appium Interceptor Plugin** provides network interception and mocking capabilities specifically for **Android** devices. It manages a local proxy server and automatically configures the device's WiFi settings to route all network traffic through it.
28+
29+
By using this plugin, you can intercept, record, and mock HTTP requests directly within your Appium tests without manually configuring certificates or proxy settings on the device.
30+
31+
## Configuration
32+
33+
### Server Arguments
34+
35+
#### 👉 Custom certificate
2636

2737
If you need to use a custom certificate, it can be done by passing `certdirectory` as an argument of the plugin:
2838

29-
`appium server -ka 800 --use-plugins=appium-interceptor --plugin-appium-interceptor-certdirectory="<YOUR DIRECTORY>" -pa /wd/hub`
39+
`appium server -ka 800 --use-plugins=appium-interceptor --plugin-appium-interceptor-certdirectory="<path_to_cert_directory>" -pa /wd/hub`
3040

3141
Please keep the same directory structure as the existing certificate folder.
3242

33-
## Whitelist/Blacklist
43+
#### 👉 Whitelist/Blacklist
3444

3545
If you need to limit the calls going through the proxy, it can be done by passing `whitelisteddomains` and `blacklisteddomains` as an argument of the plugin:
3646

@@ -44,12 +54,61 @@ Example of `blacklisteddomains`: all the calls go through the proxy, except the
4454

4555
Note: `whitelisteddomains` and `blacklisteddomains` are two different approach and are not supposed to be used together. If both are present, `blacklisteddomains` will be ignored.
4656

47-
## what does this plugin do?
57+
### Capabilities
58+
59+
To control the plugin behavior, you can use the following capabilities:
4860

49-
For every appium session, interceptor plugin will start a proxy server and updates the device proxy settings to pass all network traffic to proxy server. Mocking is disabled by default and can be enabled from the test by passing `appium:intercept : true` in the desired capability while creating a new appium session.
61+
| Capability | Type | Default | Description |
62+
| :------------------------------- | :-------- | :------ | :----------------------------------------------------------------------------------------------------------------------- |
63+
| `appium:startProxyAutomatically` | `boolean` | `false` | When `true`, the plugin initializes the proxy server and configures the device WiFi immediately during session creation. |
64+
65+
---
66+
67+
## Usages
68+
69+
### 1. Automatic lifecycle management
70+
71+
Set `appium:startProxyAutomatically` to `true` in your capabilities. The plugin will handle the proxy **setup** during session creation and the proxy **cleanup** (reverting WiFi settings and closing the server) when the session ends.
72+
73+
```javascript
74+
// Example with WebdriverIO
75+
const caps = {
76+
"platformName": "Android",
77+
"appium:automationName": "UiAutomator2",
78+
"appium:startProxyAutomatically": true
79+
};
80+
```
81+
82+
### 2. Manual management
83+
84+
If you want to control exactly when the proxy starts (e.g., only for specific test cases), leave the capability at `false` and use the following commands within your test scripts:
85+
86+
* **Start Proxy**: `driver.execute('interceptor: startProxy')`
87+
* **Stop Proxy**: `driver.execute('interceptor: stopProxy')`
88+
89+
> **Pro tip for troubleshooting**: These commands can be useful for **on-the-fly recovery** during a test session. If you encounter a network glitch or a proxy timeout, you can manually call `stopProxy` followed by `startProxy` to perform a "clean restart" without terminating your entire Appium session.
90+
91+
> **Note**: Even in manual mode, the plugin **automatically handles the cleanup** when the session ends.
92+
> Unlike a simple deactivation, the plugin **restores your previous device settings** (such as your original global proxy configuration) instead of just wiping them. This ensures your device returns exactly to the state it was in before the test started.
93+
94+
## Usable commands
5095

5196
Please refer to the [commands](/docs/commands.md) sections for detailed usage.
5297

98+
99+
## Logging & debugging
100+
101+
The plugin integrates with the standard Appium logging system. For deep troubleshooting, set the server log level to `debug`:
102+
103+
```json
104+
{
105+
"server": {
106+
"log-level": "debug:debug"
107+
}
108+
}
109+
```
110+
111+
53112
## Supported Platforms
54113

55114
💚 `Android`

docs/commands.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
## Appium interceptor commands
22

3-
Create the appium session by passing `appium:intercept : true` option in the desired capability. Once the session is successfully created, tests can manage the api mocking using below commands.
3+
To enable network interception, configure your Appium session using the `appium:startProxyAutomatically` capability. Depending on your configuration, you can manage the proxy and mocking as follows:
4+
5+
🔸 ***Automatic Mode***: Set `appium:startProxyAutomatically` to `true` in your desired capabilities. The plugin will immediately initialize the proxy and configure the device upon session start.
6+
7+
🔸 ***Manual Mode***: If the capability is set to false (default), you must explicitly trigger the proxy initialization using the `startProxy` command during your test.
8+
9+
👉 Once the proxy is successfully started (either automatically or manually), you can manage API mocking, recording, and sniffing using the commands detailed below.
410

511
To route emulator traffic through another proxy, set one of the environment variables UPSTREAM_PROXY, HTTPS_PROXY, or HTTP_PROXY. All traffic from the emulator will then be forwarded to the specified upstream proxy.
612

@@ -38,16 +44,34 @@ Mock configuration is a json object that defines the specification for filtering
3844

3945
## Commands:
4046

41-
### interceptor: addMock
47+
### interceptor: startProxy / stopProxy
4248

43-
Add a new mock specification for intercepting and updating the request. The command will returns a unique id for each mock which can be used in future to delete the mock at any point in the test.
49+
These commands allow you to manually manage the proxy lifecycle during a test session. This is the preferred method when `appium:startProxyAutomatically` is set to `false`, or if you need to reset the network configuration on-the-fly.
4450

45-
#### Example:
51+
> ***Note:*** Even when using these manual commands, the plugin provides **smart cleanup**: it will automatically stop the proxy and restore your previous device settings when the session ends or crashes.
4652
53+
#### Example:
4754

4855
***Note:*** Below example uses wedriver.io javascript client. For Java client you need to use `((JavascriptExecutor) driver).executeScript()` for executing commands instead of `driver.execute()`
4956

57+
```javascript
58+
// Manually starting the proxy
59+
await driver.execute("interceptor: startProxy");
60+
61+
// ... perform your intercepted tests ...
5062

63+
// Manually stopping the proxy
64+
// This will revert your device WiFi settings to their original state
65+
await driver.execute("interceptor: stopProxy");
66+
```
67+
68+
### interceptor: addMock
69+
70+
Add a new mock specification for intercepting and updating the request. The command will returns a unique id for each mock which can be used in future to delete the mock at any point in the test.
71+
72+
#### Example:
73+
74+
***Note:*** Below example uses wedriver.io javascript client. For Java client you need to use `((JavascriptExecutor) driver).executeScript()` for executing commands instead of `driver.execute()`
5175

5276
```javascript
5377
const authorizationMock = await driver.execute("interceptor: addMock", {

0 commit comments

Comments
 (0)