Protractor: how does `expect` work exactly?

Created on 30 Sep 2013  路  2Comments  路  Source: angular/protractor

Hi.

I have some issues with expect and would love to read more documentation or the corresponding source code. But I have some problems finding the correct file for the expect function. I would be glad if someone could point me out to the file.

I started to stub the Rest Api in the protractor test.
This works fine and now I want to assert the received requests of the stubbed api.

In my example mock is a mocked middleware in a nodejs server that works like this:

mock.onRequest({path: '/foo'}).respondWith({json: {foo: 'bar'})
// access to all requests of the first mock request. In this case `/foo`
mock.routes[0].requests
// this should be empty at the moment
// later there is a list of elements like this: {body: ..., query: ....}

//ptor.get and all the selenium stuff happens here

// end of test. I want to verify if the expected parameters have been submitted
// in this case I make it simple and just check if the correct number of calls have been made
expect(mock.routes[0].requests.length).toBe(1)

In my case this always returns that no calls have been made.
The requests to the server have been made and the mock library has working unit tests for this scenarios.
As of control-flow protractor works with promises.
My guess is that protractor goes through the all the task and executes the promises if they are the first in the queue. In this case however there is no promise and protractor just uses the value. I would also assume, that protractor is using only the value and not the reference.
This could be the reason why the requests are always empty.

My question is now. How can i handle javascript objects in my protractor tests that change there values.

question

Most helpful comment

Hi,

expect is the Jasmine expect function except that it is patched to understand promises - the way it works is that if expect encounters a promise:

expect(promise).toEqual('foo');

It will essentially replace that with:

promise.then(function(value) {
  expect(value).toEqual('foo');
}

Check out https://github.com/angular/protractor/blob/master/jasminewd/index.js#L65 to read the code.

I don't have enough context with your code to understand why expect isn't working out for you. Since your values aren't promises, it should work as it does in normal Jasmine.

All 2 comments

Hi,

expect is the Jasmine expect function except that it is patched to understand promises - the way it works is that if expect encounters a promise:

expect(promise).toEqual('foo');

It will essentially replace that with:

promise.then(function(value) {
  expect(value).toEqual('foo');
}

Check out https://github.com/angular/protractor/blob/master/jasminewd/index.js#L65 to read the code.

I don't have enough context with your code to understand why expect isn't working out for you. Since your values aren't promises, it should work as it does in normal Jasmine.

@juliemr excellent answer, thank you!

Is this also true for locators in protractor? Looking at the locator code (cssContainingText, for example), it looks like only strings should be passed in. But I was passing around the result of a getText() call, const getTextPromise, and was surprised to find it worked when I passed it into a locator.

const getTextPromise = element(by.css('span.my-fave-element')).getText();
element(by.cssContainingText('div', getTextPromise)).click();

Side note:

This is an old thread, and at this point, you can use es7 async / await syntax with SELENIUM_PROMISE_MANAGER: false in your protractor config file. This, IMO, makes understanding the code & debugging in an IDE much easier. But not everyone can upgrade to node 8 yet.

Was this page helpful?
0 / 5 - 0 ratings