Share
Embed
Transcript
21 slides
-
©2018 Wantedly, Inc. Naoki Kobayashi GitHub: @kobayang Twitter: @kbys_02 I’m
an Engineer @Wantedly React / Rails React Native࢝Ί·ͨ͠ -
©2018 Wantedly, Inc. Agenda - Puppeteer ͱ - ͲΜͳͱ͜ΖͰΘΕΔ͔ -
ྫ - Puppeteer ͷಋೖ - ಋೖͨ͠ॴ - Α͔ͬͨͱ͜ΖɾϋϚͬͨͱ͜Ζ - ࡶײ -
©2018 Wantedly, Inc. ͲΜͳͱ͜ΖͰΘΕΔʁ • Generate screenshots and PDFs of
pages. • Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. "SSR"). • Automate form submission, UI testing, keyboard input, etc. • Create an up-to-date, automated testing environment. • Capture a timeline trace of your site • Test Chrome Extensions. -
©2018 Wantedly, Inc. ͲΜͳͱ͜ΖͰΘΕΔʁ • Generate screenshots and PDFs of
pages. • Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. "SSR"). • Automate form submission, UI testing, keyboard input, etc. • Create an up-to-date, automated testing environment. • Capture a timeline trace of your site • Test Chrome Extensions. -
©2018 Wantedly, Inc. SSR Example https://developers.google.com/web/tools/puppeteer/articles/ssr import puppeteer from “puppeteer";
async function ssr(url) { const browser = await puppeteer.launch({ headless: true }); const page = await browser.newPage(); await page.goto(url, { waitUntil: "networkidle0" }); const html = await page.content(); // serialized HTML of page DOM. await browser.close(); return html; } -
©2018 Wantedly, Inc. Ͳ͜Ͱ͔ͬͨʁ - OGը૾ͷΧελϚΠθʔγϣϯ - ࢦఆͨ͠ը૾ʹͷόονΛ ͤͨը૾Λฦ͢ -
WantedlyͰ࠷ۙϛʔτΞοϓػ ೳΛϦϦʔεͨ͠ - γΣΞ͞Εͨը૾͕ϛʔτΞοϓ Ͱ͋Δ͜ͱΛ͔Γ͍ͨ͘͢͠ -
©2018 Wantedly, Inc. Puppeteer v.s. node-canvas - node-canvas ͳΒը૾ΛՃͯ͠ฦ͢͜ͱ͕Ͱ͖ͦ͏ -
ଞͷࢪࡦͰ͢Ͱʹ༻͞Ε͍ͯͨ (pros) - node-canvas༻ͷAPIΛΘ͟Θ֮͑ͯ͟σβΠϯΛͯΔͷ໘ͦ͏ (cons) - Puppeteer ͩͱ screenshot API Λ͑ྑͦ͞͏ - গ͠ͷPuppeteerͷAPI͚֮ͩ͑Εྑͦ͞͏ (pros) - ελΠϧCSS͕͑ΔͷͰσβΠϯͷมߋʹ͙͢ରԠͰ͖ͦ͏ (pros) - ໘ɾ҆ఆੑͷෆ҆ (cons) - DockerͰಈ͔͘ෆ҆ (cons) -
©2018 Wantedly, Inc. Puppeteer v.s. node-canvas - OGը૾֤SNSʹΩϟογϡ͞ΕΔ - ΞΫηεͷෛՙʹ͍ͭͯ͋·Γߟ͑ͳͯ͘ྑ͍
- ෦ͷσβΠϯ͕ޙʑมΘΔՄೳੑ͕ߴ͍ - node-canvasͰॻ͍ͨͷΛॻ͖͑Δͷίετ ࠓճͷέʔε Puppeteer Λબ -
©2018 Wantedly, Inc. ྑ͔ͬͨ͜ͱ - htmlΛฦ͢endpointͱɺPuppeteer͔ΒhtmlΛݺΜͰscreenshotΛऔΔendpointΛ࣮ - ΛೖΕͨOGը૾Λฦ͢෦؆୯ʹ࣮Ͱ͖ͨ - ελΠϧͷ֬ೝ࣌ʹ
html Λฦ͢෦͚ͩݟΕ͍͍ͷͰ֬ೝ͕༰қ ϋϚͬͨ͜ͱ - DockerͰಈ͔͢ͷ͕গ͠໘ - webfont͕දࣔ͞Εͳ͍߹͕͋ͬͨ - Puppeteerͷىಈ͕͍ -
©2018 Wantedly, Inc. DockerͰಈ͔͢ͷ͕গ͠໘ - `npm install puppeteer` Ͱಈ͔ͳ͍ -
Alpine linux Ͱಈ͔ͤͳ͔ͬͨ - https://github.com/GoogleChrome/puppeteer/blob/master/docs/ troubleshooting.md#running-puppeteer-in-docker ʹৄ͘͠ॻ͍ͯ͋ΔͷͰɺ͜ΕΛࢀߟʹ࣮ -
©2018 Wantedly, Inc. webfont͕දࣔ͞Εͳ͍߹͕͋ͬͨ - “networkidle0”Λࢦఆ͢Δ͜ͱͰɺωοτϫʔΫ͕খ߁ঢ়ଶʹͳΔ·Ͱͬͯ͘ΕΔ - “networkidle0”Λࢦఆ͢ΔͱɺࠓޙɺӬԕʹͪଓ͚Δ߹͕ى͖ͯ͠·ͬͨ - issue͋Δ
https://github.com/GoogleChrome/puppeteer/issues/1908 - ݁ہwebfontϩʔΧϧʹೖΕͯ༻͢Δ͜ͱʹͨ͠ -
©2018 Wantedly, Inc. Puppeteerͷىಈ͕͍ - ৭ΜͳExampleͰΤϯυϙΠϯτͷ࠷ॳʹ`puppeteer.launch`Λ͍ͯ͠Δ͕ɺ `puppeteer.launch` ͰͷPuppeteerͷ্ཱ͕ͪ͛ඇৗʹ͍ - 1ඵҎ্͔͔ͬͯ͠·ͬͨ
- ͞ΒʹPuppeteerΛ্ཱͪ͛ͨͱ͖ʹɺchromeͷϓϩηε͕Ұ্ཱ͕ͭͪΔͷͰɺ ΞΫηε͕૿͑ͨͱ͖ʹෆ҆ - αʔόʔͷىಈ࣌ʹ `puppeteer.launch` Λ͓͍ͯͯ͠ɺsingleton Ͱ puppeteerͷঢ়ଶΛཧͨ͠ - Ϩεϙϯε࣌ؒେମ300-1000ms͘Β͍ʹͳͬͨ -
©2018 Wantedly, Inc. ࡶײ - ௐࠪΛ࢝Ί͔ͯΒ3ఔͰ࣮·ͰͰ͖ͨ - Puppeteerͷֶशίετ͍ʢجຊతʹ Example ͱҰॹʣ
- ϋϚΓϙΠϯτ͍͔ͭ͋ͬͨ͘ɻಛʹαʔόʔͰಈ͔͢ͱ͖ɻ - ग़ͳ͍ɻ͕ඞཁʹͳΔ߹ΩϟογϡͳͲ͢Δඞཁ͕͋Δ - ࣮ͨ͠ޙʹେ͖ͳى͖ͳ͔ͬͨ Α͍PuppeteerϥΠϑΛʂ