Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
fix(exports): fix type exports and require('protractor') exports (#3404)
Browse files Browse the repository at this point in the history
* fix(package): set main to ptor instead of browser

* fix(exports): fix type exports and require('protractor') exports
  • Loading branch information
cnishina authored and heathkit committed Jul 21, 2016
1 parent 55fe53a commit 2a3a0dc
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ xmloutput*
npm-debug.log

*.swp
globals.js
3 changes: 2 additions & 1 deletion exampleTypescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"exclude": [
"node_modules",
"typings/globals"
"typings/globals",
"asyncAwait"
]
}
17 changes: 8 additions & 9 deletions globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ import {
ProtractorExpectedConditions
} from 'protractor';

export var browser: Browser = global['protractor.browser'];
export var element: ElementHelper = global['protractor.element'];
export var by: ProtractorBy = global['protractor.by'];
export var By: ProtractorBy = global['protractor.By'];
export var $: (search: string) => ElementFinder = global['protractor.$'];
export var $$: (search: string) => ElementArrayFinder = global['protractor.$$'];
export var ExpectedConditions: ProtractorExpectedConditions =
global['protractor.ExpectedConditions'];

export interface Protractor {
browser: Browser;
element: ElementHelper;
Expand All @@ -26,3 +17,11 @@ export interface Protractor {
ExpectedConditions: ProtractorExpectedConditions;
}
export var protractor: Protractor = global['protractor'];
export var browser: Browser = global['protractor']['browser'];
export var element: ElementHelper = global['protractor']['element'];
export var by: ProtractorBy = global['protractor']['by'];
export var By: ProtractorBy = global['protractor']['By'];
export var $: (search: string) => ElementFinder = global['protractor']['$'];
export var $$: (search: string) => ElementArrayFinder = global['protractor']['$$'];
export var ExpectedConditions: ProtractorExpectedConditions =
global['protractor']['ExpectedConditions'];
20 changes: 15 additions & 5 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ var fs = require('fs');
var path = require('path');
var glob = require('glob');

var runSpawn = function(done, task, opt_arg) {
var runSpawn = function(done, task, opt_arg, opt_io) {
opt_arg = typeof opt_arg !== 'undefined' ? opt_arg : [];
var child = spawn(task, opt_arg, {stdio: 'inherit'});
var stdio = 'inherit';
if (opt_io === 'ignore') {
stdio = 'ignore';
}
var child = spawn(task, opt_arg, {stdio: stdio});
var running = false;
child.on('close', function() {
if (!running) {
Expand Down Expand Up @@ -64,14 +68,20 @@ gulp.task('tsc', function(done) {
runSpawn(done, 'node', ['node_modules/typescript/bin/tsc']);
});

gulp.task('tsc:globals', function(done) {
runSpawn(done, 'node', ['node_modules/typescript/bin/tsc', 'globals.ts'],
'ignore');
});

gulp.task('prepublish', function(done) {
runSequence(['typings', 'jshint', 'format'], 'tsc', 'types', 'ambient', 'built:copy', done);
runSequence(['typings', 'jshint', 'format'], 'tsc', 'tsc:globals', 'types',
'ambient', 'built:copy', done);
});

gulp.task('pretest', function(done) {
runSequence(
['webdriver:update', 'typings', 'jshint', 'format'], 'tsc', 'types', 'ambient',
'built:copy', done);
['webdriver:update', 'typings', 'jshint', 'format'], 'tsc', 'types',
'ambient', 'tsc:globals', 'built:copy', done);
});

gulp.task('default',['prepublish']);
Expand Down
9 changes: 5 additions & 4 deletions lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {ProtractorExpectedConditions} from './expectedConditions';
import {Locator, ProtractorBy} from './locators';
import {Logger} from './logger';
import {Plugins} from './plugins';
import {protractor} from './ptor';
import * as helper from './util';

let clientSideScripts = require('./clientsidescripts');
let webdriver = require('selenium-webdriver');
let Command = require('selenium-webdriver/lib/command').Command;
let CommandName = require('selenium-webdriver/lib/command').Name;

// jshint browser: true
/* global angular */
Expand Down Expand Up @@ -361,7 +362,7 @@ export class Browser extends Webdriver {
}

return this.driver.schedule(
new protractor.Command(protractor.CommandName.EXECUTE_SCRIPT)
new Command(CommandName.EXECUTE_SCRIPT)
.setParameter('script', script)
.setParameter('args', scriptArgs),
description);
Expand All @@ -387,7 +388,7 @@ export class Browser extends Webdriver {
script = 'return (' + script + ').apply(null, arguments);';
}
return this.driver.schedule(
new protractor.Command(protractor.CommandName.EXECUTE_ASYNC_SCRIPT)
new Command(CommandName.EXECUTE_ASYNC_SCRIPT)
.setParameter('script', script)
.setParameter('args', scriptArgs),
description);
Expand Down Expand Up @@ -1003,7 +1004,7 @@ export class Browser extends Webdriver {
let context: Object = {require: require};
global.list = (locator: Locator) => {
/* globals browser */
return protractor.browser.findElements(locator).then(
return global.browser.findElements(locator).then(
(arr: webdriver.WebElement[]) => {
let found: string[] = [];
for (let i = 0; i < arr.length; ++i) {
Expand Down
11 changes: 5 additions & 6 deletions lib/expectedConditions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ElementFinder} from './element';
import {protractor} from './ptor';

let webdriver = require('selenium-webdriver');

Expand Down Expand Up @@ -150,7 +149,7 @@ export class ProtractorExpectedConditions {
*/
alertIsPresent(): Function {
return () => {
return protractor.browser.driver.switchTo().alert().then(
return global.browser.driver.switchTo().alert().then(
(): boolean => { return true; },
(err: any) => {
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ALERT) {
Expand Down Expand Up @@ -253,7 +252,7 @@ export class ProtractorExpectedConditions {
*/
titleContains(title: string): Function {
return () => {
return protractor.browser.driver.getTitle().then(
return global.browser.driver.getTitle().then(
(actualTitle: string): boolean => {
return actualTitle.indexOf(title) > -1;
});
Expand All @@ -276,7 +275,7 @@ export class ProtractorExpectedConditions {
*/
titleIs(title: string): Function {
return () => {
return protractor.browser.driver.getTitle().then(
return global.browser.driver.getTitle().then(
(actualTitle: string): boolean => { return actualTitle === title; });
};
}
Expand All @@ -298,7 +297,7 @@ export class ProtractorExpectedConditions {
*/
urlContains(url: string): Function {
return () => {
return protractor.browser.driver.getCurrentUrl().then(
return global.browser.driver.getCurrentUrl().then(
(actualUrl: string): boolean => {
return actualUrl.indexOf(url) > -1;
});
Expand All @@ -321,7 +320,7 @@ export class ProtractorExpectedConditions {
*/
urlIs(url: string): Function {
return () => {
return protractor.browser.driver.getCurrentUrl().then(
return global.browser.driver.getCurrentUrl().then(
(actualUrl: string): boolean => { return actualUrl === url; });
};
}
Expand Down
6 changes: 6 additions & 0 deletions lib/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Requiring protractor as a node module. Export the contents of the protractor
* namespace.
*/

export = require('./ptor').protractor
10 changes: 6 additions & 4 deletions lib/ptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ import {ProtractorBy} from './locators';
let webdriver = require('selenium-webdriver');

export namespace protractor {
// Variables tied to the global namespace.
export let browser: Browser;
export let $ = function(search: string): ElementFinder { return null;};
export let $$ = function(search: string): ElementArrayFinder { return null;};
export let element: ElementHelper;
export let By: ProtractorBy;
export let by: ProtractorBy;
export let wrapDriver: Function;
export let wrapDriver:
(webdriver: webdriver.WebDriver, baseUrl?: string, rootElement?: string,
untrackOutstandingTimeouts?: boolean) => Browser;
export let ExpectedConditions: ProtractorExpectedConditions;

// Export the protractor classes
// Export protractor classes.
export let Browser = require('./browser').Browser;
export let ElementFinder = require('./element').ElementFinder;
export let ElementArrayFinder = require('./element').ElementArrayFinder;
export let ElementHelper = require('./browser').ElementHelper;
export let ProtractorBy = require('./locators').ProtractorBy;
export let ProtractorExpectedConditions =
require('./expectedConditions').ProtractorExpectedConditions;

// Define selenium webdriver imports.
// Export selenium webdriver.
export let promise = webdriver.promise;
export let WebElement = webdriver.WebElement;
export let ActionSequence = webdriver.ActionSequence;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"protractor": "bin/protractor",
"webdriver-manager": "bin/webdriver-manager"
},
"main": "built/browser.js",
"main": "built/main.js",
"typings": "built/index.d.ts",
"scripts": {
"prepublish": "gulp prepublish",
Expand Down
64 changes: 64 additions & 0 deletions spec/dependencyTest/protractor_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// require('protractor') equivalent is requiring ptor
let protractor = require('../../built/main');

// the webdriver stuff we are exposing externally


describe('require(\'protractor\')', () => {

describe('exported protractor classes', () => {
it('should be defined', () => {
var protractorClasses = ['Browser', 'ElementFinder', 'ElementArrayFinder',
'ProtractorBy', 'ProtractorExpectedConditions'];
for (var pos in protractorClasses) {
var property = protractorClasses[pos];
expect(typeof protractor[property]).toEqual('function');
}
});

describe('browser class', () => {
it('should have static method defined', () => {
var staticMethod = 'wrapDriver';
expect(typeof protractor.Browser['wrapDriver']).toEqual('function');
});

it('should have static variables defined', () => {
var staticVariables = ['By', 'ExpectedConditions'];
for (var pos in staticVariables) {
var property = staticVariables[pos];
expect(typeof protractor.Browser[property]).toEqual('object');
}
});
});

});

describe('exported webdriver namespace', ()=> {
it('should have exported classes', () => {
var webdriverClasses = ['WebElement', 'ActionSequence', 'Command'];
for (var pos in webdriverClasses) {
var property = webdriverClasses[pos];
expect(typeof protractor[property]).toEqual('function');
}
});

it('should have variables defined', () => {
var webdriverObjects = ['Key', 'CommandName'];
for (var pos in webdriverObjects) {
var property = webdriverObjects[pos];
expect(typeof protractor[property]).toEqual('object');
}
});

describe('promise namespace', () => {
it('should have functions defined (spot check)', () => {
var promiseFunctions = ['Promise', 'defer', 'delayed', 'createFlow',
'controlFlow', 'all', 'fulfilled', 'filter', 'when' ]
for (var pos in promiseFunctions) {
var property = promiseFunctions[pos];
expect(typeof protractor.promise[property]).toEqual('function');
}
});
});
});
});

0 comments on commit 2a3a0dc

Please sign in to comment.