Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix work with promise chains #568

Merged
merged 33 commits into from Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/actor.js
Expand Up @@ -61,10 +61,17 @@ function recordStep(step, args) {
val = step.run.apply(step, args);
});

// ensure step result is passed to next step
recorder.add('return step result', () => val);

//return execution time for logging
recorder.add(`return execution time`, () => {
output.stepExecutionTime(step, global.suiteDetails);
return val;
});

// run async after step hooks
event.emit(event.step.after, step);

// ensure step result is passed to next step
recorder.add('return step result', () => val);
return recorder.promise();
}
1 change: 1 addition & 0 deletions lib/command/run-multiple.js
Expand Up @@ -93,6 +93,7 @@ function runSuite(suite, suiteConf, browser) {
// tweaking default output directories and for mochawesome
overriddenConfig = replaceValue(overriddenConfig, 'output', path.join(config.output, outputDir));
overriddenConfig = replaceValue(overriddenConfig, 'reportDir', path.join(config.output, outputDir));
overriddenConfig = replaceValue(overriddenConfig, 'mochaFile', path.join(config.output, outputDir, 'report.xml'));

// override grep param and collect all params
let params = ['run',
Expand Down
8 changes: 8 additions & 0 deletions lib/event.js
Expand Up @@ -11,6 +11,14 @@ module.exports = {
passed: 'test.passed',
failed: 'test.failed',
},
hook: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is a bit confusing. Ok, these are special events fired by a failing after and afterSuite.

Ok, let's try to clarify their context. Those methods should not fail, so if they do it is an error.
So I propose to have next events: test.after.error, and suite.after.error instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't use test.after/suite.after, because they are already defined. I suggest test.afterError, suite.afterError

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test.afterError, suite.afterError

good

afterSuite: {
failed: 'afterSuiteHook.failed',
},
after: {
failed: 'afterTestHook.failed'
}
},
suite: {
before: 'suite.before',
after: 'suite.after'
Expand Down
9 changes: 9 additions & 0 deletions lib/helper.js
Expand Up @@ -105,6 +105,15 @@ class Helper {

}

/**
* Hook executed after all tests are executed
*
* @param suite
*/
_finishTest() {

}

/**
* Access another configured helper: `this.helpers['AnotherHelper']`
*/
Expand Down
10 changes: 0 additions & 10 deletions lib/helper/Appium.js
Expand Up @@ -133,16 +133,6 @@ class Appium extends WebdriverIO {
return this.browser;
}

_after() {
if (this.options.restart) return this.browser.end();
if (this.options.desiredCapabilities.browserName) {
this.debugSection('Session', 'cleaning cookies and localStorage');
return this.browser.execute('localStorage.clear();').then(() => {
return this.browser.deleteCookie();
});
}
}

/**
* Check if an app is installed.
*
Expand Down
24 changes: 15 additions & 9 deletions lib/helper/Nightmare.js
Expand Up @@ -55,6 +55,8 @@ class Nightmare extends Helper {
js_errors: null
};

this.isRunning = false;

// override defaults with config
Object.assign(this.options, config);

Expand Down Expand Up @@ -172,7 +174,8 @@ class Nightmare extends Helper {
}

_beforeSuite() {
if (!this.options.restart) {
if (!this.options.restart && !this.isRunning) {
this.isRunning = true;
return this._startBrowser();
}
}
Expand All @@ -189,10 +192,13 @@ class Nightmare extends Helper {
return this._stopBrowser();
}
if (this.options.keepCookies) return;
return this.browser.cookies.clearAll();
return Promise.all([this.browser.cookies.clearAll(), this.executeScript('localStorage.clear();')]);
}

_afterSuite() {
}

_finishTest() {
if (!this.options.restart) {
this._stopBrowser();
}
Expand Down Expand Up @@ -822,9 +828,9 @@ class Nightmare extends Helper {
}, lctype(locator), lcval(locator));
}

/**
* {{> ../webapi/saveScreenshot }}
*/
/**
* {{> ../webapi/saveScreenshot }}
*/
saveScreenshot(fileName, fullPage = this.options.fullPageScreenshots) {
let outputFile = path.join(global.output_dir, fileName);
this.debug('Screenshot is saving to ' + outputFile);
Expand All @@ -837,9 +843,9 @@ class Nightmare extends Helper {
height: document.body.scrollHeight,
width: document.body.scrollWidth
})).then(({
width,
height
}) => {
width,
height
}) => {
this.browser.viewport(width, height);
return this.browser.screenshot(outputFile);
});
Expand Down Expand Up @@ -907,7 +913,7 @@ function *proceedSeeInField(assertType, field, value) {
let fieldVal = yield this.browser.evaluate(function (el) {
return codeceptjs.fetchElement(el).value;
}
, el);
, el);
if (tag == 'select') {
// locate option by values and check them
let text = yield this.browser.evaluate(function (el, val) {
Expand Down
34 changes: 31 additions & 3 deletions lib/helper/SeleniumWebdriver.js
Expand Up @@ -78,6 +78,9 @@ class SeleniumWebdriver extends Helper {
manualStart: false,
capabilities: {}
};

this.isRunning = false;

if (this.options.waitforTimeout) {
console.log(`waitforTimeout is deprecated in favor of waitForTimeout, please update config`);
this.options.waitForTimeout = this.options.waitforTimeout;
Expand Down Expand Up @@ -132,8 +135,9 @@ class SeleniumWebdriver extends Helper {
}

_beforeSuite() {
if (!this.options.restart && !this.options.manualStart) {
if (!this.options.restart && !this.options.manualStart && !this.isRunning) {
this.debugSection('Session', 'Starting singleton browser session');
this.isRunning = true;
return this._startBrowser();
}
}
Expand All @@ -146,13 +150,16 @@ class SeleniumWebdriver extends Helper {

_after() {
if (this.options.restart) return this.browser.quit();
if (this.options.keepCookies) return;
if (this.options.keepCookies) return Promise.all([this.browser.executeScript('localStorage.clear();'), this.closeOtherTabs()]);
// if browser should not be restarted
this.debugSection('Session', 'cleaning cookies and localStorage');
return Promise.all([this.browser.manage().deleteAllCookies(), this.browser.executeScript('localStorage.clear();')]);
return Promise.all([this.browser.manage().deleteAllCookies(), this.browser.executeScript('localStorage.clear();'), this.closeOtherTabs()]);
}

_afterSuite() {
}

_finishTest() {
if (!this.options.restart) return this.browser.quit();
}

Expand Down Expand Up @@ -667,6 +674,27 @@ class SeleniumWebdriver extends Helper {
return this.browser.manage().window().setSize(width, height);
}

/**
* Close all tabs expect for one.
*
* ```js
* I.closeOtherTabs();
* ```
*/
closeOtherTabs() {
let client = this.browser;

return client.getAllWindowHandles().then(function (handles){
for (var i = 1; i < handles.length; i++) {
client.switchTo().window(handles[i]).then(function (){
client.close();
});
}
return client.switchTo().window(handles[0]);
});
}


/**
* {{> ../webapi/wait }}
*/
Expand Down
70 changes: 48 additions & 22 deletions lib/helper/WebDriverIO.js
Expand Up @@ -210,6 +210,8 @@ class WebDriverIO extends Helper {
}
};

this.isRunning = false;

// override defaults with config
Object.assign(this.options, config);

Expand Down Expand Up @@ -256,8 +258,9 @@ class WebDriverIO extends Helper {
}

_beforeSuite() {
if (!this.options.restart && !this.options.manualStart) {
if (!this.options.restart && !this.options.manualStart && !this.isRunning) {
this.debugSection('Session', 'Starting singleton browser session');
this.isRunning = true;
return this._startBrowser();
}
}
Expand Down Expand Up @@ -295,12 +298,17 @@ class WebDriverIO extends Helper {

_after() {
if (this.options.restart) return this.browser.end();
if (this.options.keepCookies) return;
this.debugSection('Session', 'cleaning cookies and localStorage');
return Promise.all([this.browser.deleteCookie(), this.browser.execute('localStorage.clear();')]);
if (this.options.keepCookies) return Promise.all([this.browser.execute('localStorage.clear();'), this.closeOtherTabs()]);
if (this.options.desiredCapabilities.browserName) {
this.debugSection('Session', 'cleaning cookies and localStorage');
return Promise.all([this.browser.deleteCookie(), this.browser.execute('localStorage.clear();'), this.closeOtherTabs()]);
}
}

_afterSuite() {
}

_finishTest() {
if (!this.options.restart) return this.browser.end();
}

Expand Down Expand Up @@ -922,10 +930,10 @@ class WebDriverIO extends Helper {
return this.browser.saveScreenshot(outputFile);
}
return this.browser.execute(function () {
return ({
return {
height: document.body.scrollHeight,
width: document.body.scrollWidth
})
};
}).then(({
width,
height
Expand Down Expand Up @@ -1088,6 +1096,24 @@ class WebDriverIO extends Helper {
);
}

/**
* Close all tabs expect for one.
* Appium: support web test
*
* ```js
* I.closeOtherTabs();
* ```
*/
closeOtherTabs() {
let client = this.browser;
return client.getTabIds().then(function (handles) {
for (var i = 1; i < handles.length; i++) {
this.close(handles[i]);
}
return this.switchTab();
});
}

/**
* {{> ../webapi/wait }}
* Appium: support
Expand Down Expand Up @@ -1122,13 +1148,13 @@ class WebDriverIO extends Helper {
sec = sec || this.options.waitForTimeout;
context = context || 'body';
return this.browser.waitUntil(function () {
return this.getText(withStrictLocator(context)).then(function (source) {
if (Array.isArray(source)) {
return source.filter(part => part.indexOf(text) >= 0).length > 0;
}
return source.indexOf(text) >= 0;
});
}, sec * 1000)
return this.getText(withStrictLocator(context)).then(function (source) {
if (Array.isArray(source)) {
return source.filter(part => part.indexOf(text) >= 0).length > 0;
}
return source.indexOf(text) >= 0;
});
}, sec * 1000)
.catch((e) => {
if (e.type === 'WaitUntilTimeoutError') {
return proceedSee.call(this, 'assert', text, withStrictLocator(context));
Expand Down Expand Up @@ -1363,15 +1389,15 @@ function withStrictLocator(locator) {
locator.toString = () => `{${key}: '${value}'}`;

switch (key) {
case 'by':
case 'xpath':
return value;
case 'css':
return value;
case 'id':
return '#' + value;
case 'name':
return `[name="${value}"]`;
case 'by':
case 'xpath':
return value;
case 'css':
return value;
case 'id':
return '#' + value;
case 'name':
return `[name="${value}"]`;
}
}

Expand Down