0

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.

SeleM
  • 9,310
  • 5
  • 32
  • 51
hackp0int
  • 4,052
  • 8
  • 59
  • 95
  • 1
    You should play with it https://github.com/seleniumhq/selenium/commit/d7bdd7cc42faafb1eba031c26b7c369cdea026d5 . I have not done it before but it should work. – Oleksii Nov 06 '18 at 14:03

2 Answers2

2

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

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
  • could I run it programmatically – hackp0int Nov 08 '18 at 15:40
  • @SergeyPleashakov - No, I would like to turn it on/off depending on test. Not the whole tests suits but only single test. – hackp0int Nov 08 '18 at 16:10
  • If I understand it right, these args are used to start the browser. Normally a suite is ran in the same session. And the session uses args passed on launch. So you can't do what you want this way. But try to look up info of how to change these args while session is open or how kill session and reopen with args other then in your configs – Sergey Pleshakov Nov 08 '18 at 16:19
2

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

technodeath
  • 123
  • 3
  • @technodeatch - how do you use it inside the tests? – hackp0int Nov 15 '18 at 10:31
  • I have `enableThrottling(false)` by default in `onPrepare()` section in my config file. In a case when I want to use slow emulation, just change `false` to `true` and start the tests. Also this function above is outside of onPrepare() function, at the top of my conf – technodeath Nov 16 '18 at 11:11
  • could you please demonstrate it inside `it()` – hackp0int Nov 18 '18 at 14:42
  • it's not inside `it()` ! it just enables/disables in config file before start of tests. – technodeath Nov 19 '18 at 10:51
  • @techondeath - How can I enable and disable it from `it()` I need only one test to use this function. – hackp0int Nov 19 '18 at 13:12