Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Automated Tablet Layout Screenshots: How to Test

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to automatically capture screenshots of your website at tablet viewport sizes, so you can quickly spot layout issues without manually resizing your browser. You'll learn a simple, scriptable approach using free tools, and how to turn the results into a visual regression check. No prior coding experience is needed, though basic familiarity with the command line helps.

What You'll Need

  • A computer with Node.js installed (version 14 or newer). You can download it from nodejs.org.
  • A code editor or text editor (like VS Code, Notepad++, or even Notepad).
  • Your website's URL(s) – the pages you want to test.
  • Basic comfort with using a terminal or command prompt.

Step-by-Step

  1. Set up a new project folder. Create a new folder on your computer, for example tablet-tests. Open a terminal window and navigate into that folder using cd tablet-tests.
  2. Initialize a Node.js project. Run npm init -y in the terminal. This creates a package.json file with default settings.
  3. Install Puppeteer. Puppeteer is a library that controls a headless Chrome browser. Run npm install puppeteer. This will also download a compatible version of Chrome for you.
  4. Create a screenshot script. In your project folder, create a new file named screenshot.js. Open it in your editor.
  5. Write the basic script. Paste the following code into screenshot.js:
    const puppeteer = require('puppeteer');
    
    (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
    
      // Set the viewport to a common tablet size (portrait and landscape)
      const viewports = [
        { width: 768, height: 1024, name: 'portrait' },
        { width: 1024, height: 768, name: 'landscape' }
      ];
    
      const urls = [
        'https://example.com',
        'https://example.com/about'
      ];
    
      for (const viewport of viewports) {
        await page.setViewport(viewport);
        for (const url of urls) {
          await page.goto(url, { waitUntil: 'networkidle0' });
          const filename = `${url.replace(/[^a-z0-9]/gi, '_')}_${viewport.name}.png`;
          await page.screenshot({ path: `screenshots/${filename}`, fullPage: true });
          console.log(`Saved ${filename}`);
        }
      }
    
      await browser.close();
    })();
  6. Create a screenshots folder. In your project folder, create a subfolder named screenshots. This is where the images will be saved.
  7. Run the script. In the terminal, run node screenshot.js. You should see messages like Saved https_example_com_portrait.png.
  8. Check the output. Open the screenshots folder and review the PNG files. They should show your site at the specified tablet dimensions.
  9. Automate with a test runner (optional but recommended). To make this a repeatable test, you can use Jest or Mocha. For example, install Jest with npm install jest, and modify your script to be a test that compares screenshots against a baseline. This is called visual regression testing.
  10. Set up visual regression (using pixelmatch). Install pixelmatch and pngjs with npm install pixelmatch pngjs. Then write a script that compares each new screenshot to a baseline image and fails if the difference exceeds a threshold. This gives you an automated pass/fail on layout changes.

Common Mistakes

  • Forgetting to set the viewport before navigating. If you call goto first, the page may load at the default size, and then resizing may not trigger a proper re-layout. Always set the viewport first.
  • Using only one tablet size. Tablets come in many sizes (iPad, Surface, etc.). Test at least two common widths (e.g., 768px and 1024px) and both orientations.
  • Ignoring full-page screenshots. If you don't use fullPage: true, you only capture the visible viewport, missing content below the fold that may be broken.
  • Not waiting for fonts and images. Using waitUntil: 'networkidle0' helps, but if you have lazy-loaded images, you may need to scroll or wait a bit more. Consider adding a delay or using page.waitForSelector for critical elements.
  • Hardcoding URLs. Your script should read URLs from a config file or environment variables, so you don't have to edit code every time you add a page.

Tips

  • Use a consistent baseline. Store your initial screenshots in a baseline/ folder. When you run tests, compare new screenshots against these. If a change is intentional, update the baseline.
  • Set a pixel threshold. In visual regression, use a small tolerance (e.g., 0.1% difference) to avoid failing on anti-aliasing or minor font rendering differences.
  • Run in CI/CD. Integrate your screenshot script into your continuous integration pipeline (like GitHub Actions or Jenkins) so every push triggers a tablet layout check.
  • Use device emulation. Puppeteer can emulate specific devices like an iPad Pro with page.emulate. This also sets the user agent and touch events, which can reveal layout quirks.
  • Combine with responsive design tools. While automated screenshots are great, also manually test with Chrome DevTools' device toolbar for interactive checks.

Troubleshooting

  • Puppeteer fails to launch. If you get an error about missing Chrome, run node node_modules/puppeteer/install.js to download the browser. Also ensure your system meets the required dependencies (libnss3, libatk, etc. on Linux).
  • Screenshots are blank or white. This often happens when the page hasn't finished loading. Increase the wait time or use waitUntil: 'networkidle2'. Also check that the URL is accessible from your environment.
  • Images are cut off. If fullPage: true doesn't capture the whole page, the site might have infinite scrolling or dynamic content. You can manually scroll and capture multiple screenshots, or use a tool like puppeteer-full-page-screenshot.
  • Viewport size doesn't match. Some sites override the viewport meta tag. Check your page's <meta name="viewport"> – it should allow width to scale. If not, you may need to emulate a device instead.
  • Test passes but layout is broken. Visual regression only detects pixel differences. If the layout shifts but the pixels are similar (e.g., text wraps slightly), it might not catch it. Use a higher threshold or also check element positions with page.evaluate.

Frequently Asked Questions

Do I need to know JavaScript to use this?

Basic familiarity helps, but you can copy and paste the provided script. If you want to customize it (add more URLs, change sizes), you'll need to understand simple JavaScript syntax.

Can I test on real tablet devices?

Yes, but that requires a different setup (like BrowserStack or Appium). The automated screenshot method here uses emulation, which is faster and more reliable for catching layout issues, though it doesn't exactly replicate hardware rendering.

How do I add more tablet sizes?

Simply add more objects to the viewports array in the script. For example, add { width: 820, height: 1180, name: 'ipad-air' }.

What if my site uses responsive images that load differently on tablet?

Puppeteer will load the images as a tablet browser would, based on the viewport and device pixel ratio. You can also set deviceScaleFactor to simulate retina displays.

How do I compare screenshots automatically?

Use a library like pixelmatch along with pngjs to compare images. You can write a small script that reads two images, calculates the diff, and exits with a non-zero code if the difference is above a threshold.

Can I schedule these tests to run daily?

Yes. You can use a cron job (on Linux/macOS) or Task Scheduler (on Windows) to run the Node script at a specific time. Or integrate it into a CI pipeline that runs on a schedule.