3

I'm writing a test where two browsers need to interact. The problem with simply forking the browser is that my page objects still reference the old browser. I didn't want to rewrite all of my PO's to take the browser as a parameter so I tried the first solution found in the link below where they overwrite the global variables with the new browser's version :

Multiple browsers and the Page Object pattern

However, changing the global variables doesn't seem to work as all the subsequent page object functions that I call are performed against the original browser instance. I have tried logging the window handler before and after the switch and they are indeed different which only baffles me further. Here's some of the code.

spec:

var MultiBrowserFunctions = require('../common/multiBrowserFunctions.js');
var HomePage = require('../home/home.po.js');
describe('blah', function(){
    it('blah', function(){
        MultiBrowserFunctions.openNewBrowser(true);
        HomePage.initializePage();
    });
});

MultiBrowserFunctions:

(function() {
var browserRegistry = [];
module.exports = {
    openNewBrowser: function(isSameUrl){
        if(typeof browserRegistry[0] == 'undefined'){
            browserRegistry[0] = {
                    browser: browser,
                    element: element,
                    $: $,
                    $$: $$,
                }
          }
          var tmp = browser.forkNewDriverInstance(isSameUrl);
          var id = browserRegistry.length;
          browserRegistry[id] = {
              browser: tmp,
              element: tmp.element,
              $: tmp.$,
              $$: tmp.$$,
          }
          switchToBrowserContext(id);
          return id;
        },
    resetBrowserInstance : function(){
        browserRegistry.splice(1,browserRegistry.length);
        switchToBrowserContext(0);
    }
}

function switchToBrowserContext(id){
    console.log('---------------------------switching to browser: ' + id);
        browser=browserRegistry[id].browser;
        element=browserRegistry[id].element;
        $=browserRegistry[id].$;
        $$=browserRegistry[id].$$;
    }
}());

My questions are: (1) why doesn't this work? (2) Is there some other solution that doesn't involve rewriting all of my po's?

Community
  • 1
  • 1
Fitzpleasure
  • 109
  • 1
  • 13
  • 1. There's no need to wrap your export in a closure 2. There's not enough detail here to figure out what is wrong. The example spec you give only opens one browser. Can you add a more detailed example? – Nick Tomlin Nov 02 '15 at 23:04

1 Answers1

0

What you can do is, save the browsers in different variables and then switch between them by overriding the globals via a utility or something.

describe('Switching browsers back and forth', function () {
   var browserA, browserB;
   it('Browser Switch', function () {
       var browsers = {
           a : browser,
           b : browser.forkNewDriverInstance(true)
        };
        browserA = browsers.a;
        browserB = browsers.b;

        var browserAndElement = switchBrowser(browserB);
        browser = browserAndElement.browser;
        element = browserAndElement.element;
        //do your stuff

        var browserAndElement = switchBrowser(browserA);
        browser = browserAndElement.browser;
        element = browserAndElement.element;
        //do your stuff
    });
});

The switchBrowser() can look like following:

this.switchBrowser = function (currentBrowser) {
    browser = currentBrowser;
    element = currentBrowser.element;
    return {
        browser : browser,
        element : element
    }
}

In this way you don't have to rewrite your POs to take in the new globals. Hope it helps!

Cheers

CodeJockey
  • 423
  • 5
  • 18