A better interface for common browser automation workflows
import {
openBrowser, closeBrowser, openTab, waitForNavigation,
findElement, fillInElement, clickElement, evalInTab
} from 'puppet-strings'
async function run() {
const browser = await openBrowser('chromium')
const tab = await openTab(browser, 'https://google.com/ncr')
const searchInput = await findElement(tab, '[name="q"]')
await fillInElement(searchInput, 'Node.js')
const searchButton = await findElement(tab, `input[value="I'm Feeling Lucky"]`)
await clickElement(searchButton)
await waitForNavigation(tab)
const title = await evalInTab(tab, [], 'return document.title')
console.log(title)
await closeBrowser(browser)
}
run()
Install puppet-strings by running
yarn add puppet-strings
Or, if yarn
is not installed, run
npm install --save puppet-strings
puppet-strings
works with locally installed recent stable versions of Google
Chrome, Chromium, and Electron on Linux, Docker, OSX, and Windows.
We also maintain
vinsonchuong/javascript
,
a Docker image that includes the latest Current
version of Node.js and Chrome
(as google-chrome
)
puppet-strings
provides an interface consisting of three nouns
(browser, tab, and element) and actions that take one of them as first argument.
Each action returns a promise that is resolved when the action is finished.
Here are the actions puppet-strings
provides:
openBrowser
: Open a locally installed browsercloseBrowser
: Closes a browser
getTabs
: Gets the list of currently open tabsopenTab
: Opens a url in a new tab and waits for it to fully loadcloseTab
: Closes a tabresizeTab
: Changes the size of the viewport of a tabnavigate
: Navigates a tab to a new URLwaitForNavigation
: Waits for a page load to completeevalInTab
: Evaluates code within a tab and returns the resultfindElement
: Find a DOM element on the page by CSS selector
clickElement
: Click on an elementfillInElement
: Type text into an elementselectFiles
: Select files for a file input element
puppet-strings
is designed to be open for extension but closed for
modification.
You can create new actions that take a Browser
,
Tab
, or Element
as
argument. Your new actions can reuse other actions and interact directly with
the underlying Puppeteer objects.
If your project needs to modify an action provided by puppet-strings
, you can
duplicate that action and maintain your modifications as part of your project.
openChrome()
: Find or download a suitable version of Chrome for use with puppet-stringsopenApp()
: Compile and open a web application in Chrome