Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Greetings to headless Chrome

Marina
June 12, 2017

Greetings to headless Chrome

What headless browsers are, why we need them and what we can do with Chrome browser in headless mode.

Marina

June 12, 2017
Tweet

Other Decks in Programming

Transcript

  1. AboUT me • Mobile Web QA at Badoo • Worked

    as QA Lead in Yandex.Music • LinkedIn: www.linkedin.com/in/ marina-fadeeva
  2. Available on multiple platforms: baDOo In NumBErS 350m Messages sent

    per day Registered users worldwide Monthly active users 349m 60m </> </> Mobile Web Desktop Web Android iOS Windows Phone
  3. wHat My TeAm DoEs •In Mobile Web team in Badoo

    we build and then test web applications •We test in different ways: •manually •with functional automated tests •with visual regression tests •Testing of web applications requires
 usage of browsers (LOL)
  4. teStINg FloW In BadOo Visual Regression tests Functional automated tests

    Testing in branch Manual QA Manual QA Testing in build Visual Regression tests Functional automated tests Prod DEV Development
  5. WhaT Is a hEaDlESs BroWsER? •What is the solution? •Ordinary

    browsers need GUI, but… •Servers for automation tests don't have GUI. •We need a browser without GUI •Headless browser is a web browser without GUI
  6. USe cASes FOr a hEaDlESs BroWsER •Test automation •Scraping web

    sites for data •Taking screenshots of web pages •Generating PDF •☠
  7. BroWsERs uSAge PAtTerNs Human Browser Website Website Robot Headless Browser

    Browsers for humans Browsers for robots I/O devices API
  8. BroWsERs uSAge PAtTerNs Human Browser Website Client Server Website Robot

    Headless Browser Browsers for humans Browsers for robots I/O devices API
  9. How TO usE BroWsER on GUi-lESs MacHIne? Browser UI Rendering

    Engine API Browser Engine User I/O X-test xvfb
  10. How TO usE BroWsER on GUi-lESs MacHIne? Browser UI Rendering

    Engine API Browser Engine User I/O Headless test X-test xvfb
  11. How TO usE BroWsER on GUi-lESs MacHIne? Browser UI Rendering

    Engine API Browser Engine User I/O Headless test X-test xvfb
  12. SomE HeAdLesS bRowSErS •PhantomsJS
 phantomjs.org •Nightmare
 www.nightmarejs.org •Google Chrome with

    Xvfb •Headless Chromium
 www.google.com/chrome/browser/canary.html •And many others…
 github.com/dhamaniasad/HeadlessBrowsers
  13. PhaNtOMjS... •Scriptable with a JavaScript API •Based on QtWebKit, fork

    of WebKit •Supports various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG •There is a lot of testing frameworks integrated with it: •Jasmine, QUnit, Mocha, Capybara etc
  14. exAMpLe: lET’s TakE a ScReEnShoT Open a 
 web page

    Take a screenshot
 of the page Store the screenshot
 to the disk
  15. ScReEnShoT - wiTh PhaNtOMjS var webPage = require('webpage'); 
 var

    page = webPage.create(); 
 
 page.viewportSize = { width: 1080, height: 1920 }; 
 
 page.open("http://m.badoo.com", function start(status) { 
 page.render('mbadoo.jpeg', { 
 format: 'jpeg',
 quality: '100'
 });
 phantom.exit(); 
 }); 

  16. ProS & coNs OF pHanTOmJs Advantages •Popular •Big community •Open

    source Disadvantages •Not a real browser •Doesn’t support xPath, video&audio, WebGL etc. •Bugs everywhere •Maintainer doesn’t support it anymore
  17. WhaT Is XvFb ? •X Virtual Framebuffer •In-memory display server

    for UNIX-like operating systems •Acts like a real X display server •Works with any real browser you choose
  18. How TO inTEraCt WitH cHroME + XvFb? User I/O Headless

    test Browser UI Rendering Engine API Browser Engine
  19. How TO inTEraCt WitH cHroME + XvFb? User I/O Headless

    test Browser UI Rendering Engine API Browser Engine X-test xvfb
  20. usINg ChRomE + xVfB in BAdoO Advantages •Acts exactly like

    the the real display •Allows to use any browser you need •And all this is done on a server Disadvantages •More complex environment = less stable environment
  21. GenERal OVerVIew •Headless Chrome allows to run Chrome in a

    headless/server environment •Headless mode is now available with Chrome 59 on Mac and Linux •Get it here:
 www.google.com/chrome/browser/desktop/ index.html •Windows support is coming soon!
  22. DifFEreNcE FroM rEGulAR cHroME What do we get? •Blink with

    the latest web platform awesomeness •Profiling and introspection tools 
 (DevTools UI, tracing, etc.) •Everything below the User Interface What is missing? •No Chrome-level features (e.g., profiles, sync ...) •No UI - offscreen software rendering •No extensions (for now)
  23. How TO inTEraCt WitH heADleSs ChRomE ? Browser UI Rendering

    Engine DevTools Protocol Browser Engine Client User I/O X-test xvfb
  24. diFfERenT waYs TO coNtROl iT Headless Chrome can be driven

    in several ways: •via command line flags •using Chrome as a client •programmatically All of them use the DevTools Protocol.
  25. StaRt ChRomE --heADleSs $ chrome --headless --disable-gpu https://badoo.com Temporarily needed

    (for now) Runs Chrome in a headless mode URL to be opened by Chrome
  26. FlaGs = NO coDInG rEQuIreD! Window Size $ chrome --window-size=800,500

    --headless
 --disable-gpu https://google.com Take a screenshot $ chrome --screenshot --headless
 --disable-gpu https://badoo.com
  27. FlaGs = NO coDInG rEQuIreD! Window Size $ chrome --window-size=800,500

    --headless
 --disable-gpu https://google.com Take a screenshot $ chrome --screenshot --headless
 --disable-gpu https://badoo.com Create a PDF $ chrome --print-to-pdf --headless
 --disable-gpu https://en.wikipedia.org/wiki/ Unicorn
  28. FlaGs = NO coDInG rEQuIreD! The full list of commands:

    https://cs.chromium.org/chromium/src/headless/ app/headless_shell_switches.cc
  29. UsiNg A reAL bRowSEr •Run Chrome with --remote-debugging-port=9222 •It starts

    an instance with the DevTools protocol enabled •Navigate to http://localhost:9222 in another browser •You'll see a list of inspectable pages •You can click through and see what Headless is rendering
  30. deVEloPEr ToOlS, we KnOW tHem •You can use the familiar

    DevTools features •A powerful debugging tool
  31. DevTOolS pRotOCol •Allows to instrument, inspect, debug and profile Chromium,

    Chrome and other Blink-based browsers •Chrome DevTools use this protocol •The messaging is based on serialised JSON objects •Documentation here:
 chromedevtools.github.io/devtools-protocol/
  32. DevTOolS pRotOCol •Allows to instrument, inspect, debug and profile Chromium,

    Chrome and other Blink-based browsers •Chrome DevTools use this protocol •The messaging is based on serialised JSON objects •Documentation here:
 chromedevtools.github.io/devtools-protocol/ Chrome DevTools
 Protocol Chrome Debugging Protocol = Chrome Remote Protocol = The
 SamE!
  33. VerSIonS Of The PrOTocOL •The latest “tip-of-tree” (tot) •Changes frequently

    and can break at any time •Captures the full capabilities of the protocol •v8-inspector (v8) •Available in node 6.3+ •Enables debugging & profiling of Node.js apps •Stable 1.2 (1-2) •Stable release of the protocol (Chrome 54) •Smaller subset of the complete protocol
  34. ThiRd-ParTy LibRAriES foR tHe pROtoCOl •Allow to write testing application

    using your favourite programming language: •Python •NodeJS •Go •etc. •Not all of them are in a good shape :) •More informations here:
 developer.chrome.com/devtools/docs/debugging- clients
  35. AgaIN: leT’s TakE a ScReEnShoT Open a 
 web page

    Take a screenshot
 of the page Store the screenshot
 to the disk
  36. PyThoN + ChRomE DevTOolS pRotOCol •We’re implementing a screenshot maker

    •We use the Python package Chromote •It’s a basic wrapper for DevTools Protocol
  37. cLiEnT coDE (apP.pY) import base64 import json from chromote import

    Chromote chrome = Chromote() tab = chrome.tabs[0] tab.set_url('https://m.badoo.com') tab._send({"method": "Emulation.setVisibleSize", "params": {"width": 1080, "height": 1920}}) res = tab._send({"method": "Page.captureScreenshot", "params": {"fromSurface": True}}) res = json.loads(res) with open("imageToSave.png", "wb") as fh: fh.write(base64.decodebytes(str.encode(res['result']['data'])))
  38. • cLiEnT coDE (apP.pY) import base64 import json from chromote

    import Chromote chrome = Chromote() tab = chrome.tabs[0] tab.set_url('https://m.badoo.com') tab._send({"method": "Emulation.setVisibleSize", "params": {"width": 1080, "height": 1920}}) res = tab._send({"method": "Page.captureScreenshot", "params": {"fromSurface": True}}) res = json.loads(res) with open("imageToSave.png", "wb") as fh: fh.write(base64.decodebytes(str.encode(res['result']['data'])))
  39. Sends JSON Receives JSON CliENt + SerVEr iNtERacTIon {“method": “Emulation.setVisibleSize",

    “params": {"width": 500,"height": 500}} {"some":"data"} Client Code Server Headless Browser
  40. laUNcH it… Launch Chrome in headless mode $ chrome --headless

    --remote-debugging-port=9222 --disable-gpu Run script app.py $ python app.py Bingo!
  41. OtHer EXamPlES •Running Selenium With The New Headless Chrome
 intoli.com/blog/running-selenium-with-headless-chrome/

    •Headless Chrome Protocol Exploration (plain Python)
 gist.github.com/llimllib/ 7f6143a1a6955d243161b2fec23b8f4d •How to finally use headless Chrome to power your automated tests (Headless Chrome + capybara)
 blog.faraday.io/headless-chromium/ •Using headless Chrome as an automated screenshot tool (Node.js)
 medium.com/@dschnr/using-headless-chrome-as-an- automated-screenshot-tool-4b07dffba79a
  42. SOme USefUL liNkS •Getting Started with Headless Chrome: developers.google.com/web/updates/2017/04/ headless-chrome

    •Chrome DevTools Protocol Viewer:
 chromedevtools.github.io/devtools-protocol/ •Headless Chrome bugs:
 bugs.chromium.org/p/chromium/issues/list •DevTools Protocol:
 github.com/ChromeDevTools/devtools-protocol
  43. ThE BriGhT fUTurE •Headless Chrome for Windows •Saving and restoring

    browser sessions •Working with pop-up windows •File downloads to local folder •The Headless Chrome team offered PhantomJS to move from WebKit fork to Chromium • #$
  44. “HeAdLesS moDE” acROsS bRowSErS •Status in Chromium •Chrome for desktop:

    release 59 •Opera: release 46 •Consensus & Standardization •Firefox: In development •Edge: No public signals •Safari: No public signals •Web Devs and QAs: Positive and Enthusiastic
  45. WhY heADleSs ChRomE is COol? •Brings all modern web platform

    features to the headless browser world •Powerful debugging tool - you see what your testing application sees •Can be run on server •Uses the latest version of Blink •Supported by Google (Chromium Community)
  46. WhY it IS noT sO CoOl (Yet) ? •The DevTools

    Protocol currently does not support multiple clients connected to the protocol simultaneously •There are only low-level APIs. High-level APIs are currently supported only by third-party clients. •Bleeding edge of development