5

I upgrade my Jasmine 1.3 to 2.0 so I added a custom matcher to check css is present.Below is the code to check the matcher

hasClass = function(actual,expected){
    return actual.getAttribute('class').then(function (classes) {
            return classes.split(' ').indexOf(expected) !== -1;
        });
}   

But when I upgrade to Jasmine 2 then promise throws error by protactor as it expect return but below is async process

hasClass = function(){
     return compare: function(actual,expected){
        return actual.getAttribute('class').then(function (classes) {
                return {pass: classes.split(' ').indexOf(expected) !== -1};
            });
    }
}

How can I test class is present in element I don't want to use jasmine-jquery??

Arpit
  • 159
  • 8
  • Just wonder why do you need hasClass? I also used some time ago, but then I realized the goal of Protractor E2E tests is to test functionality not the internal implementation. So, e.g., the div can still have this class, but does not work properly, and then there is no need to check if it has the class or not. – Igor Shubovych May 26 '15 at 13:40
  • @IgorShubovych it depend let say you click something and you want to show other div then how you test it aur let say same div have success color and failure color then how you test in different scenario – Arpit May 27 '15 at 03:58
  • expect(field.getCssValue('border-color')).toBe('rgb(242, 88, 5)'); – Igor Shubovych May 27 '15 at 09:16
  • does it check external css properties also?? – Arpit May 27 '15 at 11:09

1 Answers1

6

The pass should be a promise, not resolved in one. Try to place this in your beforeEach:

this.addMatchers({
    hasClass: function() {
        return {
            compare: function(actual, expected) {
                return {
                    pass: actual.getAttribute('class').then(function(classes) {
                        return classes.split(' ').indexOf(expected) !== -1;
                    })
                };
            }
        };
    }
});
Delian Mitankin
  • 3,691
  • 26
  • 24
  • I've been digging through the Jasmine 2 code, and I don't see where it handles Promises. Instead, it just seems to examine the overall truthiness of "pass" (and a Promise object will always seem true, regardless of how it actually resolves/fails). Are you sure this works as expected, rather than always appearing to pass? – Brian Oct 12 '15 at 20:18
  • Protractor overrides Jasmine for this one to work. The code you are looking for is in [jasminewd](https://github.com/angular/jasminewd/tree/jasminewd2) – Delian Mitankin Oct 13 '15 at 07:44