Phantomjs: Waitfor support for Promise pattern

Created on 4 Apr 2016  路  8Comments  路  Source: ariya/phantomjs

When phantom is used as node module, the
https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js

does not work when page.evaluate is a Promise like:

sitepage.evaluate(function() {
                return document.querySelectorAll('.content > ul');
              }).then(function(html) {
                  sitepage.close();
                  phInstance.exit();
                  if(done) done( pageContent );
              });

so this will not work:

waitFor(
                    function () {
                       return sitepage.evaluate(function() {
                return document.querySelectorAll('.content > ul');
              }).then(function(html) {
                  sitepage.close();
                  phInstance.exit();
                  if(done) done( pageContent );
              });
                    },
                    function () {
                      sitepage.close();
                      phInstance.exit();
                    }, 1000);

In this case the then holds the results from the evaluate condition. So it possible to implement in the waitFor support for the Promise?

stale

Most helpful comment

@loretoparisi because PhantomJS uses a bit old version of WebKit. This version doesn't have some feature from ECMAScript6. We're working on updating our web engine to deliver latest cool features to our users,

All 8 comments

According to node-phantom author here https://github.com/amir20/phantomjs-node/issues/431#issuecomment-207865947

the waitFor could become waitUntil to support Promise like:

function waitUntil(asyncTest) {
    return new Promise(function(resolve, reject) {
        function wait() {
            console.log('loop...')
            asyncTest().then(function(value) {
                console.log('value', value)
                if (value === true) {
                    resolve();
                } else {
                    setTimeout(wait, 100);
                }
            }).catch(function(e) {
                console.log('Error found. Rejecting.', e);
                reject();
            });
        }
        wait();
    });
}

and then

waitUtil(function() {
    return sitepage.evaluate(function() {
        return document.querySelectorAll('.content > ul > li').length > 1;
    })
}).then(function(){
  // evaluate is true
})

Could you please add this an alternative example?

Hey @loretoparisi I think you are misunderstanding the difference between NodeJS and PhantomJS. The examples directory in this project is about doing sample scripts with PhantomJS directly. There is no Promises or async blocks because everything just runs directly with PhantomJs. phantomjs-node is a wrapper on top of phantom which allows you to use PhantomJs in NodeJS. They are two different platforms.

I hope that makes sense as to why NodeJS and PhantomJS are different.

@amir20 yes it's clear to me, that makes sense, question is then, why PhantomJS does not support Promise ECMAScript6?

@loretoparisi because PhantomJS uses a bit old version of WebKit. This version doesn't have some feature from ECMAScript6. We're working on updating our web engine to deliver latest cool features to our users,

Ok this makes a lot of sense. If you agree I would keep this issue opened tagging it as enhancement.Thank you.

@Vitallium btw at which version of the WebKit is 2.1? I'm aware it is up-to-date with Qt 5.5.
Also did Qt deprecated the whole WebKit module?!?

Deprecated Functionality

With Qt 5.5 the following modules are deprecated:
Qt WebKit
Qt Declarative (Qt Quick 1)
Qt Script
These modules are still included in the Qt 5.5 release, but considered for removal in the future releases of Qt.

What is Qt and WebKit , i am really new :confused: .
@loretoparisi i am trying to achieve same things.
Yet found 2 options:

  1. phantom
  2. phantom-promise

Due to our very limited maintenance capacity, we need to prioritize our development focus on other tasks. Therefore, this issue will be automatically closed (see #15395 for more details). In the future, if we see the need to attend to this issue again, then it will be reopened. Thank you for your contribution!

Was this page helpful?
0 / 5 - 0 ratings