Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open multiple windows or operate multiple instances

If for whatever crazy reason I need to open 100 windows/tabs and navigate to 100 different links in them how do I do that? Can I simultaneously run certain tests in all 100 of them?

let's say I have an array ['a','b','c','d','e'], I need to test if some form works for all these values. How can I open 5 instances (or windows or whatever that can be controlled independently from the rest) and test them simultaneously? e.g.:

  • find form's input text box,
  • change the text value to one of the array's
  • click submit button
  • run certain assertion etc. and all that in parallel. Simultaneously testing all array values and not one by one

upd: I guess I can open multiple tabs using

browser.executeScript("window.open('https://angularjs.org/', 'tab" + i + "')")

yet that doesn't allow me to truly run tests in parallel, since I'm gonna have to jump from tab to tab, assuming all tabs open and loaded:

1) pick value from array 2) modify input box 3) click submit button 4) switch to next tab 5) repeat

Yes, this will still be faster than testing everything in one tab, looping through array and resetting the page every time, but I need to find a better way

like image 677
iLemming Avatar asked Dec 20 '22 07:12

iLemming


1 Answers

It sounds terribly inefficient to kick off 100 different browsers just so they can run very similar scenarios. If those different values kick off the same flow, just slightly different output, you might want to use unit tests for those and run just one or few using protractor to test end-to-end.

But to answer your question, there are two ways.

1) multiCapabilities: Here, each browser will run a completely different test. (You might want to reuse a common component if your tests are similar).

exports.config = {
  specs: [
    // leave this empty if you have no shared tests. 
  ],

  multiCapabilities: [{
    'browserName': 'chrome',
    'specs': ['test1.js']
  }, {
    'browserName': 'chrome',
    'specs': ['test2.js']
  }, {
    'browserName': 'chrome',
    'specs': ['test3.js']
  }],
};

Doc: https://github.com/angular/protractor/blob/master/docs/referenceConf.js

2) browser.forkNewDriverInstance(): Here, you only run one test, but the test can spawn off n separate browsers. Disadvantage is that since everything is in just 1 test, if 1 case out of 100 fails, you'll just get a single failure.

var runtest = function(input, output) {
  var newBrowser = browser.forkNewDriverInstance(true); // true means use same url
  // note I used newBrowser.element instead of element, because you are accessing the new browser. 
  newBrowser.element(by.model(...)).sendKeys(input).click();
  expect(newBrowser.element(by.css('blah')).getText()).toEqual(output);
};

describe('...', function() {
  it('spawn browsers', function() {
    browser.get(YOUR_COMMON_URL);

    runtest('input1', 'output1');
    runtest('input2', 'output2');
    runtest('input3', 'output3');
    runtest('input4', 'output4');
  });
});

Doc: https://github.com/angular/protractor/blob/master/docs/browser-setup.md#using-multiple-browsers-in-the-same-test

like image 105
hankduan Avatar answered Dec 21 '22 19:12

hankduan