Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FTR] Implement browser network condition utils (elastic#163633)
## 📓 Summary The PR implements some utilities into the `browser` service to allow controlling the network conditions during the execution of a functional test. ### `getNetworkConditions` Returns the current network simulation options. If none conditions are previously set, it returns `undefined` **N.B.**: _if the testing environment is not a Chromium browser, it throws an error that can be easily caught to manually skip the test or handle a fallback scenario._ ```ts it('should display a loading skeleton while loading', async function () { // Skip the test in case network condition utils are not available try { const networkConditions = await browser.getNetworkConditions(); // undefined await browser.setNetworkConditions('SLOW_3G'); const networkConditions = await browser.getNetworkConditions(); // { // offline: false, // latency: 2000, // download_throughput: 50000, // upload_throughput: 50000, // } } catch (error) { this.skip(); } }); ``` ### `setNetworkConditions` Set the desired network conditions. It supports different presets that match the [network profiles provided by Chrome debugger](https://github.com/ChromeDevTools/devtools-frontend/blob/da276a3faec9769cb55e442f0db77ebdce5cd178/front_end/core/sdk/NetworkManager.ts#L363-L393): - `NO_THROTTLING` - `FAST_3G` - `SLOW_3G` - `OFFLINE` - `CLOUD_USER` (pre-existing) It also accepts ad-hoc options to configure more specifically the network conditions. **N.B.**: _if the testing environment is not a Chromium browser, it throws an error that can be easily caught to manually skip the test or handle a fallback scenario._ ```ts it('should display a loading skeleton while loading', async function () { // Skip the test in case network condition utils are not available try { await browser.setNetworkConditions('NO_THROTTLING'); await browser.setNetworkConditions('FAST_3G'); await browser.setNetworkConditions('SLOW_3G'); await browser.setNetworkConditions('OFFLINE'); await browser.setNetworkConditions('CLOUD_USER'); await browser.setNetworkConditions({ offline: false, latency: 5, // Additional latency (ms). download_throughput: 500 * 1024, // Maximal aggregated download throughput. upload_throughput: 500 * 1024, // Maximal aggregated upload throughput. }); } catch (error) { this.skip(); } }); ``` ### restoreNetworkConditions Restore the original network conditions, setting to `NO_THROTTLING`. The native implementation of `deleteNetworkConditions` exposed by selenium is unofficial and didn't consistently work, the recommended approach by the google dev tools team is to restore the connection setting the no throttling profile. **N.B.**: _if the testing environment is not a Chromium browser, it throws an error that can be easily caught to manually skip the test or handle a fallback scenario._ ```ts it('should display a loading skeleton while loading', async function () { // Skip the test in case network condition utils are not available try { await browser.setNetworkConditions('SLOW_3G'); // Slow down network conditions // Do your assertions await browser.restoreNetworkConditions(); // Restore network conditions } catch (error) { this.skip(); } }); ``` Co-authored-by: Marco Antonio Ghiani <[email protected]> Co-authored-by: Dzmitry Lemechko <[email protected]> (cherry picked from commit aa45152)
- Loading branch information