javascript - Is it possible to resolve promise within the 'executeScript'? - Stack Overflow

I am trying to write my first test using Protractor+Jasmine for my non Angular application. I need call

I am trying to write my first test using Protractor+Jasmine for my non Angular application.

I need call API function of my app global instance, get result and pare it in test. One of passed in parameters for this function is a callback which is called as soon as data are ready. This function is executed some time depend on configuration of app.

I tried to resolve promise object inside this callback function and handle it in test. This is a simplified version of my code and it also doesn't work. Looks like script arguments[0].fulfill("Some data"); is never executed because test was failed by timeout with message:

timed out after 10000msec waiting for spec to plete

describe('Text', function() {
    it('should be displayed on stage with set value', function() {
        var deferred = protractor.promise.defer();
        var promise = deferred.promise;

        promise.then(function (data) {
            console.log(data);
        });

        browser.driver.executeScript('arguments[0].fulfill("Some data");', deferred);
    });
});

Is it at all possible to resolve (fulfill) a promise object inside context of function executeScript()? Are there other ways to handle this issue?

UPD: This code works for me. Thanks!

describe('Text', function() {
    it('should be displayed on stage with set value', function() {
        var deferred = protractor.promise.defer();

        browser.driver.executeAsyncScript(function () {
            var callback = arguments[arguments.length - 1];

            MyApp.apiFunction({
                callback: function (callbackParams) {
                    callback(callbackParams);
                }
            });
        }, function (data) { // Callback
            deferred.fulfill(data);
        }).then(function (result) {
            // Do what you need with data...
            console.log('Result: ', result);
        });
    });
});

I am trying to write my first test using Protractor+Jasmine for my non Angular application.

I need call API function of my app global instance, get result and pare it in test. One of passed in parameters for this function is a callback which is called as soon as data are ready. This function is executed some time depend on configuration of app.

I tried to resolve promise object inside this callback function and handle it in test. This is a simplified version of my code and it also doesn't work. Looks like script arguments[0].fulfill("Some data"); is never executed because test was failed by timeout with message:

timed out after 10000msec waiting for spec to plete

describe('Text', function() {
    it('should be displayed on stage with set value', function() {
        var deferred = protractor.promise.defer();
        var promise = deferred.promise;

        promise.then(function (data) {
            console.log(data);
        });

        browser.driver.executeScript('arguments[0].fulfill("Some data");', deferred);
    });
});

Is it at all possible to resolve (fulfill) a promise object inside context of function executeScript()? Are there other ways to handle this issue?

UPD: This code works for me. Thanks!

describe('Text', function() {
    it('should be displayed on stage with set value', function() {
        var deferred = protractor.promise.defer();

        browser.driver.executeAsyncScript(function () {
            var callback = arguments[arguments.length - 1];

            MyApp.apiFunction({
                callback: function (callbackParams) {
                    callback(callbackParams);
                }
            });
        }, function (data) { // Callback
            deferred.fulfill(data);
        }).then(function (result) {
            // Do what you need with data...
            console.log('Result: ', result);
        });
    });
});
Share Improve this question edited Jun 2, 2015 at 7:49 Vrednost asked Jun 1, 2015 at 14:47 VrednostVrednost 531 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

executeAsyncScript() is specifically what you need.

Quoting @hankduan from the Understanding execute async script in Selenium topic:

use executeAsyncScript when you care about a return value in a calling script, but that return value won't be available immediately. This is especially necessary if you can't poll for the result, but must get the result using a callback or promise (which you must translate to callback yourself).

Jasmine has an async resole called done So if you would pass it as deferred than I think it should work

 it('should be displayed on stage with set value', function(done) {
 browser.driver.executeScript('arguments[0].fulfill("Some data");', function()

    {
 Object.defineProperty(this, "promise", {
          get: function () { done() },
          enumerable: true
      });


done()
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744387329a4571732.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信