Cannot use 'should' assertion style with mocha / chai / chai-as-promised
I tried to use myElement.getText().should.eventually.equal("foo") as in the chai-as-promised manual, but got TypeError: Cannot read property 'eventually' of undefined. This is not a problem with chai-as-promised either, because myElement.getText().should.equal("foo") failed with a similar error. Using expect works as expected, though.
var chai = require('chai');
chai.use(require('chai-as-promised'));
var should = chai.should();
describe('Home', function() {
it('should say hello', function() {
browser.get('/');
$('h1').getText().should.eventually.equal('Hello, world');
});
});
I've encountered the same.
Mocha support is slightly limited - I'd suggest sticking with expect for now :)
If someone has time to look into how should adds itself as a property to everything and why that doesn't work with webdriver promises, please go ahead!
@juliemr I came across the same issue as well. Looking at the code, should adds itself to Object.prototype
https://github.com/chaijs/chai/blob/master/lib/chai/interface/should.js#L35
webdriver.promise.Promise doesn't appear to inherit from Object. It's acting like it comes from a different global context.
(new webdriver.promise.Promise) instanceof Object === false
This isn't an answer, but hopefully it will give someone direction.
Thanks @gabejohnson that got me on the right track. This works:
var chai = require('chai');
var should = chai.should();
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
Object.defineProperty(protractor.promise.Promise.prototype, 'should', {
get: Object.prototype.__lookupGetter__('should'),
set: Object.prototype.__lookupSetter__('should')
});
describe('home page', function() {
beforeEach(function() {
browser.ignoreSynchronization = true;
});
it('should get home page title', function() {
browser.get('http://github.com');
browser.getTitle().should.eventually.equal("GitHub · Build software better, together.");
});
});
I think a check in Protractor to see if Object has been extended with should would be nice - then Protractor can add that tidibit to extend Promise.
It breaks the same way in cucumber framework as well using protractor
Btw, I try codes from @juliemr and it works.
I'd suggest
Object.defineProperty(
protractor.promise.Promise.prototype,
'should',
Object.getOwnPropertyDescriptor(Object.prototype, 'should')
);
@domenic :+1: works like a charm and it's a bit cleaner than the first version posted here
@domenic I get the following error when i'm trying to use the code. Can you plz help ? : )
TypeError: Property description must be an object: undefined
Stacktrace:
TypeError: Property description must be an object: undefined
at Function.defineProperty (native)
at Object.
@vladei I received a similar error until I confirmed the following as the correct order:
var chai = require('chai');
var should = chai.should();
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
Object.defineProperty(
protractor.promise.Promise.prototype,
'should',
Object.getOwnPropertyDescriptor(Object.prototype, 'should')
);
Note that as of protractor 4.0.0, the workaround above no longer seems to be required (partly due to https://github.com/angular/protractor/pull/3214).
Protractor no longer exposes a Promise object of it's own, and instead defers (no pun intended) to the ManagedPromise object provided by selenium-webdriver.
(Note: if you need to work directly with Promises in protractor 4.0.0, such as protractor.promise.all(), you now need to use the WebDriver promise directly), e.g.
var promise = require('selenium-webdriver').promise;
promise.all([promise1, promise2, promise3])
It would seem that the WebDriver promise inherits from Object.prototype, so initializing chai / chai-as-promised is enough to bestow a should property onto the Promise object, e.g.
// protractor.conf.js
onPrepare() {
// Load chai assertions
const chai = require("chai");
// Load chai-as-promised support
chai.use(require("chai-as-promised"));
// Initialise should API (attaches as a property on Object)
chai.should();
}
Sweet, closing as fixed.