dequelabs / axe-webdriverjs

Provides a chainable axe API for Selenium's WebDriverJS and automatically injects into all frames.
Mozilla Public License 2.0
130 stars 46 forks source link

Use within Jasmine -- cannot figure out how to use custom matcher #14

Closed atwright147 closed 6 years ago

atwright147 commented 8 years ago

Hey,

I am trying to use AxeBuilder in Jasmine (with Protractor) but I cannot figure out how to deal with it's async nature i.e. is there a promise I can use?

The following is my custom matcher but Jasmine always sees result.pass as undefined:

beforeAll(function() {

    var customMatchers = {
        toBeAccessible: function(util) {
            var stringify = true;
            return {
                compare: function(actual, expected) {
                    if (expected === undefined) {
                        expected = 0;
                    }

                    var result = {};
                    AxeBuilder(browser.driver).analyze(function(results) {  // tried returning AxeBuilder with various return within it
                        if (util.equals(results.violations.length, expected)) {
                            result.pass = true;
                            result.message = 'Selection was accessible.';
                        } else {
                            result.pass = false;
                            result.message = (stringify === true) ? JSON.stringify(results.violations, null, 4) : results.violations;
                        }
                        return result;  // tried returning here
                    });
                    // return result;  // tried returning here too
                }
            };
        }
    };
    jasmine.addMatchers(customMatchers);

});

aXe is returning accurate results but it just happens too late for Jasmine to log it as passed or failed correctly (results is always empty at that stage).

Any ideas?

Andy

dylanb commented 8 years ago

@atwright147 there is a way to use it but I do not know of a way to write an async Jasmine matcher. Also couldn't find any information on the Interwebs http://stackoverflow.com/questions/37611672/jasmine-custom-asynchronous-matcher?rq=1

dylanb commented 8 years ago

you could try to write something similar to this https://github.com/bvaughn/jasmine-es6-promise-matchers

marcysutton commented 8 years ago

I got axe-webdriverjs to work with a custom matcher, the trick was to set it up before calling for results. https://github.com/marcysutton/axe-webdriverjs-demo/blob/hfa-demo/spec/test.js

dylanb commented 8 years ago

that solution works but results in tests being part of the before which is probably mostly not what would be required

darlanalves commented 6 years ago

Hey guys!

Found this thread by accident... I'm maintaining jasmine-es6-promise-matchers. Indeed for async matchers you need to return { pass: true } in a matcher and later on either call done or done.fail to flag the async results of a test spec.

I found this issue while searching for a better syntax for async matchers 😄

Blackbaud-SteveBrush commented 6 years ago

I answered a similar question on StackOverflow; not sure if it's the best, but it worked: https://stackoverflow.com/a/50070461/6178885