aysnc jasmine

90 views
Skip to first unread message

Pankaj Kumar

unread,
Sep 13, 2016, 10:03:29 AM9/13/16
to Jasmine
Hi All,

I am using async calls in jasmine test using done() still my expects are always getting passed.


getDetails: {
   value: functoin(args,cb) {
       return browser.controlFlow().execute(function() {
               return Q.ninvoke(browser,'getDetails',args).then(function() {
                   //processing 
                   var data = ...
                   cb(data);
               })
       })
    }
}


getCount: {
    value: function(cb) {
           return this.getDetails(args,function() {
                   //processing
                   var data = ...
                   cb(data); 
           })
    }
}

......spec.........................

it('test',function(done) {
      instance.getCount(function() {      
            expect(element(by.css(".input2")).isDisplayed()).toBe(true);
            done();
      })
})


the above expect gets passed everytime even if the element is not present on the page. 


Charles Hansen

unread,
Sep 14, 2016, 10:59:13 AM9/14/16
to jasmi...@googlegroups.com
Your jasmine-based code looks correct. It looks like you are driving a web browser in another process (like Selenium), I have frequently run into situations when I thought the element was not present in the page, but my selenium driver thought the element did exist. I'd look into that first. If you do not trust the jasmine code, include something like 'expect(true).toBe(false);' right before the 'done()' and make sure the test fails in that case.

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at https://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Pankaj Kumar

unread,
Sep 20, 2016, 7:19:44 AM9/20/16
to Jasmine
Hi Charles,

Thanks for the quick response.
I tried the way you mentioned but it seems that when I am trying yo use promise in expect inside callback then it always evaluated to true.

e.g

it('test',function(done) {
      expect(element(by.css(".input2")).isDisplayed()).toBe(true);
      instance.getCount(function() {      
            expect(element(by.css(".input2")).isDisplayed()).toBe(true);
            done();
      })
})

The expectation outside of the getCount gives me correct result whereas the expect inside getCount always get passed.
I checked jasmine based code and it's working fine. 
Am i missing something ?
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.

Charles Hansen

unread,
Sep 20, 2016, 11:38:15 AM9/20/16
to jasmi...@googlegroups.com
The only real difference is that the expect inside the callback is evaluated at a later time. I don't know what the `element` or `isDisplayed` functions do, but checking for visibility is often difficult in test. Maybe input2 is always rendered, but at first render there is something on top of it that goes away later? You may try console logging document.querySelectorAll('.input2') in both cases. If you are not running integration tests here, you may also try jasmine_dom_matchers `toBeVisible` or `toExist`. Hope that helps.

To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.

Pankaj Kumar

unread,
Sep 20, 2016, 1:16:34 PM9/20/16
to Jasmine
Basically I am trying to do similar stuff as below

it('test',function(done) {
    expect(element(by.css(".action")).getText()).toBe('false'); 
    setTimeout(function() {
        expect(element(by.css(".action")).getText()).toBe('false');
        done();
    },4000);
});

In expect statements we can have any sort of promise (in above example I am trying to get the text of the element -> using protractor) but the promise which is mentioned in the expect statement is not getting resolved even though we have written done after the expect and hence its getting passed every time.

Gregg Van Hove

unread,
Sep 30, 2016, 5:09:01 PM9/30/16
to jasmi...@googlegroups.com
I think you may be running into some issues with the implicit promises in Protractor and how they interact with your own async code. I've seen issues like this before. You might go looking into some of the protractor docs and see if they have any other insights for you.


To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages