Webdriverio: Advice on how to fail a test if there are javascript errors

Created on 8 Aug 2017  ·  4Comments  ·  Source: webdriverio/webdriverio

The problem

Briefly describe the issue you are experiencing (or the feature you want to see added to WebdriverIO). Tell us what you were trying to do and what happened instead. Remember, this is _not_ a place to ask questions. For that, join the Gitter chat room (Gitter)!

I tried using Gitter and no one responded.

Environment

  • WebdriverIO version: 4.8.0
  • Node.js version: 7.7.2
  • Standalone mode or wdio testrunner: wdio
  • if wdio testrunner, running synchronous or asynchronous tests: mocha
  • Additional wdio packages used (if applicable): NA

Details

Describe in more detail the problem you have been experiencing, if necessary.

I would like my test to fail if there are javascript errors on the page.

Link to Selenium/WebdriverIO logs

NA? Tests pass just fine, that is the problem.

Code To Reproduce Issue [ Good To Have ]

Please remember that, with sample code; it's easier to reproduce bug and much faster to fix it.
Maybe load a page and onload = function () { throw "fail this test!" }
Then run the test that just visits that page and see the test still passes even though javascript errors would be created in the console.

All 4 comments

This seems to me to be a very basic use case. I believe the .Net selenium suite has an option for it. In PHP and Codeception, I attach to every "Step" which is like every command in wdio be it waitFor* or getText or url() and each step I check for errors that have bubbled up to window.onerror. Is there a way to run a function at every step in wdio?

I suppose it would be possible to do something like:

window.onerror = function () {
document.write('');
}

That would cause the next step to fail. But that seems slightly unreliable.
This of course would coincide with the command:

beforeSuite(() => browser.execute(...));

Is there a way to run a function at every step in wdio?

Yes, the wdio testrunner has a afterCommand hook to do this.

Join our Gitter for these kind of questions. This is not necessary an issue.

Perfect thank you! I'll post back with my solution to make it fail the test
and maybe we can get the use case in the docs!!

Any suggestion on how to fail a test if there are errors?

I tried the chat and was ignored.

This is an issue because this is a very common use case and its entirely undocumented.

Here is what I came up with for the javascript error checker that I used on another site and Codeception, works with Angular and wdio now:

    beforeCommand: function (commandName, args) {
        if (browser.getUrl().indexOf(this.baseUrl) > -1) {
            browser.execute(function () {
                if (window.wdioErrorHandler !== window.onerror) {
                    const oldHandler = window.onerror;
                    window.windowErrors = [];
                    window.onerror = window.wdioErrorHandler = function (err) {
                        window.windowErrors.push(err + '');
                        if (typeof oldHandler === 'function') {
                            oldHandler.apply(this, arguments);
                        }
                    }
                }
            })
        }
    },

    afterCommand: function (commandName, args, result, error) {
        if (browser.getUrl().indexOf(this.baseUrl) > -1) {
            const errors = browser.execute(function () {
                return window.windowErrors;
            });
            if (errors.value !== null && errors.value.length > 0) {
                throw new Error('Javascript errors! ' + errors.value);
            }
        }
    }

How do I intentionally fail a command from the afterCommand block? I need the test to fail, but even with the throw in there it still says the test passes.

Is there somewhere in the docs we could put some examples on how to use hooks? Maybe in the debugging section?

This search doesn't really show anything helpful: https://www.google.com/search?q=wdio+hooks+example

Was this page helpful?
0 / 5 - 0 ratings