ScreenshotNeo

BlogHow-to

How to Capture a New Relic Dashboard Screenshot With CasperJS

Capture a reliable New Relic dashboard image with CasperJS, choose the right viewport and wait conditions, and compare native export options.

By the ScreenshotNeo team30 September 20267 min read

How to Capture a New Relic Dashboard Screenshot With CasperJS

Direct answer: set an explicit CasperJS viewport, open the authorized New Relic dashboard URL, wait for a dashboard-specific element or rendering condition, then call capture() for the page or captureSelector() for one dashboard element. CasperJS inherits PhantomJS’s 400 × 300 default viewport, so leaving the viewport unchanged commonly produces a cropped or unreadable image. The documented capture and waiting APIs are described in the CasperJS module documentation.

The example below is a maintenance pattern for an existing CasperJS installation. CasperJS’s project repository says it is no longer actively maintained, so for a new integration check New Relic’s built-in export or NerdGraph first.

1. Before you write the script

  • Use a dashboard URL that the browser process is authorized to access. Do not put New Relic credentials in source code, command history, screenshots, or logs.
  • Choose the output: a full page, a clipped rectangle, or a single dashboard element.
  • Choose a viewport that matches the intended artifact. A wide dashboard usually needs at least 1280 pixels of width; use the actual report size you need rather than relying on the 400 × 300 default.
  • Identify a selector that appears only when the dashboard shell has loaded. Then add a stronger condition if charts continue rendering after that element appears.

2. Complete CasperJS capture script

Replace the URL, selector, and output path with values from your account. This script waits for a dashboard element before saving a full-page PNG.

A reliable capture waits for dashboard content before writing the image.
A reliable capture waits for dashboard content before writing the image.
var casper = require('casper').create({
  verbose: true,
  logLevel: 'warning'
});

var dashboardUrl = 'https://YOUR_NEW_RELIC_DASHBOARD_URL';
var readySelector = 'YOUR_DASHBOARD_READY_SELECTOR';
var outputPath = 'new-relic-dashboard.png';

casper.start(dashboardUrl);

casper.viewport(1920, 1080, function () {
  this.waitForSelector(readySelector, function () {
    // Add a page-specific chart or loading-state check here when needed.
    this.capture(outputPath);
    this.echo('Saved ' + outputPath);
  }, function () {
    this.die('Dashboard selector did not appear: ' + readySelector, 1);
  });
});

casper.run(function () {
  this.exit();
});

Run it with the CasperJS executable used by your legacy environment, for example casperjs capture-dashboard.js. The code is an adaptable structure: the cited documentation establishes the APIs, but no generic selector proves that every New Relic chart has finished drawing.

Use a fixed delay only as a fallback

A selector is preferable to an arbitrary sleep. If the dashboard has no useful stable selector, combine a selector with a short, justified delay or wait for a page-specific loading indicator to disappear. A long fixed delay makes every run slower; a short delay can capture incomplete charts.

casper.waitForSelector('.dashboard-container', function () {
  this.wait(3000, function () {
    this.capture('new-relic-dashboard.png');
  });
});

3. Capture a region or one widget

Clip a rectangle

Use capture() with a clip rectangle when the required area has predictable coordinates. Coordinates are viewport pixels, so changing the viewport changes the result.

casper.then(function () {
  this.capture('dashboard-clip.png', {
    top: 80,
    left: 0,
    width: 1920,
    height: 900
  });
});

Capture an element by CSS selector

captureSelector() is less sensitive to surrounding layout changes and is usually better for one dashboard panel.

casper.then(function () {
  this.captureSelector(
    'primary-widget.png',
    'YOUR_WIDGET_SELECTOR'
  );
});

The selector must identify an element present in the rendered page. If the widget is inside an iframe or is drawn into a canvas, wait for the frame or canvas to exist and verify that its pixels are populated before capture.

4. Make readiness reflect the dashboard

  1. Wait for the dashboard container or a known widget.
  2. Check for a loading or error state and fail instead of saving a misleading image.
  3. If data is loaded after the shell, wait for a chart-specific class, a changed text value, or another condition your dashboard reliably exposes.
  4. Capture only after the viewport has reflowed and the final layout is visible.
casper.waitFor(function checkDashboard() {
  return this.exists('.dashboard-container') &&
         !this.exists('.dashboard-loading') &&
         !this.exists('.dashboard-error');
}, function thenReady() {
  this.capture('dashboard-ready.png');
}, function timedOut() {
  this.die('Dashboard did not reach a capturable state', 1);
}, 30000);

This condition is a template. Inspect the actual DOM and adapt the selectors; New Relic dashboards can differ by account, permissions, layout, and widget state.

5. New Relic’s native export options

For routine exports, New Relic documents dashboard downloads as PDF, PNG, or JSON in the UI. Its NerdGraph API also provides a dashboardCreateSnapshotUrl mutation for programmatic PDF or PNG snapshots, with options such as custom dimensions, filters, and template-variable overrides. The full-dashboard mutation requires the dashboard page GUID, not the dashboard GUID. See New Relic’s dashboard import/export documentation and the NerdGraph export tutorial.

Snapshot URLs are publicly accessible without authentication and expire after three months. Treat a generated URL as a public share link and check whether the dashboard contains sensitive data before distributing it. For large dashboards, New Relic notes that requests can time out when the time window is large or many widgets are present; reduce the time range or remove unused widgets when troubleshooting.

Approach Best for Control Maintenance and exposure
CasperJS Preserving a legacy browser workflow or custom page interaction Viewport, waits, clipping, selectors, and browser-side behavior CasperJS is no longer actively maintained; the captured page follows the browser session’s access rules
New Relic UI export Manual PDF, PNG, or JSON downloads New Relic’s supported dashboard export flow Lowest setup cost for occasional exports
NerdGraph snapshot Automated dashboard files and variable-driven exports PDF/PNG, dimensions, filters, and variable overrides Snapshot URLs are public and expire after three months

6. Troubleshooting

Symptom Likely cause Fix
Image is 400 × 300 or badly cropped PhantomJS’s default viewport was left unchanged Call casper.viewport(width, height) before capture and allow the page to reflow.
Blank or partly rendered charts Capture happened before asynchronous data or canvas rendering completed Wait for a chart-specific condition, loading state to clear, or a measured page event; do not assume the first container means all charts are ready.
waitForSelector() times out The selector is wrong, hidden behind a different layout, or never rendered for that account Inspect the page in the same viewport, verify the selector, and choose an element guaranteed by that dashboard.
Only one panel is needed but the PNG contains the whole page capture() was used without clipping Use captureSelector() or pass a clipRect.
Charts differ between runs Responsive layout, changing data, delayed requests, or a different authenticated state Fix the viewport, use deterministic time ranges where possible, wait for stable content, and keep browser credentials outside the script.
NerdGraph export times out Large time window or too many widgets Reduce the time window or remove unused widgets, as described in New Relic’s export tutorial.
A snapshot link exposes data New Relic snapshot URLs are public without authentication Apply your data-sharing policy before creating or forwarding the URL; remember the three-month expiry.

7. Performance, reliability, and cost

  • Performance: viewport size, full-page layout, chart count, and the chosen wait condition affect capture time. Capture one selector when a full dashboard is unnecessary.
  • Reliability: fail loudly on missing selectors, loading states, and error states. Save a diagnostic log or page metadata separately instead of silently publishing an incomplete image.
  • Repeatability: keep viewport, URL parameters, account context, and readiness checks consistent. Dashboard data itself can change between runs.
  • Cost: CasperJS is local automation, so its direct cost is the machine and browser environment you operate. New Relic API limits, account permissions, and dashboard size still determine whether an export succeeds.
Pre-capture cleanup removes common overlays so the resulting screenshot contains the dashboard.
Pre-capture cleanup removes common overlays so the resulting screenshot contains the dashboard.

Or skip the browser setup

ScreenshotNeo captures a URL with one request and returns PNG, JPEG, WebP, or PDF. It accepts the cookie or consent banner like a visitor, removes more than 60 known consent platforms plus newsletter popups and chat widgets before the shot, and reports whether a response was billed.

For a public dashboard URL, the call is:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://YOUR_NEW_RELIC_DASHBOARD_URL -o dashboard.webp

Python:

import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={
        "access_key": "YOUR_API_KEY",
        "url": "https://YOUR_NEW_RELIC_DASHBOARD_URL"
    },
    timeout=90
)
r.raise_for_status()
open("dashboard.webp", "wb").write(r.content)

Node.js:

const q = new URLSearchParams({
  access_key: 'YOUR_API_KEY',
  url: 'https://YOUR_NEW_RELIC_DASHBOARD_URL'
});
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot failed: ${res.status}`);
const fs = await import('node:fs/promises');
await fs.writeFile('dashboard.webp', Buffer.from(await res.arrayBuffer()));

See the ScreenshotNeo API documentation for viewport, full-page, selector, wait, custom headers, cookies, user-agent, caching, PDF, and other options. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and the response identifies the page verdict and billing status. Its MCP server lets Claude, Cursor, and other MCP clients call take_screenshot, get_page_info, and capture_pdf. The Free plan includes 1,000 screenshots each month with no card; paid plans start at $5 for 3,000.

Create a free ScreenshotNeo account and start with 1,000 screenshots per month at no charge.

FAQ

Can CasperJS log in to New Relic?

It can automate browser interactions in principle, but the reviewed sources do not document a CasperJS-specific New Relic login flow. Use an authorized browser setup and keep credentials out of examples and logs.

Should I use a full-page capture or a selector?

Use a full-page capture for an archival dashboard image. Use captureSelector() for one widget or a stable dashboard region.

Is a New Relic snapshot URL private?

No. New Relic documents snapshot URLs as publicly accessible without authentication and expiring after three months.

Is CasperJS suitable for a new project?

The CasperJS repository says the project is no longer actively maintained. Prefer New Relic’s supported UI or NerdGraph export when those meet the requirement; keep CasperJS for an existing workflow that you can maintain.