Email Subscription Form

Saturday, February 8, 2020

"Less" is More Part II: Headless Browser Testing

Last week we talked about serverless architecture, and we learned that it's not really serverless at all!  This week we're going to be learning about a different type of "less": headless browser testing.

Headless browser testing means testing the UI of an application without actually opening up the browser.  The program uses the HMTL and CSS files of the app to determine exactly what is on the page without rendering it.

Why would you want to test your application headlessly?  Because without waiting for a webpage to load, your tests will be faster and less flaky.  You might think that it's impossible to actually see the results of your testing by using the headless method, but that's not true!  Headless testing applications can use a webpage's assets to determine what the page will look like and take a screenshot.  You can also find the exact coordinates of elements on the page.

Something important to note is that headless browser testing is not browserless testing.  When you run a headless test, you are actually running it in a specific browser; it's just that the browser isn't rendering.  Chrome, Firefox, and other browsers have added code that makes it possible to run the browser headlessly.  


To investigate headless browser testing, I tried out three different applications: Cypress, Puppeteer, and TestCafe.  All three applications can run in either regular browser mode or headless mode, although Puppeteer is set to be headless by default.  I found great tutorials for all three, and I was able to run a simple headless test with each very quickly.  

Cypress is a really great UI testing tool that you can get up and running in literally minutes.  (I'm so excited about this tool that it will be the subject of next week's blog post!)  You can follow their excellent documentation to get started:  https://docs.cypress.io/guides/getting-started/installing-cypress.html.  Once you have a test up and running, you can try running it headlessly in Chrome by using this command:  cypress run --headless --browser chrome.

Puppeteer is a node.js library that works specifically with Chrome.  To learn how to install and run it, I used this awesome tutorial by Nick Chikovani.  I simply installed Puppeteer with npm and tried out his first test example.  It was so fun to see how easy it is to take a screenshot headlessly!

Finally, I tried out TestCafe.  To install, I simply ran npm install -g testcafe.  Then I created a basic test file with the instructions on this page.  To run my test headlessly, I used this command: testcafe "chrome:headless" test1.js.

With only three basic tests, I barely scratched the surface of what these applications can do.  But I was happy to learn just how easy it is to set up and start working with headless browser tests.  I hope you find this helpful as you explore running your UI tests headlessly.  

3 comments:

  1. I first heard of headless testing this week at work and today's post is about it! Such a coincidence.

    ReplyDelete
    Replies
    1. I love it when things like that happen, Claudio! :-)

      Delete
  2. All three of those tools make it nice and easy to use headless browsers.

    If you have an existing UI automation framework and want to take advantage of headless browsers you can switch to containerized versions of Chrome and Firefox (search Docker hub for this) or pass in the headless flag to chrome itself.

    ReplyDelete

Book Review: The Unicorn Project

As I mentioned in a previous post , it's my goal this year to read and review one tech-related book each month.  This month, I read The ...