protractor-parallel-instances
A simple tool that allows you to create protractor browser instances based on the
configuration file passed to createNewBrowser(*) method. You need to explicitly handle closing of browser as protractor does not handle it
Installation: npm i protractor-parallel-instances --save-dev
Table of Contents
ParallelInstanceHelper
createNewBrowser
Helps in creating new ProtractorBrowser instance based on the conf file passed into the arguments
You need to close these browsers explicitly by calling closeBrowser() as protractor does not manage it
Returns an instance of ProtractorBrowser
Parameters
configurationFile
anywaitForAngularEnabled
boolean
Returns Promise<ProtractorBrowser>
switchToDefaultBrowser
Switches the driver context to the global/default browser
switchToNewBrowser
Switches the driver content to local browsers
index is an optional parameter, if there is only one local instance available then
calling this method will switch the driver context to the local browser
Otherwise an Exception is thrown to specify the browser index explicitly
Parameters
executeInNewBrowser
Executes a piece of code in newly created driver and sets global values to default
once function definition is executed
Parameters
oldBrowser
native browsernewBrowser
browser created by calling createNewBrowser methoddefinition
verb - operations you want to perform
closeBrowser
Closes locally create driver instances
Parameters
• Problem statement:
Protractor uses a static configuration file for creating driver sessions
In some cases, we need to use other browsers(same/different) in parallel with native browser/appium sessions opened by protractor.
One such case being, when we use protractor with Appium and if we need to provide support for test-cases which
interacts with Appium and desktop browser simultaneously, then we have a problem
• Solution:
There is a npm package created to handle such situtations which lets you dynamically create multiple browser sessions without
affecting existing driver sessions based on the configuration file provided.
https://www.npmjs.com/package/protractor-parallel-instances - Navigate to the
package to get installation instructions.
• Usage instructions:
How to create a new browser session:
Import protractor conf file and pass the imported 'config' object to createNewBrowser method of ParallelInstanceHelper class
const newBrowser = await ParallelInstanceHelper.createNewBrowser(config);
There are two ways to work with newly created browser:
1. Finding elements using local browser reference:
newBrowser.element(By.css('selector')).click();
newBrowser.element(By.css('selector')).sendKeys('Test');
2. Overriding global objects with newly created 'newBrowser' object
This approach can also be implemeneted in two ways:
a. Calling 'await ParallelInstanceHelper.switchToNewBrowser();' to switch to new browser and calling 'await ParallelInstanceHelper.switchToDefaultBrowser();' to switch to default browser:
This will override the global object- 'browser' content with newly created 'newBrowser'. In case of any error in your script, as globals have been overriden by 'switchToNewBrowser' method, all the tests will run against newly created browser.
b. Overriding global only for specific code block by calling 'await ParallelInstanceHelper.executeInNewBrowser';
await ParallelInstanceHelper.executeInNewBrowser(browser, newBrowser, async () => {
await browser.get('https://google.com');
await browser.element(By.css('selector')).click();
await browser.element(By.css('selector')).sendKeys('Test');
});
This block using try-finally block, so even in case of any error during execution; Global object will be again set to default browser
• Things to be taken care:
New instances created by calling 'createNewBrowser' method are not handled by protractor runner, so closing of these browsers needs to be done explicitly
Calling 'await ParallelInstanceHelper.closeBrowser();' will close the first locally created browser.
Note: closeBrowser also accepts index of browsers for closing them.
0 being the index of native browser and 1 to n being indexes of locally created browsers