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

Commit

Permalink
fix(export): export selenium-webdriver (#3433)
Browse files Browse the repository at this point in the history
- rename to ProtractorBrowser to be able to export selenium-webdriver Browser as Browser
- export all selenium-webdriver items and subfolders in ptor
- update dependency tests for selenium
- add tests when protractor is installed

closes #3427 
closes #2092
  • Loading branch information
cnishina authored Aug 4, 2016
1 parent 9182d58 commit 5f690fe
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 68 deletions.
35 changes: 14 additions & 21 deletions globals.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import {
Browser,
ProtractorBrowser,
ElementArrayFinder,
ElementFinder,
ElementHelper,
ProtractorBy,
ProtractorExpectedConditions
ProtractorExpectedConditions,
Ptor
} from 'protractor';

export interface Protractor {
browser: Browser;
element: ElementHelper;
by: ProtractorBy;
By: ProtractorBy;
$: (search: string) => ElementFinder;
$$: (search: string) => ElementArrayFinder;
ExpectedConditions: ProtractorExpectedConditions;
}
interface global {};
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'];
export let protractor: Ptor = global['protractor'];
export let browser: ProtractorBrowser = protractor.browser;
export let $: (search: string) => ElementFinder = protractor.$;
export let $$: (search: string) => ElementArrayFinder = protractor.$$;
export let element: ElementHelper = protractor.element;
export let By: ProtractorBy = protractor.By;
export let by: ProtractorBy = protractor.by;
export let wrapDriver:
(webdriver: any, baseUrl?: string, rootElement?: string,
untrackOutstandingTimeouts?: boolean) => ProtractorBrowser = protractor.wrapDriver;
export let ExpectedConditions: ProtractorExpectedConditions = protractor.ExpectedConditions;
5 changes: 3 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ gulp.task('webdriver:update', function(done) {

gulp.task('jshint', function(done) {
runSpawn(done, 'node', ['node_modules/jshint/bin/jshint', 'lib', 'spec', 'scripts',
'--exclude=lib/selenium-webdriver/**/*.js,spec/dependencyTest/*.js']);
'--exclude=lib/selenium-webdriver/**/*.js,spec/dependencyTest/*.js,' +
'spec/install/**/*.js']);
});

gulp.task('format:enforce', () => {
Expand Down Expand Up @@ -89,7 +90,7 @@ gulp.task('default',['prepublish']);
gulp.task('types', function(done) {
var folder = 'built';
var files = ['browser', 'element', 'locators', 'expectedConditions',
'config', 'plugins'];
'config', 'plugins', 'ptor'];
var outputFile = path.resolve(folder, 'index.d.ts');
var contents = '';
files.forEach(file => {
Expand Down
11 changes: 6 additions & 5 deletions lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface ElementHelper extends Function {
* @param {Browser} browser A browser instance.
* @returns {function(webdriver.Locator): ElementFinder}
*/
function buildElementHelper(browser: Browser): ElementHelper {
function buildElementHelper(browser: ProtractorBrowser): ElementHelper {
let element: ElementHelper = (locator: Locator) => {
return new ElementArrayFinder(browser).all(locator).toElementFinder_();
};
Expand All @@ -104,7 +104,7 @@ function buildElementHelper(browser: Browser): ElementHelper {
* @param {boolean=} opt_untrackOutstandingTimeouts Whether Protractor should
* stop tracking outstanding $timeouts.
*/
export class Browser extends Webdriver {
export class ProtractorBrowser extends Webdriver {
/**
* @type {ProtractorBy}
*/
Expand Down Expand Up @@ -329,7 +329,8 @@ export class Browser extends Webdriver {
* @returns {Browser} A browser instance.
*/
forkNewDriverInstance(
opt_useSameUrl?: boolean, opt_copyMockModules?: boolean): Browser {
opt_useSameUrl?: boolean,
opt_copyMockModules?: boolean): ProtractorBrowser {
return null;
}

Expand Down Expand Up @@ -1242,8 +1243,8 @@ export class Browser extends Webdriver {
*/
static wrapDriver(
webdriver: webdriver.WebDriver, baseUrl?: string, rootElement?: string,
untrackOutstandingTimeouts?: boolean): Browser {
return new Browser(
untrackOutstandingTimeouts?: boolean): ProtractorBrowser {
return new ProtractorBrowser(
webdriver, baseUrl, rootElement, untrackOutstandingTimeouts);
}
}
13 changes: 7 additions & 6 deletions lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let webdriver = require('selenium-webdriver');
let clientSideScripts = require('./clientsidescripts');

import {Logger} from './logger';
import {Browser} from './browser';
import {ProtractorBrowser} from './browser';
import {Locator} from './locators';

let logger = new Logger('element');
Expand Down Expand Up @@ -83,7 +83,7 @@ export class WebdriverWebElement {
* });
*
* @constructor
* @param {Browser} browser A browser instance.
* @param {ProtractorBrowser} browser A browser instance.
* @param {function(): Array.<webdriver.WebElement>} getWebElements A function
* that returns a list of the underlying Web Elements.
* @param {webdriver.Locator} locator The most relevant locator. It is only
Expand All @@ -97,7 +97,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
getWebElements: Function;

constructor(
public browser_: Browser, getWebElements?: Function,
public browser_: ProtractorBrowser, getWebElements?: Function,
public locator_?: any,
public actionResults_: webdriver.promise.Promise<any> = null) {
super();
Expand Down Expand Up @@ -703,7 +703,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
*
* @constructor
* @extends {webdriver.WebElement}
* @param {Browser} browser_ A browser instance.
* @param {ProtractorBrowser} browser_ A browser instance.
* @param {ElementArrayFinder} elementArrayFinder The ElementArrayFinder
* that this is branched from.
* @returns {ElementFinder}
Expand All @@ -716,7 +716,8 @@ export class ElementFinder extends WebdriverWebElement {
errorFn: Function) => webdriver.promise.Promise<any> = null;

constructor(
public browser_: Browser, elementArrayFinder: ElementArrayFinder) {
public browser_: ProtractorBrowser,
elementArrayFinder: ElementArrayFinder) {
super();
if (!elementArrayFinder) {
throw new Error('BUG: elementArrayFinder cannot be empty');
Expand Down Expand Up @@ -785,7 +786,7 @@ export class ElementFinder extends WebdriverWebElement {
}

static fromWebElement_(
browser: Browser, webElem: webdriver.WebElement,
browser: ProtractorBrowser, webElem: webdriver.WebElement,
locator: Locator): ElementFinder {
let getWebElements =
() => { return webdriver.promise.fulfilled([webElem]); };
Expand Down
2 changes: 1 addition & 1 deletion lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
* namespace.
*/

export = require('./ptor').protractor
export = require('./ptor').protractor;
64 changes: 42 additions & 22 deletions lib/ptor.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,56 @@
import {Browser, ElementHelper} from './browser';
import {ElementHelper, ProtractorBrowser} from './browser';
import {ElementArrayFinder, ElementFinder} from './element';
import {ProtractorExpectedConditions} from './expectedConditions';
import {ProtractorBy} from './locators';

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

export namespace protractor {
export class Ptor {
// 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:
browser: ProtractorBrowser;
$ = function(search: string): ElementFinder { return null; };
$$ = function(search: string): ElementArrayFinder { return null; };
element: ElementHelper;
By: ProtractorBy;
by: ProtractorBy;
wrapDriver:
(webdriver: webdriver.WebDriver, baseUrl?: string, rootElement?: string,
untrackOutstandingTimeouts?: boolean) => Browser;
export let ExpectedConditions: ProtractorExpectedConditions;
untrackOutstandingTimeouts?: boolean) => ProtractorBrowser;
ExpectedConditions: ProtractorExpectedConditions;

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

// Export selenium webdriver.
export let promise = webdriver.promise;
export let WebElement = webdriver.WebElement;
export let ActionSequence = webdriver.ActionSequence;
export let Key = webdriver.Key;
export let Command = require('selenium-webdriver/lib/command').Command;
export let CommandName = require('selenium-webdriver/lib/command').Name;
ActionSequence = webdriver.ActionSequence;
Browser = webdriver.Browser;
Builder = webdriver.Builder;
Button = webdriver.Button;
Capabilities = webdriver.Capabilities;
Capability = webdriver.Capability;
EventEmitter = webdriver.EventEmitter;
FileDetector = webdriver.FileDetector;
Key = webdriver.Key;
Session = webdriver.Session;
WebDriver = webdriver.WebDriver;
WebElement = webdriver.WebElement;
WebElementPromise = webdriver.WebElementPromise;
error = webdriver.error;
logging = webdriver.logging;
promise = webdriver.promise;
until = webdriver.until;
Command = require('selenium-webdriver/lib/command').Command;
CommandName = require('selenium-webdriver/lib/command').Name;
utils = {
firefox: require('selenium-webdriver/firefox'),
http: require('selenium-webdriver/http'),
remote: require('selenium-webdriver/remote')
}
}

export var protractor = new Ptor();
12 changes: 6 additions & 6 deletions lib/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {EventEmitter} from 'events';
import * as q from 'q';
import * as util from 'util';

import {Browser} from './browser';
import {ProtractorBrowser} from './browser';
import {Config} from './config';
import {AttachSession, BrowserStack, Direct, Hosted, Local, Mock, Sauce} from './driverProviders';
import {DriverProvider} from './driverProviders';
Expand Down Expand Up @@ -151,15 +151,15 @@ export class Runner extends EventEmitter {
* Sets up convenience globals for test specs
* @private
*/
setupGlobals_(browser_: Browser) {
setupGlobals_(browser_: ProtractorBrowser) {
// Keep $, $$, element, and by/By under the global protractor namespace
protractor.browser = browser_;
protractor.$ = browser_.$;
protractor.$$ = browser_.$$;
protractor.element = browser_.element;
protractor.by = protractor.By = Browser.By;
protractor.wrapDriver = Browser.wrapDriver;
protractor.ExpectedConditions = Browser.ExpectedConditions;
protractor.by = protractor.By = ProtractorBrowser.By;
protractor.wrapDriver = ProtractorBrowser.wrapDriver;
protractor.ExpectedConditions = ProtractorBrowser.ExpectedConditions;

if (!this.config_.noGlobals) {
// Export protractor to the global namespace to be used in tests.
Expand Down Expand Up @@ -197,7 +197,7 @@ export class Runner extends EventEmitter {
var config = this.config_;
var driver = this.driverprovider_.getNewDriver();

var browser_ = Browser.wrapDriver(
var browser_ = ProtractorBrowser.wrapDriver(
driver, config.baseUrl, config.rootElement,
config.untrackOutstandingTimeouts);

Expand Down
3 changes: 2 additions & 1 deletion scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ var passingTests = [
// Unit tests
'node node_modules/jasmine/bin/jasmine.js JASMINE_CONFIG_PATH=scripts/unit_test.json',
// Dependency tests
'node node_modules/jasmine/bin/jasmine.js JASMINE_CONFIG_PATH=scripts/dependency_test.json'
'node node_modules/jasmine/bin/jasmine.js JASMINE_CONFIG_PATH=scripts/dependency_test.json',
// Typings tests
'node spec/install/test.js'
// FIX THIS: 'node scripts/typings_tests/test_typings.js'
];

Expand Down
20 changes: 17 additions & 3 deletions spec/dependencyTest/protractor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,39 @@ describe('require(\'protractor\')', () => {

describe('exported protractor classes', () => {
it('should be defined', () => {
var protractorClasses = ['Browser', 'ElementFinder', 'ElementArrayFinder',
var protractorClasses = ['ProtractorBrowser', 'ElementFinder', 'ElementArrayFinder',
'ProtractorBy', 'ProtractorExpectedConditions'];
for (var pos in protractorClasses) {
var property = protractorClasses[pos];
expect(typeof protractor[property]).toEqual('function');
}
});
var seleniumClasses = ['ActionSequence', 'Browser', 'Builder', 'Button',
'Capabilities', 'Capability', 'EventEmitter', 'FileDetector', 'Key',
'Session', 'WebDriver', 'WebElement', 'WebElementPromise', 'Command',
'CommandName'];
for (var pos in seleniumClasses) {
var propertyObj = seleniumClasses[pos];
it('should have selenium-webdriver defined: ' + propertyObj, () => {
expect(typeof protractor[propertyObj]).toEqual('object');
});
}

it('should have selenium-webdriver promise.Promise', function() {
expect(typeof protractor['promise']['Promise']).toEqual('function');
});

describe('browser class', () => {
it('should have static method defined', () => {
var staticMethod = 'wrapDriver';
expect(typeof protractor.Browser['wrapDriver']).toEqual('function');
expect(typeof protractor.ProtractorBrowser['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');
expect(typeof protractor.ProtractorBrowser[property]).toEqual('object');
}
});
});
Expand Down
4 changes: 4 additions & 0 deletions spec/install/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
typings
conf.js
typescript_spec.js
7 changes: 7 additions & 0 deletions spec/install/conf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Config} from 'protractor';

export let config: Config = {
mockSelenium: true,
specs: ['*_spec.js'],
framework: 'jasmine'
}
43 changes: 43 additions & 0 deletions spec/install/javascript_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
describe('javascript', function () {
it('should have global objects that match the protractor namespace', function () {
expect(protractor.browser === browser).toBeTruthy();
expect(protractor.by === by).toBeTruthy();
expect(protractor.By === By).toBeTruthy();
expect(protractor.$ === $).toBeTruthy();
expect(protractor.$$ === $$).toBeTruthy();
expect(protractor.ExpectedConditions === ExpectedConditions).toBeTruthy();
});
it('should have selenium-webdriver components for the protractor namespace', function () {
expect(typeof protractor.promise.all).toEqual('function');
expect(typeof protractor.promise.defer).toEqual('function');
expect(typeof protractor.promise.Promise).toEqual('function');
expect(typeof protractor.ActionSequence).toEqual('function');
expect(typeof protractor.Browser).toEqual('object');
expect(typeof protractor.Builder).toEqual('function');
expect(typeof protractor.Capabilities).toEqual('function');
expect(typeof protractor.Capability).toEqual('object');
expect(typeof protractor.EventEmitter).toEqual('function');
expect(typeof protractor.FileDetector).toEqual('function');
expect(typeof protractor.Key).toEqual('object');
expect(typeof protractor.Session).toEqual('function');
expect(typeof protractor.WebDriver).toEqual('function');
expect(typeof protractor.WebElement).toEqual('function');
expect(typeof protractor.WebElementPromise).toEqual('function');
expect(typeof protractor.error).toEqual('object');
expect(typeof protractor.logging).toEqual('object');
expect(typeof protractor.promise).toEqual('object');
expect(typeof protractor.until).toEqual('object');
expect(typeof protractor.Command).toEqual('function');
expect(typeof protractor.CommandName).toEqual('object');
expect(typeof protractor.utils.firefox).toEqual('object');
expect(typeof protractor.utils.http).toEqual('object');
expect(typeof protractor.utils.remote).toEqual('object');
});
it('should have protractor class definitions', function () {
expect(typeof protractor.ProtractorBrowser).toBe('function');
expect(typeof protractor.ElementFinder).toBe('function');
expect(typeof protractor.ElementArrayFinder).toBe('function');
expect(typeof protractor.ProtractorBy).toBe('function');
expect(typeof protractor.ProtractorExpectedConditions).toBe('function');
});
});
Loading

0 comments on commit 5f690fe

Please sign in to comment.