protractor-parallel-instances
Advanced tools
Comparing version 1.0.20 to 1.2.1
import { ProtractorBrowser } from 'protractor'; | ||
export declare class ParallelInstanceHelper { | ||
private static ptorRunner; | ||
private static browserInstances; | ||
private static browser; | ||
/** | ||
* 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 | ||
* @param {any} configurationFile | ||
* @param {boolean} waitForAngularEnabled | ||
* @returns {Promise<ProtractorBrowser>} | ||
* Helps in creating new ProtractorBrowser instance. | ||
* It creates instances based on capabilities present in configuration passed | ||
* @return ProtractorBrowser | ||
* @param configurationFile | ||
* @param waitForAngularEnabled | ||
*/ | ||
static createNewBrowser(configurationFile: any, waitForAngularEnabled?: boolean): Promise<any>; | ||
static createNewBrowser(configurationFile: any, waitForAngularEnabled?: boolean): Promise<ProtractorBrowser>; | ||
/** | ||
* Switches the driver context to the global/default browser | ||
* It updates the global `browser` object with newly create driver instance | ||
*/ | ||
static switchToDefaultBrowser(): Promise<void>; | ||
/** | ||
* 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 | ||
* @param index | ||
*/ | ||
static switchToNewBrowser(index?: number): Promise<void>; | ||
/** | ||
* Executes a piece of code in newly created driver and sets global values to default | ||
* once function definition is executed | ||
* @param oldBrowser - native browser | ||
* @param newBrowser - browser created by calling createNewBrowser method | ||
* @param definition - verb - operations you want to perform | ||
*/ | ||
static executeInNewBrowser(oldBrowser: ProtractorBrowser, newBrowser: ProtractorBrowser, definition: Function): Promise<void>; | ||
/** | ||
* Closes locally create driver instances | ||
* @param index | ||
*/ | ||
static closeBrowser(index?: number): Promise<void>; | ||
/** | ||
* Returns an array of newly created browsers | ||
*/ | ||
static getBrowsers(): any[]; | ||
static updateGlobal(): Promise<void>; | ||
} |
@@ -38,4 +38,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// @ts-ignore | ||
var protractor_1 = require("protractor"); | ||
var ProtractorConfigParser = require('protractor/built/configParser').ConfigParser; | ||
@@ -47,8 +45,7 @@ var ProtractorRunner = require('protractor/built/runner').Runner; | ||
/** | ||
* 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 | ||
* @param {any} configurationFile | ||
* @param {boolean} waitForAngularEnabled | ||
* @returns {Promise<ProtractorBrowser>} | ||
* Helps in creating new ProtractorBrowser instance. | ||
* It creates instances based on capabilities present in configuration passed | ||
* @return ProtractorBrowser | ||
* @param configurationFile | ||
* @param waitForAngularEnabled | ||
*/ | ||
@@ -58,5 +55,5 @@ ParallelInstanceHelper.createNewBrowser = function (configurationFile, waitForAngularEnabled) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var ptorConfig, newBrowser; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var ptorConfig, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
@@ -71,17 +68,17 @@ ptorConfig = new ProtractorConfigParser().config_; | ||
ptorConfig.noGlobals = true; | ||
ParallelInstanceHelper.ptorRunner = new ProtractorRunner(ptorConfig); | ||
ParallelInstanceHelper.ptorRunner.driverprovider_.setupEnv(); | ||
this.ptorRunner = new ProtractorRunner(ptorConfig); | ||
this.ptorRunner.driverprovider_.setupEnv(); | ||
_a = this; | ||
return [4 /*yield*/, this.ptorRunner.createBrowser()]; | ||
case 1: | ||
newBrowser = _a.sent(); | ||
_a.browser = _b.sent(); | ||
// wait for binaries to respond | ||
return [4 /*yield*/, protractor_1.browser.sleep(5000)]; | ||
return [4 /*yield*/, this.browser.sleep(5000)]; | ||
case 2: | ||
// wait for binaries to respond | ||
_a.sent(); | ||
ParallelInstanceHelper.browserInstances.push(newBrowser); | ||
return [4 /*yield*/, newBrowser.waitForAngularEnabled(waitForAngularEnabled)]; | ||
_b.sent(); | ||
return [4 /*yield*/, this.browser.waitForAngularEnabled(waitForAngularEnabled)]; | ||
case 3: | ||
_a.sent(); | ||
return [2 /*return*/, newBrowser]; | ||
_b.sent(); | ||
return [2 /*return*/, this.browser]; | ||
} | ||
@@ -92,12 +89,8 @@ }); | ||
/** | ||
* Switches the driver context to the global/default browser | ||
* It updates the global `browser` object with newly create driver instance | ||
*/ | ||
ParallelInstanceHelper.switchToDefaultBrowser = function () { | ||
ParallelInstanceHelper.updateGlobal = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
// undefined check | ||
if (ParallelInstanceHelper.ptorRunner === undefined) { | ||
throw new Error('Please invoke ParallelInstanceHelper.createNewBrowser() first to createNewBrowser a new session'); | ||
} | ||
ParallelInstanceHelper.ptorRunner.setupGlobals_(this.browserInstances[0]); | ||
this.ptorRunner.setupGlobals_(this.browser); | ||
return [2 /*return*/]; | ||
@@ -107,84 +100,4 @@ }); | ||
}; | ||
/** | ||
* 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 | ||
* @param index | ||
*/ | ||
ParallelInstanceHelper.switchToNewBrowser = function (index) { | ||
if (index === void 0) { index = 1; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
// undefined check | ||
if (ParallelInstanceHelper.ptorRunner === undefined) { | ||
throw new Error('Please invoke ParallelInstanceHelper.createNewBrowser() first to createNewBrowser a new session'); | ||
} | ||
if (index === 1 && ParallelInstanceHelper.browserInstances.length > 2) { | ||
throw new Error('More than 2 browser instances are available; Please provide pass a browser index explicitly'); | ||
} | ||
this.ptorRunner.setupGlobals_(ParallelInstanceHelper.browserInstances[index]); | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Executes a piece of code in newly created driver and sets global values to default | ||
* once function definition is executed | ||
* @param oldBrowser - native browser | ||
* @param newBrowser - browser created by calling createNewBrowser method | ||
* @param definition - verb - operations you want to perform | ||
*/ | ||
ParallelInstanceHelper.executeInNewBrowser = function (oldBrowser, newBrowser, definition) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
// undefined check | ||
if (ParallelInstanceHelper.ptorRunner === undefined) { | ||
throw new Error('Please invoke ParallelInstanceHelper.createNewBrowser() first to createNewBrowser a new session'); | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, , 3, 4]); | ||
ParallelInstanceHelper.ptorRunner.setupGlobals_(newBrowser); | ||
return [4 /*yield*/, definition()]; | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
ParallelInstanceHelper.ptorRunner.setupGlobals_(oldBrowser); | ||
return [7 /*endfinally*/]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Closes locally create driver instances | ||
* @param index | ||
*/ | ||
ParallelInstanceHelper.closeBrowser = function (index) { | ||
if (index === void 0) { index = 1; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, ParallelInstanceHelper.browserInstances[index].close()]; | ||
case 1: | ||
_a.sent(); | ||
ParallelInstanceHelper.browserInstances.splice(index, 1); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Returns an array of newly created browsers | ||
*/ | ||
ParallelInstanceHelper.getBrowsers = function () { | ||
return ParallelInstanceHelper.browserInstances; | ||
}; | ||
ParallelInstanceHelper.browserInstances = [protractor_1.browser]; | ||
return ParallelInstanceHelper; | ||
}()); | ||
exports.ParallelInstanceHelper = ParallelInstanceHelper; |
{ | ||
"name": "protractor-parallel-instances", | ||
"version": "1.0.20", | ||
"description": "Helps in creating dynamic instances based on the conf file passed", | ||
"version": "1.2.1", | ||
"description": "Node package to create protractor browser objects on the fly", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/shoaibmansoor/protractor-parallel-instances.git" | ||
}, | ||
"scripts": { | ||
"build": "tsc" | ||
"build": "tsc", | ||
"webdriver:update": "webdriver-manager update --standalone=false --gecko=false", | ||
"lint": "tslint -p tsconfig.json -c tslint.json", | ||
"lint:fix": "tslint -p tsconfig.json -c tslint.json --fix", | ||
"test": "protractor protractor.conf.js" | ||
}, | ||
@@ -13,3 +21,9 @@ "keywords": [ | ||
"protractor", | ||
"instances", | ||
"protractor-parallel", | ||
"dynamic", | ||
"on-the-go", | ||
"realtime", | ||
"configuration", | ||
"on-the-fly", | ||
"conf" | ||
@@ -20,4 +34,15 @@ ], | ||
"devDependencies": { | ||
"@types/jasmine": "^3.3.9", | ||
"@angular/compiler": "^6.1.8", | ||
"rxjs": "^6.3.2", | ||
"@angular/core": "^6.1.8", | ||
"jasmine": "^3.3.1", | ||
"jasmine-spec-reporter": "^4.2.1", | ||
"tslint-jasmine-rules": "^1.5.1", | ||
"ts-node": "^8.0.2", | ||
"tslint": "~5.11.0", | ||
"codelyzer": "~4.4.4", | ||
"typescript": "^3.1.5", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"protractor": "file:~/node_modules/protractor", | ||
"typescript": "^3.1.5", | ||
"webdriver-manager": "file:~/node_modules/protractor/bin/webdriver-manager" | ||
@@ -27,3 +52,19 @@ }, | ||
"@types/node": "^10.12.1" | ||
}, | ||
"standard": { | ||
"globals": [ | ||
"describe", | ||
"context", | ||
"before", | ||
"sandbox", | ||
"beforeEach", | ||
"after", | ||
"afterEach", | ||
"it", | ||
"expect", | ||
"sinon", | ||
"browser", | ||
"$" | ||
] | ||
} | ||
} |
145
README.md
@@ -1,20 +0,32 @@ | ||
## protractor-parallel-instances | ||
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 | ||
Node module to create browser instances on the fly. It accepts a protractor config file to create new instances | ||
### Installation: npm i protractor-parallel-instances --save-dev | ||
```shell | ||
npm i protractor-parallel-instances --save-dev | ||
``` | ||
# Usage | ||
```typescript | ||
// Typescript | ||
import { config } from 'protractor.conf'; | ||
import { ParallelInstanceHelper } from 'protractor-parallel-instances'; | ||
// create new browser instance with imported config file | ||
const newBrowser = await ParallelInstanceHelper.createNewBrowser(config); | ||
await newBrowser.get('https://google.com'); | ||
``` | ||
```javascript | ||
// Javascript: TODO | ||
``` | ||
### Table of Contents | ||
- [ParallelInstanceHelper][1] | ||
- [createNewBrowser][2] | ||
- [ParallelInstanceHelper class][1] | ||
- [createNewBrowser function][2] | ||
- [Parameters][3] | ||
- [switchToDefaultBrowser][4] | ||
- [switchToNewBrowser][5] | ||
- [Parameters][6] | ||
- [executeInNewBrowser][7] | ||
- [Parameters][8] | ||
- [closeBrowser][9] | ||
- [Parameters][10] | ||
- [updateGlobal function][4] | ||
@@ -25,5 +37,3 @@ ## ParallelInstanceHelper | ||
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 | ||
Helps in creating new ProtractorBrowser instance. Accepts protractor config file to create new instances | ||
@@ -33,40 +43,13 @@ #### Parameters | ||
- `configurationFile` **any** | ||
- `waitForAngularEnabled` **[boolean][11]** | ||
- `waitForAngularEnabled` **[boolean]** | ||
Returns **[Promise][12]<ProtractorBrowser>** | ||
Returns **[Promise]\<ProtractorBrowser>** | ||
### switchToDefaultBrowser | ||
### updateGlobal | ||
Switches the driver context to the global/default browser | ||
Updates the global `browser` object with newly create driver instance | ||
```typescript | ||
await ParallelInstanceHelper.updateGlobal(); | ||
``` | ||
### 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 | ||
- `index` | ||
### executeInNewBrowser | ||
Executes a piece of code in newly created driver and sets global values to default | ||
once function definition is executed | ||
#### Parameters | ||
- `oldBrowser` native browser | ||
- `newBrowser` browser created by calling createNewBrowser method | ||
- `definition` verb - operations you want to perform | ||
### closeBrowser | ||
Closes locally create driver instances | ||
#### Parameters | ||
- `index` | ||
[1]: #parallelinstancehelper | ||
@@ -78,62 +61,2 @@ | ||
[4]: #switchtodefaultbrowser | ||
[5]: #switchtonewbrowser | ||
[6]: #parameters-1 | ||
[7]: #executeinnewbrowser | ||
[8]: #parameters-2 | ||
[9]: #closebrowser | ||
[10]: #parameters-3 | ||
[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean | ||
[12]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise | ||
### • 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 | ||
[4]: #updateglobal |
@@ -6,6 +6,21 @@ { | ||
"lib": ["es2017"], | ||
"declaration": true, | ||
"declaration": false, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"skipLibCheck": true, | ||
"allowJs": true, | ||
"moduleResolution": "node", | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"outDir": "./dist", | ||
"strict": true | ||
} | ||
}, | ||
"include": [ | ||
"src/**/*.ts" | ||
], | ||
"exclude": [ | ||
"dist", | ||
"test/**/*.ts", | ||
"node_modules" | ||
] | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
11
471
0
19110
14
59
1