kibana/test/analytics
Marco Antonio Ghiani aa45152a4e
[FTR] Implement browser network condition utils (#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](da276a3fae/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 <marcoantonio.ghiani@elastic.co>
Co-authored-by: Dzmitry Lemechko <dzmitry.lemechko@elastic.co>
2023-08-11 14:24:06 +02:00
..
plugins [packages] migrate all plugins to packages (#148130) 2023-02-08 21:06:50 -06:00
services [EBT] Better FTR helper APIs (#135298) 2022-06-29 14:11:13 +02:00
tests [FTR] Implement browser network condition utils (#163633) 2023-08-11 14:24:06 +02:00
config.ts Move real plugins out of 'fixtures' dirs (#148756) 2023-01-12 12:38:49 -07:00
README.md Move real plugins out of 'fixtures' dirs (#148756) 2023-01-12 12:38:49 -07:00

FTR tests for the core.analytics service

This suite allows us to test the event-based telemetry client provided by the core.analytics service.

How to add my own tests

Implement your own tests in the tests/instrumented_events/(from_the_browser|from_the_server)/ directory and list it in the tests/instrumented_events/(from_the_browser|from_the_server)/index.ts file.

There are 2 FTR helpers to allow you to retrieve the generated events:

  1. For events generated in the UI, you can use kibana_ebt_ui to retrieve the events.
  2. For events generated in the server, you can use the kibana_ebt_server helper.

The API is the same for both of them:

// To retrieve 2 events of type "my-event-type"
const events = await getService('kibana_ebt_ui').getEvents(2, { eventTypes: ['my-event-type'] });
expect(events).to...

If you are reusing these helpers in another suite, please remember to make sure to optIn via await getService('kibana_ebt_ui').setOptIn(true);

Refer to EBTHelpersContract for more details about the existing APIs and all the options they accept.