E2E Angular application for slow network 3g slow using Protractor

1.5k views Asked by At

I have a question, I need to simulate slow network on my e2e tests with Protractor. I'm using anuglar-cli and angular5.

I have tried to find a way to simulate slow network without success, using setNetworkConnection But it's fails to work and throws an exception.

The issue is only on slow network my typeahead is creating an extra call, because the before call haven't completed yet, I'm using this function to do it:

this.modelChanged
    .debounceTime(400)
    .distinctUntilChanged()
    .subscribe((model: FilterModel) => {
        this.filters.set(model.filterName, model.filterVal);
        const filters = {};
        this.filters.forEach((value: any, key: string) => {
            filters[key] = value;
        });
        this.ds.changeData({
            filters: filters
        });
    });

Maybe you will have a better suggestion to fix it too. Will gladly accept any suggestion.

Now I need to test it, to prevent the regression, when I will fix it with a better solution.

Thanks in advance.

2

There are 2 answers

3
Sergey Pleshakov On

If you use Chrome, take a look at this page https://peter.sh/experiments/chromium-command-line-switches/

When you start the browser you may pass arguments to it to specify the desired behavior. Especially pay attention to these args:

--shill-stub - 'cellular=1' - Cellular is initially connected 'cellular=LTE' - Cellular is initially connected

--enabled-3g

--force-effective-connection-type

************* E D I T *************

You specify args in config file. It should have capabilities object that looks like this

capabilities: {
        "browserName": "chrome",
        "chromeOptions": {
            "args": ["incognito", "--window-size=1920,1080", "disable-extensions", "--no-sandbox", "start-maximized", "--test-type=browser"],
            "prefs": {
                "download": {
                    "prompt_for_download": false,
                    "directory_upgrade": true,
                    "default_directory": path.join(process.cwd(), "__test__reports/downloads")
                    }
                }
        }
    },

and args is what you are looking for to pass arguments

5
technodeath On

I use that. Add to your Protractor's config.ts file:

    function enableThrottling(flag: boolean) {
  if(flag){
    if (typeof (browser.driver as any).setNetworkConditions === 'function') {
      console.log('set network conditions ON');
      (browser.driver as any).setNetworkConditions({
        offline: false,
        latency: 150,
        download_throughput: 450 * 1024,
        upload_throughput: 150 * 1024
      });
    }
  }
  else {console.log('set network conditions OFF')}
}

And then you can enable/disable it during your tests by enableThrottling(true)

Values of latency/download/upload could be changed to:

/*GPRS (50 * 1024)/(20 * 1024)/500 ms
Regular 2G (250 * 1024)/(50 * 1024)/300 ms
Good 2G (450 * 1024)/(150 * 1024)/150 ms
Regular 3G (750 * 1024)/(250 * 1024)/100 ms
Good 3G (1.5 * 1024 * 1024)/(750 * 1024)/40 ms
Regular 4G (4 * 1024 * 1024)/(3 * 1024 * 1024)/20 ms
DSL (2 * 1024 * 1024)/(1 * 1024 * 1024)/5 ms
Wifi (30 * 1024 * 1024)/(15 * 1024 * 1024)/2 ms */

So, for example, now in example above it's set up to Good 2G