8

For example, I would like to test a chat feature between two logged in users implemented using AngularJS.

The test needs to verify two or more separate sets of browser actions occur in the proper sequence. For example, user A logs in, user B logs in, user A sees B is logged in, user A sends user B chat text, user B receives chat text. user B responds to user A.

Is this something that can be done using protractor? If not, is there another way to automatically test multiple browser session workflows?

softweave
  • 1,455
  • 2
  • 16
  • 27
  • This may help you http://stackoverflow.com/questions/20692008/protractor-run-multiple-tests-in-parallel-on-different-browsers – Mike Pugh May 08 '14 at 14:24
  • I don't believe just running one or more specs in parallel will meet this requirement. I added a sample workflow to the question to clarify the requirements. – softweave May 08 '14 at 14:50

2 Answers2

12

UPDATE This feature has finally being added see example usage and docs and tests here or see below:

browser.get('http://www.angularjs.org');

// To create a new browser with url as 'http://www.angularjs.org':
var browser2 = browser.forkNewDriverInstance(true);

// Interaction in the first browser
element(by.model(...)).click();

// Interaction in the second browser
browser2.$('.css').click();

Note that IE and Safari won't isolate cookies or cache so don't expect to be able to login with different users at the same time there unless your selenium provider is a grid that always uses a clean session like SauceLabs or BrowserStack.

Leo Gallucci
  • 16,355
  • 12
  • 77
  • 110
  • The feature to support multiple browser instances in a single test is part of Protractor 1.5.1. – softweave Apr 09 '15 at 13:41
  • 1
    There is also a protractor plugin that creates screenshots for each browsers instance and provides nice visualizations of results - https://github.com/azachar/protractor-screenshoter-plugin . Disclaimer, I am the author of it. – Andrej Oct 19 '16 at 17:00
6

You can open additional windows within a test and execute actions and assertions in those windows. You do this using standard webdriverjs APIs:

browser.getAllWindowHandles().then(function (handles) {

  // handle of first window
  var originalHandle = handles[0];

  // open new window
  browser.executeScript('window.open("https://angularjs.org/", "second-window")');

  // switch to new window
  browser.switchTo().window('second-window');

  // do something within context of new window

  // switch to original window
  browser.switchTo().window(originalHandle);

  // do something within context of original window

  // closes the current window
  browser.executeScript('window.close()');

});

This may not work for you if you use session cookies. Both windows may get the same cookie. If so you will end up logging in as the same user in both windows. One workaround is accessing the second instance of the application on a different domain. For example using the IP address or setting up a new domain in your hosts file..

chashi
  • 486
  • 1
  • 3
  • 9
  • 1
    Hey! I like your idea for the workaround *"using the IP address or setting up a new domain in your hosts file"* very clever :) – Leo Gallucci Aug 23 '14 at 17:44