Puppetry
Search…
Command API
API available via page.runjs command

NPM modules

Modules "fs", "path" and "os" exposed with the corresponding variables.
1
const title = ( await bs.page.title() ) + os.version();
Copied!
Available since ver. 3.2.6

Puppeteer methods

    bs - object representing browsing session
      browser - instance of Browser, which is being created in test setup with configuration provided in Puppetry app
      page - instance of Page (alias of bs.browser.page)
      performance
        resources - array with network activity logs
      getGaTracking - log of Google Analytics beacons
      getTarget - get target by name
      getTargetOrFalse - the same as previous, but when target is not available in the DOM return false instead of throwing exception
      target(element: ElementHandle) - helpers
        getProp(prop: string): Promise<string> - obtain element property value
        getAttr(attr: string): Promise<string> - obtain element attribute value
        isVisible(): Promise<boolean> - find out f element visible
        select(value: string): Promise<void> - set a value on select element
Examples:
1
// Navigating to "https://dsheiko.github.io/react-html5-form/"
2
await bs.page.goto( "https://dsheiko.github.io/react-html5-form/", {
3
timeout: 30000,
4
waitUntil: "load"
5
});
6
7
const title = await bs.page.title();
Copied!

Working with targets

Assuming we have targets SUBMIT_BTN, EMAIL_INPUT defined with Puppetry app
1
// Emulating mouse click
2
await ( await bs.getTarget( "SUBMIT_BTN" ) ).click();
3
// Emulating user input
4
await ( await bs.getTarget( "EMAIL_INPUT" ) ).type( "[email protected]" );
5
// Obtaining "name" attribute's value of EMAIL_INPUT
6
const name = await bs.target( await bs.getTarget( "EMAIL_INPUT" ) ).getAttr( "name" );
Copied!

Jest tools

Please find available API in official documentation of Jest framework. Most demanded tool for the context is expect. In the scope of the test it is extended with the following methods:
    expect( result )
      toBeOk( expectedValue, assertionSource) - assert the received value is truthy
      toBeEqual( expectedValue, assertionSource) - assert the received value equals expected one
      toIncludeSubstring(expectedSubstring, assertionSource) - assert the received string contains expected one
      toPassCondition(operator, valueToCompare, assertionSource) - assert the received value satisfies a given Puppetry condition
      toMatchBoundingBoxSnapshot(snapshot, assertionSource) - assert the received bounding box matches a given snapshot
      toMatchPosition(position, target, counterpart, assertionSource) - assert the received position object matches a given one
Examples:
1
// Asserting that page HTML satisfies the given constraint
2
result = await bs.page.content();
3
expect( result ).toIncludeSubstring( "demo", "page.assertContent" );
4
5
// Asserting that window scroll offset satisfies the given constraint
6
result = await ( await bs.page.evaluate( () => window.scrollX ) );
7
expect( result ).toPassCondition( "lt", 10000, "page.assertScroll" );
8
9
// Asserting that EMAIL_INPUT element is visible
10
result = await bs.target( await EMAIL_INPUT() ).isVisible();
11
expect( result ).toBeOk( "EMAIL_INPUT.assertVisible" );
12
13
// Asserting that the bounding box of the element satisfies the given constraint
14
result = await ( await SUBMIT_BTN() ).boundingBox();
15
expect( result ).toMatchBoundingBoxSnapshot( {
16
"xOperator": "gt",
17
"xValue": 0,
18
"yOperator": "gt",
19
"yValue": 0,
20
"wOperator": "gt",
21
"wValue": 0,
22
"hOperator": "gt",
23
"hValue": 0
24
}, "SUBMIT_BTN.assertBoundingBox" );
25
26
// Asserting that the bounding box of the element satisfies the given constraint
27
result = {
28
target: await ( await DAY_SELECT() ).boundingBox(),
29
counterpart: await ( await MONTH_SELECT() ).boundingBox()
30
};
31
expect( result ).toMatchPosition( "left", "DAY_SELECT", "MONTH_SELECT", "DAY_SELECT.assertPosition" );
Copied!

Helpers

Helpers are functions available in specification (suite) scope
    util
      png - build screenshot option
      pollForValue - poll for value (used by page.assignVarRemotely)
      exp - template expressions functions
      bytesToString - translates size into human-readable string
      generateTmpUploadFile - creates a file in temporary directory by given name and size
    fetch - Node.js-implementation of window.fetch
    localStorage - Node.js-implementation of localStorage
png( id, parentId, screenshotTitle, options ): Object - build screenshot options
Example:
1
await bs.page.screenshot( util.png( "id", "id", "Just for test", {
2
"fullPage":true
3
}));
Copied!
This command saves screenshot in ./screenshots/All--available--methods/Just-for-test.png given that test title is All available methods

Node.js

In the test scope one can access Node.js modules.
Last modified 2mo ago