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

Add callback for after completion of test #135

Closed
disciplezero opened this issue Apr 9, 2014 · 12 comments
Closed

Add callback for after completion of test #135

disciplezero opened this issue Apr 9, 2014 · 12 comments

Comments

@disciplezero
Copy link
Contributor

We want to be able to run additional commands after the completion of a test.

In our case, we are informing browserStack that a test has failed.

I've implemented a rudimentary solution here: disciplezero@26c83d4

I think using a queue would be smarter and more robust, but this is sufficient for what we need. 😄

@beatfactor
Copy link
Member

what about using tearDown?

@disciplezero
Copy link
Contributor Author

Is there a way to tell if the test passed or failed?

On April 10, 2014 at 2:35:00 AM, Andrei Rusu ([email protected]) wrote:

what about using tearDown?


Reply to this email directly or view it on GitHub.

@beatfactor
Copy link
Member

No, but I can pass in the results and errors, which is probably even better.

On Thu, Apr 10, 2014 at 4:33 PM, disciplezero [email protected]:

Is there a way to tell if the test passed or failed?

On April 10, 2014 at 2:35:00 AM, Andrei Rusu ([email protected])
wrote:

what about using tearDown?

Reply to this email directly or view it on GitHub.

Reply to this email directly or view it on GitHubhttps://github.com/beatfactor/nightwatch/issues/135#issuecomment-40088657
.

@disciplezero
Copy link
Contributor Author

That would make you my favorite person ever! Seriously though, it looks like thats why I rejected using teardown, because it didn’t have test information.

-- 
Chris Egner

On April 10, 2014 at 9:33:20 AM, Chris Egner ([email protected]) wrote:

Is there a way to tell if the test passed or failed?

On April 10, 2014 at 2:35:00 AM, Andrei Rusu ([email protected]) wrote:

what about using tearDown?


Reply to this email directly or view it on GitHub.

@beatfactor
Copy link
Member

It's a bit tricky though.

Here's why: tearDown accepts an optional callback parameter to allow you to do a final async operation before closing the session. It sounds like you need this callback. At the same time it won't complain or it won't hang if you don't pass a callback. It does this by looking at the number of arguments you pass to tearDown - 0 or 1.

With this new addition that logic will become more complex because it needs to handle now 0, 1, 2 or 3 arguments. Consider all these cases:

tearDown : function() {}
tearDown : function(results, errors) {}
tearDown : function(callback) {}
tearDown : function(callback, results, errors) {}

But it would be a valuable addition. I'll look into it but I'm not sure how quickly it will be added.

@disciplezero
Copy link
Contributor Author

You’re currently doing the equivalent of:

tearDown.call(client.api), right?

As an alternative can we expose the results through the client/api?

-- 
Chris Egner

On April 10, 2014 at 10:16:04 AM, Andrei Rusu ([email protected]) wrote:

It's a bit tricky though.

Here's why: tearDown accepts an optional callback parameter to allow you to do a final async operation before closing the session. It sounds like you need this callback. At the same time it won't complain or it won't hang if you don't pass a callback. It does this by looking at the number of arguments you pass to tearDown - 0 or 1.

With this new addition that logic will become more complex because it needs to handle now 0, 1, 2 or 3 arguments. Consider all these cases:

tearDown : function() {}
tearDown : function(results, errors) {}
tearDown : function(callback) {}
tearDown : function(callback, results, errors) {}
But it would be a valuable addition. I'll look into it but I'm not sure how quickly it will be added.


Reply to this email directly or view it on GitHub.

@beatfactor
Copy link
Member

So inside your tearDown you can inspect the results and errors properties.
The results object will look like this:

{
  passed: 1,
  failed: 0,
  errors: 0,
  skipped: 0,
  tests: [ {
    message: 'test message',
    stacktrace: '',
    failure: false 
  }] 
}

The errors is just an array containing error message strings.

beatfactor added a commit that referenced this issue Apr 24, 2014
* 'master' of github.com:beatfactor/nightwatch:
  added results and errors properties on the test object available from inside tearDown to satisfy #135
  updated tests for waitForElementPresent
  added waitForConditionPollInterval also, changed the set milliseconds logic a bit and updated the api doc
  Fixed an issue where the set locateStrategy wasn't used correctly in the case of xpath
  Fixed an issue when the selenium server wasn't being stopped after a session create error
  added <no such element> to array of DO_NOT_LOG_ERRORS
  add missing semicolon
  add global timeout for waitFor commands. Fixes #129
  Standardize return - important for callbacks.
@glebpopoff
Copy link

Make sure to use this.results as tearDown won't accept anything but the callback parameter.

tearDown : function(callback) {
var data = JSON.stringify({
"passed" : (this.results.failed == 0) ? true : false,
"tags" : ["test","example"]
});

@MWaelisch
Copy link

Hi,
I just tried using nightwatch with saucelabs and got the problem that the test result is always just "Finished".
I dont really understand where the JSON object with the result is passed and
how i have to use this to set my tests to passed or failed in SaucLabs.
Could someone please give me a short example how this works?
This would really help me a lot.

Thank You :)

@glebpopoff
Copy link

Hi Michael-

Try the following:

var https = require('http');
module.exports = {
"My - Test" : function (browser) {
browser
.url("http://www.googel.com")
.waitForElementVisible('body', 1000)
browser.end();
},

after : function(browser) {
browser.end();
},
tearDown : function(callback) {
if (this.client.globals.selenium_server == 'saucelabs')
{

require('./lib/saucelabs.js').update_saucelabs(this.results.failed,
["my-tag1", "my-tag2"], this.client, https, callback);
} else
{
callback();
}
}
};

Here's what the include file (./lib/saucelabs.js) looks like:

function update_saucelabs(failed, tags, client, https, callback)
{
var data = JSON.stringify({
"passed" : (failed == 0) ? true : false,
"tags" : tags
});

var requestPath = '/rest/v1/'+ client.options.username +'/jobs/' +
client.sessionId;
try
{
console.log('Updaing SauceLabs..', requestPath);
var req = https.request({
hostname: 'saucelabs.com',
path: requestPath,
method: 'PUT',
auth : client.options.username + ':' + client.globals.access_key,
headers : {
'Content-Type': 'application/json',
'Content-Length' : data.length
}
},
function(res)
{
res.setEncoding('utf8');
//console.log('Response: ', res.statusCode,
JSON.stringify(res.headers));
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
res.on('end', function () {
console.info('Finished updating SauceLabs...');
callback();
});
});

  req.on('error', function(e)
  {
    console.log('Problem with request: ' + e.message);
  });
  req.write(data);
  req.end();

} catch (err) {
console.log('Error', err);
callback();
}
}
module.exports.update_saucelabs = update_saucelabs;

On Tue, Apr 7, 2015 at 10:13 AM, MichaelCheck [email protected]
wrote:

Hi,
I just tried using nightwatch with saucelabs and got the same problem.
I dont really understand where the JSON object with the result is passed
and
how i have to use this to set my tests to passed or failed in SaucLabs.
Could someone please give me a short example how this works?
This would really help me a lot.

Thank You :)


Reply to this email directly or view it on GitHub
https://github.com/beatfactor/nightwatch/issues/135#issuecomment-90577524
.

Gleb Popoff

@beatfactor
Copy link
Member

You should actually use afterEach instead of tearDown. See http://nightwatchjs.org/guide#asynchronous-before-each-and-after-each-

@MWaelisch
Copy link

Very cool ! Now i got it.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants