2017-09-23 4 views
0

私は人形を使ってページに移動しようとしています。ウェブアプリケーションが特定の状態になるのを待って、スクリーンショットを撮って終了します。 SPAがスクリーンショットを撮りたい状態にあるとき、SPAは機能を呼び出します。私は非同期のjsコードの周りに頭をラップするのが難しいです。次のように公開された機能が呼び出された後、人形がスクリーンショットを撮る

私のコードでは、なります「。セッションを閉じたほとんどのページが閉じられた」

const puppeteer = require('puppeteer'); 

const url = 'http://example.com/'; 
const width = 300; 
const height = 250; 
const path = '/vagrant/tmp/screenshot.jpg'; 

async function takeScreenshoot(url, width, height, path) { 
    "use strict"; 

    const browser = await puppeteer.launch(); 
    const page = await browser.newPage(); 
    await page.setViewport({width: width, height: height}); 

    await page.exposeFunction('interestingFunction', (async data => { 
     console.log('Interesting function has been called. Taking a screenshot now.'); 
     await page.screenshot({path: path}); 
     await browser.close(); 
    })); 

    await page.goto(url); 
} 

(async() => { 
    "use strict"; 
    await takeScreenshoot(url, width, height, path); 
})(); 

をしかし、私はscreenshot.jsを呼び出すときに、私が言って未処理の約束についての警告を取得します

node --trace-warnings screenshot.js 

Interesting function has been called. Taking a screenshot now. 
(node:2572) Error: Protocol error (Runtime.evaluate): Session closed. Most likely the page has been closed. 
    at Session.send (/vagrant/node_modules/puppeteer/lib/Connection.js:167:29) 
    at Page._onConsoleAPI (/vagrant/node_modules/puppeteer/lib/Page.js:296:20) 
    at <anonymous> 
    at process._tickCallback (internal/process/next_tick.js:188:7) 
(node:2572) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 
    at emitWarning (internal/process/promises.js:78:15) 
    at emitPendingUnhandledRejections (internal/process/promises.js:95:11) 
    at process._tickCallback (internal/process/next_tick.js:189:7) 

私はライン18からawait browser.close()を削除すると、そこには警告はありませんが、スクリプトが終了したことがありません。

今や、interstingFunction()はもう少しですが、webappのウィンドウに公開することは安全です。私はちょうど失敗した上記の最小限のスクリプトの例を挙げようとしました。

私はノードv8.5.0を使用しています。

私はこれに間違っていますか?

+0

あなた 'page.goto(URL)の'の呼び出しは、見当違いのようです。 – nilobarp

+0

私はhttps://github.com/GoogleChrome/puppeteer/blob/master/examples/custom-event.jsの例にしたがって機能を公開してから、ページに移動します。また、 'await browser.close();'は、アプリケーションが読み込まれてから数秒後に呼び出されるコールバックで発生するので、実際には問題ではありません。 'await page.goto(url);'を移動すると警告が消えませんでした。 – hjm27

答えて

0

私は、その関数の呼び出し側があると思います。評価するために自分で呼び出す必要はありません。

Imoでは、browser.closeを最後まで移動してから公開する必要があります。文書のexposeFunctionを読む限り、それは待たれる約束を返します。エンド非同期に/待っていますが、約束

砂糖
await page.exposeFunction('interestingFunction', (async data => { 
    console.log('Interesting function has been called. Taking a screenshot now.'); 
    await page.screenshot({path: path}); 
    await browser.close(); 
})); 

await page.goto(url); 

であり、それは私の全体の一例

const url = 'http://example.com/'; 
const width = 300; 
const height = 250; 
const path = './screenshot.jpg'; 

async function takeScreenshoot(url, width, height, path) { 
    const browser = await puppeteer.launch(); 
    const page = await browser.newPage(); 

    await page.goto(url, {waitUntil: 'load'}); 

    await page.exposeFunction('jjj', (async() => { 
     console.log('Interesting function has been called. Taking a screenshot now.'); 
     return await page.screenshot({path: path}); 
    })); 

    await page.evaluate(async() => { 
     const myHash = await window.jjj(); 
    }); 

    await browser.close(); 
    } 

(async() => { 
    await takeScreenshoot(url, width, height, path); 
})(); 
+0

ありがとうございました!はい、私は 'interestingFunction()'自身を呼び出すことはありません。残念ながら、提案されたソリューションは、公開された関数が少なくとも1回は呼び出されるのを待たずに戻りますので、スクリーンショットを取得することはありません。それが私の元の例でコールバック内でブラウザを閉じようとする背後にある理由です。 – hjm27

関連する問題