Assuming we have targets SUBMIT_BTN, EMAIL_INPUT defined with Puppetry app
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:
Helpers
Helpers are functions available in specification (suite) scope
util
png - build screenshot option
pollForValue - poll for value (used by page.assignVarRemotely)
// Emulating mouse click
await ( await SUBMIT_BTN() ).click();
// Emulating user input
await ( await EMAIL_INPUT() ).type( "[email protected]" );
// Obtaining "name" attribute's value of EMAIL_INPUT
const name = await bs.target( await EMAIL_INPUT() ).getAttr( "name" );
// Asserting that page HTML satisfies the given constraint
result = await bs.page.content();
expect( result ).toIncludeSubstring( "demo", "page.assertContent" );
// Asserting that window scroll offset satisfies the given constraint
result = await ( await bs.page.evaluate( () => window.scrollX ) );
expect( result ).toPassCondition( "lt", 10000, "page.assertScroll" );
// Asserting that EMAIL_INPUT element is visible
result = await bs.target( await EMAIL_INPUT() ).isVisible();
expect( result ).toBeOk( "EMAIL_INPUT.assertVisible" );
// Asserting that the bounding box of the element satisfies the given constraint
result = await ( await SUBMIT_BTN() ).boundingBox();
expect( result ).toMatchBoundingBoxSnapshot( {
"xOperator": "gt",
"xValue": 0,
"yOperator": "gt",
"yValue": 0,
"wOperator": "gt",
"wValue": 0,
"hOperator": "gt",
"hValue": 0
}, "SUBMIT_BTN.assertBoundingBox" );
// Asserting that the bounding box of the element satisfies the given constraint
result = {
target: await ( await DAY_SELECT() ).boundingBox(),
counterpart: await ( await MONTH_SELECT() ).boundingBox()
};
expect( result ).toMatchPosition( "left", "DAY_SELECT", "MONTH_SELECT", "DAY_SELECT.assertPosition" );
await bs.page.screenshot( util.png( "Just for test", {
"fullPage":true
}));