2013-03-08 53 views
9

我正在使用试图在服务器端为我们的网站的搜索引擎优化使用nodejs和phantomjs。虽然Ajax工作正常,但我无法执行我在代码中使用的自定义承诺。我如何让幻影等待承诺解决。 以下是我编码的内容。如何在phantomJS中执行jQuery承诺?

$('body').addClass('before-dom-ready'); 

$(function() { 
    $('body').addClass('after-dom-ready'); 

    var dfrd = $.Deferred(), 
      promise = dfrd.promise(); 

    setTimeout(function() { 
     dfrd.resolve(); 
    }, 5000); 

    promise.done(function() { 
     $('body').addClass('promise-executed'); 
    }); 

}); 

phantomJS增加了“前-DOM就绪”和阶级“-DOM就绪后”,但我无法得到“的承诺,执行”类的身体。

+0

是否有可能看到您的完整phantomJS脚本(以及它依赖的任何其他文件的完整代码)。即所以我们可以很容易地重现这个问题。 – 2013-05-28 00:24:42

+0

在什么时间点以及如何检查'promise-executed'类是否已添加?你做了什么样的调试,例如,你是否试过在'dfrd.resolve()'之后放置'console.log'调用?您是否在普通浏览器中试过这些代码,换句话说,是什么让您认为这是一个PhantomJS问题? – 2013-05-28 17:05:16

回答

4

PhantomJs不会自动等待所有待处理脚本的结束。在onload事件上调用WebPage#onLoadFinished。

至于大部分脚本,这里的想法是等到“某件事”完成或者真实。 我强烈建议你测试waitfor.js。在PhantomJs中理解这个例子非常重要。

我想你的例子是一个例子,但让我提出一个答案。

HTML页面

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script> 
    <title>Test</title> 
</head> 
<body id="body"> 

    <script type="text/javascript"> 
     //alert('hello'); 
     $('body').addClass('before-dom-ready'); 

     $(function() { 
      $('body').addClass('after-dom-ready'); 

      var dfrd = $.Deferred(), 
        promise = dfrd.promise(); 

      setTimeout(function() { 
       dfrd.resolve(); 
      }, 5000); 

      promise.done(function() { 
       $('body').addClass('promise-executed'); 
       $('body').text('Hello World !'); 
      }); 

     }); 
    </script> 
</body> 
</html> 

PhantomJs脚本

var page = require('webpage').create(); 
var system = require('system'); 

function waitFor(testFx, onReady, timeOutMillis) { 
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 10000, //< Default Max Timout is 10s 
     start = new Date().getTime(), 
     condition = false, 
     interval = setInterval(function() { 
      if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) { 
       // If not time-out yet and condition not yet fulfilled 
       condition = (typeof (testFx) === "string" ? eval(testFx) : testFx()); //< defensive code 
      } else { 
       if (!condition) { 
        // If condition still not fulfilled (timeout but condition is 'false') 
        //console.log("'waitFor()' timeout"); 
        typeof (onReady) === "string" ? eval(onReady) : onReady(); 
        clearInterval(interval); 
        //phantom.exit(1); 
       } else { 
        // Condition fulfilled (timeout and/or condition is 'true') 
        console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); 
        typeof (onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled 
        clearInterval(interval); //< Stop this interval 
       } 
      } 
     }, 500); //< repeat check every 500ms 
}; 

if (system.args.length != 1) { 
    console.log('invalid call'); 
    phantom.exit(1); 
} else { 
    //adapt url to your context 
    page.open('http://localhost:9231/demo.html', function (status) { 
     if (status !== 'success') { 
      console.log('Unable to load the address!'); 
      phantom.exit(); 
     } else { 
      waitFor(
       function() { 
        return page.evaluate(function() { 
         return $('body').hasClass('promise-executed'); 
        }) > 0; 
       }, 
       function() { 
        page.render('page.png'); 
        phantom.exit(); 
       }, 10000); 
     } 
    }); 
} 

基本上,waitFor将检查每500毫秒,如果身体有一个名为'promise-executed'类。

+0

感谢您的详细回复..我将不得不做更多的阅读之前纳入到我的测试,但这似乎是合理的和有据可查的.. – 2013-05-29 15:14:21

+0

是的,这对我很有用。希望这也能帮助你。 – Cybermaxs 2013-05-30 14:07:33