Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does Jasmine 'expect' waits for a protractor promise to resolve

I'm working on e2e testing. I have a confirm pop which doesnt exist on the page till I click a button. Once the confirm popup is create I get the text from an element there.

After that I click on OK button which causes the confirm popup to be delete from the DOM and also add a new element to the DOM with the value i got the text earlier.

the problem is, because getText() returns a promise, by the time I do the comparison the first element is not present on the screen and the test fails.

if I do expect while the confirm popup on the screen I can see the text of the confirm popup element.

how does Jasmine expect() resolve the promise?

thanks in advance

like image 401
Gal Ziv Avatar asked Jan 08 '15 09:01

Gal Ziv


2 Answers

Something like this?

element(by.id('dangerous-activity')).click().then(function () {
  element(by.id('confirmation-text')).getText().then(function (textToConfirm) {
    element(by.id('confirm-button')).click().then(function () {
      element(by.id('new-element')).getText().then(function (newText)) {
        expect(newText).toBe(textToConfirm);
      });
    });
  });
});

Here all promises are explicitly resolved, so Jasmine does not need to resolve any promise anymore.

You can let expect resolve the new-element promise, replacing the last two lines by:

    ....
      expect(element(by.id('new-element')).getText()).toBe(textToConfirm);
    ....

But you cannot get the textToConfirm in the same expectation, since it is gone by then as you indicated.

like image 159
avandeursen Avatar answered Sep 30 '22 23:09

avandeursen


This should be the simplest way to do what you want:

$('#open-popup').click();
var textToConfirm = $('#popup-text').getText();
$('#confirm-button').click();
var newText = $('#new-element').getText();
expect(newText).toBe(textToConfirm);

Note this will not work:

$('#open-popup').click();
var textToConfirmElement = $('#popup-text');
$('#confirm-button').click();
var newText = $('#new-element').getText();
expect(newText).toBe(textToConfirmElement.getText());

because here you get the text after the popup is already closed.

like image 42
hankduan Avatar answered Sep 30 '22 22:09

hankduan