Skip to content

Commit 3e900a0

Browse files
committed
refactored Protractor helper
1 parent c101f28 commit 3e900a0

File tree

6 files changed

+46
-32
lines changed

6 files changed

+46
-32
lines changed

examples/yahoo_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Feature('Yahoo test');
22

33
Scenario('Nightmare basic test', (I) => {
4-
I.amOnPage('http://yahoo.com');
4+
I.amOnPage('http://127.0.0.1:8000');
5+
pause();
56
I.fillField('p', 'github nightmare');
67
I.click('Search Web');
78
I.waitForElement('#main', 2);

lib/helper/Protractor.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
2-
let By, EC, Runner;
2+
let By;
3+
let EC;
4+
let Runner;
5+
let Key;
36

47
const requireg = require('requireg');
58
const Helper = require('../helper');
@@ -108,19 +111,13 @@ class Protractor extends Helper {
108111
this.options.waitForTimeout /= 1000; // convert to seconds
109112
}
110113

111-
_init() {
114+
async _init() {
112115
Runner = requireg('protractor/built/runner').Runner;
113116
By = requireg('protractor').ProtractorBy;
114-
this.isProtractor5 = !requireg('protractor').wrapDriver;
117+
Key = requireg('protractor').Key;
118+
115119
this.context = this.options.rootElement;
116-
try {
117-
// get selenium-webdriver
118-
this.webdriver = requireg('selenium-webdriver');
119-
} catch (e) {
120-
// maybe it is installed as protractor dependency?
121-
this.webdriver = requireg('protractor/node_modules/selenium-webdriver');
122-
}
123-
return Promise.resolve(Runner);
120+
return Promise.resolve();
124121
}
125122

126123
static _checkRequirements() {
@@ -237,6 +234,7 @@ class Protractor extends Helper {
237234
}
238235

239236
_withinEnd() {
237+
if (!Object.keys(withinStore).length) return;
240238
this.browser.findElement = withinStore.elFn;
241239
this.browser.findElements = withinStore.elsFn;
242240
withinStore = {};
@@ -308,7 +306,9 @@ class Protractor extends Helper {
308306
async click(locator, context = null) {
309307
let matcher = this.browser;
310308
if (context) {
311-
matcher = await this._locate(context);
309+
let els = await this._locate(context, true);
310+
assertElementExists(els, context);
311+
matcher = els[0];
312312
}
313313
let el = await findClickable.call(this, matcher, locator);
314314
return el.click();
@@ -320,7 +320,7 @@ class Protractor extends Helper {
320320
async doubleClick(locator, context = null) {
321321
let matcher = this.browser;
322322
if (context) {
323-
let els = await this._locate(guessLocator(locator) || global.by.css(locator));
323+
let els = await this._locate(context, true);
324324
assertElementExists(els, context);
325325
matcher = els[0];
326326
}
@@ -336,7 +336,7 @@ class Protractor extends Helper {
336336
if (offsetX !== null || offsetY !== null) {
337337
offset = {x: offsetX, y: offsetY};
338338
}
339-
let els = await this._locate(guessLocator(locator) || global.by.css(locator));
339+
let els = await this._locate(locator, true);
340340
assertElementExists(els, locator);
341341
return this.browser.actions().mouseMove(els[0], offset).perform();
342342
}
@@ -396,13 +396,13 @@ class Protractor extends Helper {
396396
async pressKey(key) {
397397
let modifier;
398398
if (Array.isArray(key) && ~['Control', 'Command', 'Shift', 'Alt'].indexOf(key[0])) {
399-
modifier = this.browser.Key[key[0].toUpperCase()];
399+
modifier = Key[key[0].toUpperCase()];
400400
key = key[1];
401401
}
402402

403403
// guess special key in Selenium Webdriver list
404-
if (this.browser.Key[key.toUpperCase()]) {
405-
key = this.browser.Key[key.toUpperCase()];
404+
if (Key[key.toUpperCase()]) {
405+
key = Key[key.toUpperCase()];
406406
}
407407

408408
let action = this.browser.actions();
@@ -467,7 +467,9 @@ class Protractor extends Helper {
467467
async checkOption(field, context = null) {
468468
let matcher = this.browser;
469469
if (context) {
470-
matcher = await matcher.findElement(guessLocator(context) || global.by.css(context));
470+
let els = await this._locate(context, true);
471+
assertElementExists(els, context);
472+
matcher = els[0];
471473
}
472474
let els = await findCheckable(matcher, field);
473475
assertElementExists(els, field, "Checkbox or radio");
@@ -887,7 +889,7 @@ async function findCheckable(client, locator) {
887889
if (els.length) {
888890
return els;
889891
}
890-
return await client.findElements(global.by.css(locator));
892+
return client.findElements(global.by.css(locator));
891893
}
892894

893895
async function findFields(client, locator) {
@@ -970,7 +972,6 @@ async function findClickable(matcher, locator) {
970972
return els[0];
971973
}
972974
let literal = xpathLocator.literal(locator.value);
973-
974975
let narrowLocator = Locator.clickable.narrow(literal);
975976
let els = await matcher.findElements(global.by.xpath(narrowLocator));
976977
if (els.length) {

lib/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ module.exports.test = {
167167

168168
submittedData: function (dataFile) {
169169
return function (key) {
170+
if (!fs.existsSync(dataFile)) {
171+
var waitTill = new Date(new Date().getTime() + 1 * 1000); // wait for one sec for file to be created
172+
while(waitTill > new Date()){}
173+
}
174+
if (!fs.existsSync(dataFile)) {
175+
throw new Error('Data file was not created in time');
176+
}
170177
var data = JSON.parse(fs.readFileSync(dataFile, 'utf8'));
171178
if (key) {
172179
return data.form[key];

test/helper/Nightmare_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Nightmare', function () {
3030
show: false
3131
});
3232
I._init();
33-
I._beforeSuite();
33+
return I._beforeSuite();
3434
});
3535

3636
beforeEach(function() {

test/helper/ProtractorWeb_test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ describe('Protractor-NonAngular', function () {
3939
});
4040
});
4141

42-
webApiTests.init({ I, site_url});
4342

4443
beforeEach(function() {
44+
webApiTests.init({ I, site_url});
4545
return I._before();
4646
});
4747

48-
after(function() {
48+
afterEach(function() {
4949
return I._after();
5050
});
5151

52+
after(function() {
53+
return I._afterSuite();
54+
});
55+
5256
describe('open page : #amOnPage', () => {
5357
it('should open main page of configured site', function*() {
5458
yield I.amOnPage('/');
@@ -115,8 +119,8 @@ describe('Protractor-NonAngular', function () {
115119
});
116120

117121
describe('SmartWait', () => {
118-
before(() => I.options.smartWait = 3000);
119-
after(() => I.options.smartWait = 0);
122+
beforeEach(() => I.options.smartWait = 3000);
123+
afterEach(() => I.options.smartWait = 0);
120124

121125
it('should wait for element to appear', () => {
122126
return I.amOnPage('/form/wait_element')

test/helper/webapi.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ module.exports.tests = function() {
166166
yield I.amOnPage('/form/checkbox');
167167
yield I.checkOption('#checkin');
168168
yield I.click('Submit');
169+
yield I.wait(1);
169170
return assert.equal(formContents('terms'), 'agree');
170171
});
171172

@@ -508,7 +509,7 @@ module.exports.tests = function() {
508509
.then(() => assert.ok(fileExists(path.join(global.output_dir, `screenshot_full_${+sec}`)), null, 'file does not exists'));
509510
});
510511

511-
it('should create a screenshot on fail @ups', () => {
512+
it('should create a screenshot on fail @ups', () => {
512513
let sec = (new Date()).getUTCMilliseconds().toString();
513514
let test = { title: 'sw should do smth ' + sec };
514515
return I.amOnPage('/')
@@ -560,7 +561,7 @@ module.exports.tests = function() {
560561
});
561562
});
562563

563-
describe('#waitForElement', () => {
564+
describe('#waitForElement @ups', () => {
564565
it('should wait for visible element', () => {
565566
return I.amOnPage('/form/wait_visible')
566567
.then(() => I.dontSee('Step One Button'))
@@ -590,23 +591,23 @@ module.exports.tests = function() {
590591

591592

592593
});
593-
594+
594595
describe('#waitUntilExists', () => {
595-
596+
596597
it('should wait for an element to be removed from DOM', () => {
597598
return I.amOnPage('/spinner')
598599
.then(() => I.seeElementInDOM('.loader'))
599600
.then(() => I.waitUntilExists('.loader'))
600601
.then(() => I.dontSeeElement('.loader'))
601602
});
602-
603+
603604
it('should wait for a non-exising element to be removed from DOM', () => {
604605
return I.amOnPage('/spinner')
605606
.then(() => I.dontSeeElement('.non-existing-class'))
606607
.then(() => I.waitUntilExists('.non-existing-class'))
607608
.then(() => I.dontSeeElement('.non-existing-class'))
608609
});
609-
610+
610611
});
611612

612613
describe('within tests', () => {

0 commit comments

Comments
 (0)