Demonstrates a way of headless browser automation with SharePoint.
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.
npm install
npm run build
npm run auth
Provide auth options for SPO or On-Prem SharePoint site.
Auth options are stored in ./config/private.json
See more here.
npm run start
or
ts-node ./src/main.ts
Should start the Puppeteer (in headless mode) authenticated to SharePoint with node-sp-auth.
npm run start:interactive
or
ts-node ./src/interactive.ts
Interactive mode emulates user credentials input which let us navigate to any Office 365 section, e.g. Central Administration, etc., and run some UI automation or testing.
E.g., ./src/interactive script outputs a list of surveys and a number of responses from MS Forms.
import * as puppeteer from 'puppeteer';
import { authPuppeteer } from './auth';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const siteUrl = await authPuppeteer(page);
// Since this time page is authenticated in SharePoint
await page.goto(siteUrl, { waitUntil: 'networkidle2' });
/* Here comes Puppeteer logic: UI tests, screenshots, etc. */
// Save a screenshot of SharePoint page as PDF
await page.pdf({ path: 'sp.pdf', format: 'A4' });
// For other goodies check for Puppeteer API:
// https://github.com/GoogleChrome/puppeteer/blob/v1.9.0/docs/api.md
await browser.close();
})()
.catch(console.warn);
In CI environment configure SPAuth variables. E.g.:
- SPAUTH_SITEURL=https://contoso.sharepoint.com/sites/my-site
- SPAUTH_USERNAME=[email protected]
- SPAUTH_PASSWORD=secret
Clone the sample:
git clone [email protected]:koltyakov/sp-auth-puppeteer-sample.git
Install dependencies and build:
npm ci
npm run build
Run automation. E.g.:
node ./build/main --ci "--scenarios" "workbench,screenshot"
Embed approach into your favorite UI tests framework.