|
| 1 | +--- |
| 2 | +id: network-capture |
| 3 | +title: Network Traffic Capture |
| 4 | +--- |
| 5 | + |
| 6 | +import useBaseUrl from '@docusaurus/useBaseUrl'; |
| 7 | +import Tabs from '@theme/Tabs'; |
| 8 | +import TabItem from '@theme/TabItem'; |
| 9 | + |
| 10 | +Network Traffic Capture is a functionality that gives you the ability to record network traffic for HTTP/HTTPS requests during Android and iOS/iPadOS mobile app tests on real devices. This provides granular insight into your app’s behavior, facilitates debugging, and helps you determine which development team is responsible for application failure. |
| 11 | + |
| 12 | +With this feature, traffic is recorded on our side and exposes the HAR file through your test’s endpoint, producing HTTP traffic information to help you troubleshoot. |
| 13 | + |
| 14 | +## What You'll Need |
| 15 | + |
| 16 | +- A Sauce Labs account ([Log in](https://accounts.saucelabs.com/am/XUI/#login/) or sign up for a [free trial license](https://saucelabs.com/sign-up)) |
| 17 | +- A native Android, iOS, or iPadOS mobile app that makes HTTP/HTTPS requests. |
| 18 | + |
| 19 | +## Using Network Traffic Capture |
| 20 | + |
| 21 | +To enable network traffic capturing in your tests: |
| 22 | + |
| 23 | +1. On Sauce Labs, click **Live** > **Mobile App**. |
| 24 | +2. Upload your mobile app to Sauce Labs [through our UI](/mobile-apps/app-storage/#uploading-apps-via-ui) or [our REST API](/mobile-apps/app-storage/#uploading-apps-via-rest-api). |
| 25 | +3. After you’ve uploaded your app, return to the **Live** > **Mobile App** page, hover your mouse over your app, then select **Settings**.<br/><img src={useBaseUrl('img/mobile-apps/networkcapturescr.png')} alt="Mobile app settings navigation" width="600"/> |
| 26 | +4. Under **Default Settings**, toggle Instrumentation and Network Capture to enable the feature.<br/><img src={useBaseUrl('img/mobile-apps/networkcapturescr2.png')} alt="Mobile app settings navigation" width="800"/> |
| 27 | +5. For Automated Testing only: add the networkCapture capability to your test script. Click the link below corresponding to your framework: |
| 28 | + |
| 29 | + - [Appium](/dev/test-configuration-options/#saucelabsnetworkcaptureenabled) |
| 30 | + - [Espresso via saucectl](/mobile-apps/automated-testing/espresso-xcuitest/espresso/#networkcapture) |
| 31 | + - [XCUITest via saucectl](/mobile-apps/automated-testing/espresso-xcuitest/xcuitest/#networkcapture) |
| 32 | + |
| 33 | +Now you can start your live or automated testing session. Your network logs will be captured and displayed in the test results page as well as During a Live Testing session with the Developer Options window. |
| 34 | + |
| 35 | +## Accessing Network Traffic in real-time (Live Testing) |
| 36 | + |
| 37 | +To be able to observe network traffic in real-time: |
| 38 | + |
| 39 | +1. Navigate to **Live** -> **Mobile App**/**Cross Browser** |
| 40 | +2. Choose device and start a session |
| 41 | +3. Launch the network inspector |
| 42 | + |
| 43 | +<img src={useBaseUrl('img/mobile-apps/network-capture-1.png')} alt="Select Developer Options" width="300"/> |
| 44 | + |
| 45 | + * Start the capture. |
| 46 | + |
| 47 | +<img src={useBaseUrl('img/mobile-apps/network-capture-2.png')} alt="Start capture" width="650"/> |
| 48 | + |
| 49 | + * Inspect network logs as they arrive |
| 50 | + |
| 51 | +<img src={useBaseUrl('img/mobile-apps/network-capture-3.png')} alt="Inspect network logs" width="650"/> |
| 52 | + |
| 53 | + * A copy of what you see in the inspector can be downloaded in HAR format with the following button. |
| 54 | + |
| 55 | +<img src={useBaseUrl('img/mobile-apps/network-capture-4.png')} alt="Inspect network logs" width="650"/> |
| 56 | + |
| 57 | +## Accessing HAR Files (Live and Automated Testing) |
| 58 | + |
| 59 | +To download HAR files from Sauce Labs: |
| 60 | + |
| 61 | +1. Navigate to **Test Results**. |
| 62 | +2. Select the applicable test. |
| 63 | +3. Click the **Network** tab in the bottom to preview network calls. |
| 64 | +4. Click the **Logs** tab on the right panel and select **network.har** from the dropdown menu to download the report. |
| 65 | + |
| 66 | +You can also download a HAR file programmatically using the following API request: |
| 67 | + |
| 68 | +```curl |
| 69 | +curl --compressed \ |
| 70 | +-O https://{SAUCE_USERNAME}:{SAUCE_ACCESS_KEY}@{DATA_CENTER}.saucelabs.com/v1/rdc/jobs/{JOB_ID}/network.har |
| 71 | +``` |
| 72 | + |
| 73 | +### Viewing HAR Files |
| 74 | + |
| 75 | +You'll need a HAR viewer to read HAR files. Sauce Labs provides a [React Network Viewer](https://opensource.saucelabs.com/network-viewer/) HAR viewer for your convenience. For step-by-step usage instructions, see our [Visualize HAR Files with the New React Network Viewer](https://opensource.saucelabs.com/blog/react_network_viewer/) blog article. |
| 76 | + |
| 77 | +Alternatively, some other commonly used HAR viewers include: |
| 78 | + |
| 79 | +- Chrome devtools builtin [HAR imports](https://developer.chrome.com/blog/new-in-devtools-62/#har-imports) |
| 80 | +- Google Admin Toolbox [HAR Analyzer](https://toolbox.googleapps.com/apps/har_analyzer/) |
| 81 | +- Software is Hard blog [HTTP Archive Viewer](http://www.softwareishard.com/har/viewer/) |
| 82 | +- Firefox network monitoring builtin [HAR imports](https://firefox-source-docs.mozilla.org/devtools-user/network_monitor/toolbar/index.html) |
| 83 | + |
| 84 | +## HAR Files |
| 85 | + |
| 86 | +[HTTP Archive Format (HAR) files](<https://en.wikipedia.org/wiki/HAR_(file_format)>) collect all network requests and responses made and received by the browser during testing. HAR files offer useful debugging information, such as: |
| 87 | + |
| 88 | +- Identifying browser requests that time out |
| 89 | +- Pinpointing requests slowing down the loading process |
| 90 | +- Locating faulty API calls |
| 91 | + |
| 92 | +## Limitations |
| 93 | + |
| 94 | +<!-- prettier-ignore --> |
| 95 | +:::note Not Supported |
| 96 | + |
| 97 | +- Android Emulators |
| 98 | +- iOS Simulators |
| 99 | +- Hybrid Applications in automated tests (React Native, Cordova, Flutter) |
| 100 | +- Android Chrome Browser in automated tests |
| 101 | +- iOS Safari Browser in automated tests |
| 102 | + |
| 103 | +::: |
| 104 | + |
| 105 | +### Android |
| 106 | + |
| 107 | +Our network capture code depends on the fact that these classes are not obfuscated: |
| 108 | + |
| 109 | +[OkHTTP](https://square.github.io/okhttp/) |
| 110 | + |
| 111 | +- okhttp3.Interceptor |
| 112 | +- okhttp3.OkHttpClient |
| 113 | +- okhttp3.Request |
| 114 | +- okhttp3.Response |
| 115 | +- okhttp3.ResponseBody |
| 116 | +- okio.Buffer |
| 117 | + |
| 118 | +[Volley](https://github.com/google/volley) |
| 119 | + |
| 120 | +- com.android.volley.AuthFailureError |
| 121 | +- com.android.volley.NetworkResponse |
| 122 | +- com.android.volley.Request |
| 123 | +- com.android.volley.RequestQueue |
| 124 | +- com.android.volley.Response |
| 125 | + |
| 126 | +### iOS |
| 127 | + |
| 128 | +- Network capture works if the app uses [NSURLSession](https://developer.apple.com/documentation/foundation/nsurlsession) or a library (like [Alamofire](https://github.com/Alamofire/Alamofire) or [AFNetworking](https://github.com/AFNetworking/AFNetworking)) that uses NSURLSession inside. |
| 129 | +- Calls made by [NSURLConnection](https://developer.apple.com/documentation/foundation/nsurlconnection) (deprecated by Apple), will not be captured. |
| 130 | + |
| 131 | +## More Information |
| 132 | + |
| 133 | +- [Sauce Labs Test Results](/test-results) |
| 134 | +- [Sauce Labs Insights](/insights) |
0 commit comments