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

Commit d932ad7

Browse files
committed
chore(browser): rename protractor to browser and add a protractor namespace (#3214)
* added wrapDriver method from the browser.ts and ExpectedConditions to the protractor namespace * imported selenium webdriver ActionSequence, Key, promise, Command, and CommandName to the protractor namespace
1 parent 97c482c commit d932ad7

File tree

9 files changed

+135
-60
lines changed

9 files changed

+135
-60
lines changed

lib/protractor.ts lib/browser.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {ProtractorBy} from './locators';
88
import {ElementArrayFinder, ElementFinder, build$, build$$} from './element';
99
export {ElementFinder, ElementArrayFinder};
1010
import {Plugins} from './plugins';
11+
import {protractor} from './ptor';
1112

1213
let clientSideScripts = require('./clientsidescripts');
1314

@@ -16,8 +17,6 @@ let clientSideScripts = require('./clientsidescripts');
1617
import * as EC from './expectedConditions';
1718

1819
let webdriver = require('selenium-webdriver');
19-
let Command = require('selenium-webdriver/lib/command').Command;
20-
let CommandName = require('selenium-webdriver/lib/command').Name;
2120

2221
// jshint browser: true
2322
/* global angular */
@@ -76,14 +75,14 @@ export interface ElementHelper extends Function {
7675
* @param {Protractor} ptor
7776
* @return {function(webdriver.Locator): ElementFinder}
7877
*/
79-
function buildElementHelper(ptor: Protractor): ElementHelper {
78+
function buildElementHelper(browser: Browser): ElementHelper {
8079
let element: ElementHelper = function(locator: webdriver.Locator) {
81-
return new ElementArrayFinder(ptor).all(locator).toElementFinder_();
80+
return new ElementArrayFinder(browser).all(locator).toElementFinder_();
8281
};
8382

8483
element.all =
8584
function(locator: webdriver.Locator) {
86-
return new ElementArrayFinder(ptor).all(locator);
85+
return new ElementArrayFinder(browser).all(locator);
8786
}
8887

8988
return element;
@@ -100,7 +99,9 @@ function buildElementHelper(ptor: Protractor): ElementHelper {
10099
* @param {boolean=} opt_untrackOutstandingTimeouts Whether Protractor should
101100
* stop tracking outstanding $timeouts.
102101
*/
103-
export class Protractor {
102+
export class Browser {
103+
ExpectedConditions = new EC.ExpectedConditions();
104+
104105
/**
105106
* The wrapped webdriver instance. Use this to interact with pages that do
106107
* not contain Angular (such as a log-in screen).
@@ -121,14 +122,14 @@ export class Protractor {
121122
*
122123
* @type {function(string): ElementFinder}
123124
*/
124-
$: Function;
125+
$: (query: string) => ElementFinder;
125126

126127
/**
127128
* Shorthand function for finding arrays of elements by css.
128129
*
129130
* @type {function(string): ElementArrayFinder}
130131
*/
131-
$$: Function;
132+
$$: (query: string) => ElementArrayFinder;
132133

133134
/**
134135
* All get methods will be resolved against this base URL. Relative URLs are =
@@ -184,7 +185,7 @@ export class Protractor {
184185
*
185186
* @type {Plugins} Object containing plugin funtions from config.
186187
*/
187-
private plugins_: Plugins;
188+
plugins_: Plugins;
188189

189190
/**
190191
* The reset URL to use between page loads.
@@ -198,7 +199,7 @@ export class Protractor {
198199
* error message if Protractor fails to synchronize with Angular in time.
199200
* @private {boolean}
200201
*/
201-
private trackOutstandingTimeouts_: boolean;
202+
trackOutstandingTimeouts_: boolean;
202203

203204
/**
204205
* If set, will be the universal timeout applied to all tests run by
@@ -213,16 +214,16 @@ export class Protractor {
213214
* @type {Array<{name: string, script: function|string, args:
214215
* Array.<string>}>}
215216
*/
216-
private mockModules_: any[];
217+
mockModules_: any[];
217218

218219
/**
219220
* If specified, start a debugger server at specified port instead of repl
220221
* when running element explorer.
221222
* @private {number}
222223
*/
223-
private debuggerServerPort_: number;
224+
debuggerServerPort_: number;
224225

225-
private debuggerValidated_: boolean;
226+
debuggerValidated_: boolean;
226227

227228
// This index type allows looking up methods by name so we can do mixins.
228229
[key: string]: any;
@@ -339,7 +340,7 @@ export class Protractor {
339340
}
340341

341342
return this.driver.schedule(
342-
new Command(CommandName.EXECUTE_SCRIPT)
343+
new protractor.Command(protractor.CommandName.EXECUTE_SCRIPT)
343344
.setParameter('script', script)
344345
.setParameter('args', scriptArgs),
345346
description);
@@ -364,7 +365,7 @@ export class Protractor {
364365
script = 'return (' + script + ').apply(null, arguments);';
365366
}
366367
return this.driver.schedule(
367-
new Command(CommandName.EXECUTE_ASYNC_SCRIPT)
368+
new protractor.Command(protractor.CommandName.EXECUTE_ASYNC_SCRIPT)
368369
.setParameter('script', script)
369370
.setParameter('args', scriptArgs),
370371
description);
@@ -972,7 +973,7 @@ export class Protractor {
972973
var context: Object = {require: require};
973974
global.list = (locator: webdriver.Locator) => {
974975
/* globals browser */
975-
return browser.findElements(locator).then(
976+
return protractor.browser.findElements(locator).then(
976977
(arr: webdriver.WebElement[]) => {
977978
var found: string[] = [];
978979
for (var i = 0; i < arr.length; ++i) {
@@ -1200,7 +1201,7 @@ export class Protractor {
12001201
*/
12011202
export let wrapDriver =
12021203
(webdriver: webdriver.WebDriver, baseUrl?: string, rootElement?: string,
1203-
untrackOutstandingTimeouts?: boolean) => {
1204-
return new Protractor(
1204+
untrackOutstandingTimeouts?: boolean): Browser => {
1205+
return new Browser(
12051206
webdriver, baseUrl, rootElement, untrackOutstandingTimeouts);
12061207
};

lib/configParser.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ export class ConfigParser {
117117

118118
if (patterns) {
119119
for (let fileName of patterns) {
120-
let matches = glob.hasMagic(fileName) ? glob.sync(fileName, {cwd}) : [fileName];
120+
let matches =
121+
glob.hasMagic(fileName) ? glob.sync(fileName, {cwd}) : [fileName];
121122
if (!matches.length && !opt_omitWarnings) {
122123
logger.warn('pattern ' + fileName + ' did not match any files.');
123124
}

lib/driverProviders/driverProvider.ts

+16
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ export class DriverProvider {
7272
return deferred.promise;
7373
}
7474

75+
/**
76+
* Default update job method.
77+
* @return a promise
78+
*/
79+
updateJob(update: any): q.Promise<any> {
80+
return q.fcall(function() {});
81+
};
82+
83+
/**
84+
* Default setup environment method.
85+
* @return a promise
86+
*/
87+
setupEnv(): q.Promise<any> {
88+
return q.fcall(function() {});
89+
};
90+
7591
/**
7692
* Teardown and destroy the environment and do any associated cleanup.
7793
* Shuts down the drivers.

lib/element.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ let webdriver = require('selenium-webdriver');
22
let clientSideScripts = require('./clientsidescripts');
33

44
import {Logger} from './logger2';
5-
import {Protractor} from './protractor';
5+
import {Browser} from './browser';
66

77
let logger = new Logger('element');
88

@@ -56,7 +56,7 @@ let WEB_ELEMENT_FUNCTIONS = [
5656
* });
5757
*
5858
* @constructor
59-
* @param {Protractor} ptor A protractor instance.
59+
* @param {Browser} browser A protractor instance.
6060
* @param {function(): Array.<webdriver.WebElement>} getWebElements A function
6161
* that returns a list of the underlying Web Elements.
6262
* @param {webdriver.Locator} locator The most relevant locator. It is only
@@ -70,7 +70,7 @@ export class ElementArrayFinder {
7070
getWebElements: Function;
7171

7272
constructor(
73-
private ptor_: Protractor, getWebElements?: Function,
73+
private browser_: Browser, getWebElements?: Function,
7474
private locator_?: any, public actionResults_: webdriver.Promise = null) {
7575
this.getWebElements = getWebElements || null;
7676

@@ -96,7 +96,7 @@ export class ElementArrayFinder {
9696
// modified. (Locator can be modified by the user, but that should
9797
// rarely/never happen and it doesn't affect functionalities).
9898
return new ElementArrayFinder(
99-
this.ptor_, this.getWebElements, this.locator_, this.actionResults_);
99+
this.browser_, this.getWebElements, this.locator_, this.actionResults_);
100100
}
101101

102102
/**
@@ -132,7 +132,7 @@ export class ElementArrayFinder {
132132
* @return {ElementArrayFinder}
133133
*/
134134
all(locator: any): ElementArrayFinder {
135-
let ptor = this.ptor_;
135+
let ptor = this.browser_;
136136
let getWebElements = () => {
137137
if (this.getWebElements === null) {
138138
// This is the first time we are looking for an element
@@ -172,7 +172,7 @@ export class ElementArrayFinder {
172172
});
173173
}
174174
};
175-
return new ElementArrayFinder(this.ptor_, getWebElements, locator);
175+
return new ElementArrayFinder(this.browser_, getWebElements, locator);
176176
}
177177

178178
/**
@@ -210,7 +210,7 @@ export class ElementArrayFinder {
210210
let list =
211211
parentWebElements.map((parentWebElement: any, index: number) => {
212212
let elementFinder = ElementFinder.fromWebElement_(
213-
this.ptor_, parentWebElement, this.locator_);
213+
this.browser_, parentWebElement, this.locator_);
214214

215215
return filterFn(elementFinder, index);
216216
});
@@ -222,7 +222,7 @@ export class ElementArrayFinder {
222222
});
223223
});
224224
};
225-
return new ElementArrayFinder(this.ptor_, getWebElements, this.locator_);
225+
return new ElementArrayFinder(this.browser_, getWebElements, this.locator_);
226226
}
227227

228228
/**
@@ -268,7 +268,7 @@ export class ElementArrayFinder {
268268
return [parentWebElements[i]];
269269
});
270270
};
271-
return new ElementArrayFinder(this.ptor_, getWebElements, this.locator_)
271+
return new ElementArrayFinder(this.browser_, getWebElements, this.locator_)
272272
.toElementFinder_();
273273
}
274274

@@ -329,7 +329,7 @@ export class ElementArrayFinder {
329329
* @private
330330
*/
331331
toElementFinder_(): ElementFinder {
332-
return new ElementFinder(this.ptor_, this);
332+
return new ElementFinder(this.browser_, this);
333333
}
334334

335335
/**
@@ -399,7 +399,7 @@ export class ElementArrayFinder {
399399
throw e;
400400
});
401401
return new ElementArrayFinder(
402-
this.ptor_, this.getWebElements, this.locator_, actionResults);
402+
this.browser_, this.getWebElements, this.locator_, actionResults);
403403
}
404404

405405
/**
@@ -412,7 +412,7 @@ export class ElementArrayFinder {
412412
return this.getWebElements().then((arr: webdriver.WebElement[]) => {
413413
return arr.map((webElem: webdriver.WebElement) => {
414414
return ElementFinder.fromWebElement_(
415-
this.ptor_, webElem, this.locator_);
415+
this.browser_, webElem, this.locator_);
416416
});
417417
});
418418
}

lib/expectedConditions.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var webdriver = require('selenium-webdriver');
1+
import {protractor} from './ptor';
2+
let webdriver = require('selenium-webdriver');
23

34
/* globals browser */
45

@@ -140,7 +141,7 @@ export class ExpectedConditions {
140141
*/
141142
alertIsPresent(): Function {
142143
return () => {
143-
return browser.switchTo().alert().then(
144+
return protractor.browser.driver.switchTo().alert().then(
144145
(): boolean => { return true; },
145146
(err: any) => {
146147
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ALERT) {
@@ -237,9 +238,10 @@ export class ExpectedConditions {
237238
*/
238239
titleContains(title: string): Function {
239240
return () => {
240-
return browser.getTitle().then((actualTitle: string): boolean => {
241-
return actualTitle.indexOf(title) > -1;
242-
});
241+
return protractor.browser.driver.getTitle().then(
242+
(actualTitle: string): boolean => {
243+
return actualTitle.indexOf(title) > -1;
244+
});
243245
};
244246
}
245247

@@ -258,7 +260,7 @@ export class ExpectedConditions {
258260
*/
259261
titleIs(title: string): Function {
260262
return () => {
261-
return browser.getTitle().then(
263+
return protractor.browser.driver.getTitle().then(
262264
(actualTitle: string): boolean => { return actualTitle === title; });
263265
};
264266
}

lib/globals.d.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
// Typescript transpiling will give a warning about 'global'. This work around
2-
// is to allow protractor to set global variables. Warning message as follows:
3-
//
4-
// lib/globals.d.ts(2,19): error TS2300: Duplicate identifier 'global'.
5-
// typings/main/ambient/node/index.d.ts(33,13): error TS2300: Duplicate
6-
// identifier 'global'.
7-
declare var browser: any;
8-
declare var protractor: any;
9-
declare var $: any;
10-
declare var $$: any;
11-
declare var element: any;
12-
declare var by: any;
13-
declare var By: any;
14-
declare var DartObject: any;
15-
161
// Used for Protractor mock module loader.
172
declare namespace angular {
183
var module: Function;
@@ -35,7 +20,11 @@ declare namespace NodeJS {
3520
element: any;
3621
by: any;
3722
By: any;
23+
ExpectedConditions: any;
3824
DartObject: any;
25+
ActionSequence: any;
26+
Command: any;
27+
CommandName: any;
3928
// Helper function added by the debugger in protractor.ps
4029
list: (locator: webdriver.Locator) => string[];
4130
[key: string]: any;
@@ -45,16 +34,21 @@ declare namespace NodeJS {
4534
declare interface String { startsWith: Function; }
4635

4736
declare namespace webdriver {
37+
var error: any;
4838
class WebDriver {
4939
findElements: Function;
5040
getSession: Function;
5141
quit: Function;
5242
executeScript: Function;
5343
getCapabilities: Function;
44+
getCurrentUrl: Function;
45+
getPageSource: Function;
46+
getTitle: Function;
5447
navigate: Function;
5548
get: Function;
5649
wait: Function;
5750
schedule: Function;
51+
switchTo: Function;
5852
controlFlow: Function;
5953
static attachToSession: Function;
6054
// This index type allows looking up methods by name so we can do mixins.
@@ -87,6 +81,18 @@ declare namespace webdriver {
8781
code: number;
8882
}
8983

84+
class By {
85+
static css: (css: string) => webdriver.By;
86+
static id: (id: string) => webdriver.By;
87+
static linkText: (linkText: string) => webdriver.By;
88+
static js: (js: string) => webdriver.By;
89+
static name: (name: string) => webdriver.By;
90+
static partialLinkText: (partialLinkText: string) => webdriver.By;
91+
static tagName: (tagName: string) => webdriver.By;
92+
static xpath: (xpath: string) => webdriver.By;
93+
toString(): string;
94+
}
95+
9096
interface Locator {
9197
toString(): string;
9298
isPresent?: Function;

0 commit comments

Comments
 (0)