2017-06-22 39 views
0

我第一次使用CasperJS,我想在加載頁面之前添加一個cookie。我正在使用此代碼:如何從CasperJS訪問幻像對象?

casper.test.setUp(function() { 
    casper.echo('Cookies enabled?: ' + phantom.cookiesEnabled); 
    phantom.addCookie({ 
    domain: '.localhost', 
    name: 'sessionId', 
    value: '12345abcd6789efg' 
}); 

這個問題似乎與我的代碼中的phantom。我得到以下錯誤:

TypeError: undefined is not a constructor

如何我可以定義phantom

回答

0

CasperJS是建立在PhantomJS頂部,這意味着你的卡斯帕環境實際上是一個PhantomJS環境,一些額外的...

你可以寫檢查您的環境中最基本的腳本是以下:

console.log('PhantomJS version: ' + phantom.version.major + '.' + phantom.version.minor + '.' + phantom.version.patch); 
phantom.exit(); 

通常情況下,你應該能夠phantomjscasperjs命令來執行這個腳本。這會給你與phantomjs --version相同的輸出。

在您的腳本中,您正在使用CasperJS的測試模塊。但是我們可以從那裏訪問phantom對象嗎?當然是!

casper.test.setUp(function() { 
    console.log('PhantomJS version: ' + phantom.version.major + '.' + phantom.version.minor + '.' + phantom.version.patch); 
}); 

casper.test.begin('Example', function (test) { 
    casper.start('http://example.com'); 

    casper.then(function() { 
    test.assertEquals(this.getTitle(), 'Example Domain'); 
    }); 

    casper.run(function() { 
    test.done(); 
    }); 
}); 

這個時候你不能使用phantomjs命令,必須使用casperjs test代替。如果你嘗試運行這個腳本,你應該在測試開始之前看到PhantomJS版本...