# 📱 Capture Your Mobile Application’s Network Logs from Automation – Part 1

## **🤔 Why Are Network Logs a Game-Changer for Mobile Test Automation?**

Ever been in this situation?

✅ Your **automated mobile tests** run fine locally.  
✅ They pass on your machine.  
✅ But once they run on **CI/CD**, they mysteriously **fail**. 😱

Now you’re left scratching your head wondering:

* **Did the app fail?** 🤷
    
* **Did the API return an error?** 🧐
    
* **Did some background service just die silently?** 💀
    

Let’s face it—debugging test failures **without proper network logs** is like **finding a needle in a haystack**.

But what if you could **see every request and response your app sends and receives** in real time? 🎯

Welcome to **MITM Proxy**—the ultimate debugging tool that lets you capture and analyze your app’s **network traffic** like a pro! 🚀

This guide will help **automation engineers**, **testers**, and even **curious college students** set up **MITM Proxy** to **monitor, capture, and debug** mobile network traffic while running automation tests.

So, grab your coffee ☕, because we’re about to make debugging **100x easier**!

---

# **🎯 What Will We Cover?**

This is a **3-part series** to help you integrate **MITM Proxy** into your mobile automation framework:

✅ **Part 1 (This Article)** – Set up **MITM Proxy** with an **Android/iOS** device.  
✅ **Part 2** – Capture and **save real-time network logs** for deeper analysis.  
✅ **Part 3** – Add **automation-specific network logs** to your **HTML test reports**.

By the end of this series, you’ll be **a debugging ninja**. 🥷🔥

---

# **🔍 What is MITM Proxy? And Why Should You Care?**

💡 **MITM Proxy** stands for **Man-in-the-Middle Proxy**, a powerful tool that allows you to:

✅ **Intercept** all network traffic from your mobile app.  
✅ **Inspect** HTTP & HTTPS requests **in real time**.  
✅ **Modify API responses** for testing different scenarios.  
✅ **Replay previous API calls** without re-running the entire test.

### **Why Are We Using MITM Proxy Instead of Other Tools?**

| Feature | MITM Proxy | Charles Proxy | Fiddler | Wireshark |
| --- | --- | --- | --- | --- |
| **Intercepts Requests in Real-Time** | ✅ | ✅ | ✅ | ❌ |
| **Works for Mobile Apps (Android/iOS)** | ✅ | ✅ | ✅ | ❌ |
| **Modify API Responses Dynamically** | ✅ | ❌ | ❌ | ❌ |
| **Command-Line Friendly (Great for CI/CD)** | ✅ | ❌ | ❌ | ❌ |
| **Lightweight & Open Source** | ✅ | ❌ (Paid) | ❌ (Paid) | ✅ |

MITM Proxy is like **Wireshark, but built for automation engineers**! 🛠

# **🛠 Setting Up MITM Proxy with an Android/iOS Device**

## **Step 1: Install MITM Proxy**

If you’re on **Mac**, install it using **Homebrew**

```plaintext
brew install mitmproxy
```

For **Windows/Linux**, check the [official installation guide](https://docs.mitmproxy.org/stable/overview-installation/#installation).

---

## [**Step 2: Start the MITM Proxy**](https://docs.mitmproxy.org/stable/overview-installation/#installation)\*\* Server\*\*

Now, let’s fire up [**MITM Proxy** so it can start](https://docs.mitmproxy.org/stable/overview-installation/#installation) listening [for network requests:](https://docs.mitmproxy.org/stable/overview-installation/#installation)

```bash
mitmdump --listen-port 8090
```

This starts the proxy on **port 8090** and captures **ALL** traffic.

To capture traffic **only for a specific domain**, run:

```bash
mitmdump --listen-port 8090 "example.com"
```

You can also use `mitmproxy` instead of `mitmdump` if you prefer an **interactive UI**.

### **Step 3: Configure Proxy Settings on Your Mobile Device**

Your phone needs to **route all network traffic** through MITM Proxy.

### **🔎 Find Your Private IP Address**

We need the IP address of the machine running MITM Proxy.

* **Mac/Linux**:
    
    ```bash
    ifconfig | grep -i mask
    ```
    
    Look for an IP that starts with **192.168.x.x**.
    
* **Windows**:  
    Open **CMD** and type:
    
    ```bash
    ipconfig
    ```
    
    Find your **IPv4 Address** under your active network connection.
    

---

### **📱 Configure Proxy in Mobile Network Settings**

#### **For Real Devices:**

1️⃣ Connect your phone to the **same WiFi network** as your laptop/PC.  
2️⃣ Open **WiFi Settings** → Tap on your **current network**. (Connect your device to the same network as the device running proxy server)  
3️⃣ Find **"Proxy Settings"** and set:

* **Proxy Type**: Manual
    
* **Proxy Host**: Your **private IP** (from previous step)
    
* **Proxy Port**: `8090`  
    4️⃣ Save and exit.
    

#### Connect your device to the same network as the proxy device

* Open Network Settings -&gt; Goto Wifi
    
* Open Network Details
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680345964213/da6ff899-2c71-43dc-87f8-be88a08c4bf6.png align="left")
    
    Edit connection and configure proxy settings, make sure to put the correct private IP Address and port
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680346461599/31c2ea94-a0e2-44dd-816b-5d10c2180cdb.png align="center")
    
    #### **For Emulators:**
    
    If you’re using an emulator, start it with:
    
    ```bash
    emulator -avd <EMULATOR_NAME> -http-proxy http://<YOUR_PRIVATE_IP>:8090
    ```
    
    🚀 **Now your device/emulator is sending network traffic through MITM Proxy!**
    
    ---
    
    ## **Step 5: Intercept HTTPS Traffic (Important for Most Apps!)**
    
    🚨 **By default, MITM Proxy only captures HTTP traffic.** But most modern apps use **HTTPS**.
    
    To capture **secure** API requests, we need to install the **MITM CA Certificate**.
    
    ### **📥 Install the MITM CA Certificate**
    
    #### **For Android:**
    
    1️⃣ Open your mobile browser and go to [**mitm.it**.  
    ](http://mitm.it)2️⃣ Download the **MITM CA Certificate**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680346656070/1228abbf-c09a-4d4e-bd09-49fc08c0eee3.png align="left")

3️⃣ Install it via [mitm.it](http://mitm.it):

```bash
Settings → Security → Encryption & Credentials → Install a Certificate → CA Certificate
```

4️⃣ Select **VPN and Apps** → Tap OK.

#### **For iOS:**

1️⃣ Visit [**mitm.it**](http://mitm.it) in Safari.  
2️⃣ [Downlo](http://mitm.it)ad the **CA Certificate**.  
3️⃣ Go to **Settings → General → Profile → Install Certificate**.  
4️⃣ Enable **Full Trust** under **Settings → General → About → Certificate Trust** [**Settings**.](http://Settings.You)

## **Step 6: Make Your App Respect the Certificate (For Debugging Only!)**

Android apps **ignore user-installed certificates** by default. If you're debugging, update `network_security_config.xml`:

```bash
<network-security-config xmlns:tools="http://schemas.android.com/tools">
    <debug-overrides>
        <trust-anchors>
            <certificates src="system" />
            <certificates src="user" />
        </trust-anchors>
    </debug-overrides>
</network-security-config>
```

🚨 **Warning:** NEVER do this in a production app!

---

# **🎯 What’s Next?**

✅ **MITM Proxy is set up!**  
✅ **Your device is connected!**  
✅ **You can now capture mobile API traffic!** 🎉

👉 In **Part 2**, we’ll:  
✅ **Save intercepted requests to a log file**.  
✅ **Filter traffic based on API endpoints**.  
✅ **Use captured logs for debugging and automation reports**.

**Stay tuned!** 🚀

💬 **Questions? Thoughts? Drop a comment!**

🔜 **Coming up in Part 2:** Saving logs & integrating with Appium! 🎯
