Skip to content

Commit

Permalink
parse template in assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
dsheiko committed Jun 8, 2020
1 parent 8da86ac commit 6ecc111
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Puppetry 3.2.3

### Bug fixes
- fix: parsing template expressions in TARGET.assertAttribute, page.assertConsoleMessage, page.assertDialog, TARGET.assertProperty, TARGET.assertHtml, TARGET.assertStyle, page.assertContent, page.assertTitle, page.assertUrl, page.assertVar

# Puppetry 3.2.2

### Features
Expand Down
2 changes: 1 addition & 1 deletion app/main/install-runtime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ exports.installRuntimeTest = ( event, appInstallDirectory ) => {
"faker@^4.1.0",
"jsonpath@^1.0.2",
"node-localstorage@^1.3.1",
"puppeteer@3.1.0",
"puppeteer@3.3.0",
"shelljs@^0.8.2",
"pixelmatch@^5.1.0",
"pngjs@^3.4.0",
Expand Down
2 changes: 1 addition & 1 deletion jest-pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"jsonpath": "^1.0.2",
"node-fetch": "^2.6.0",
"node-localstorage": "^1.3.1",
"puppeteer": "^3.1.0",
"puppeteer": "^3.3.0",
"shelljs": "^0.8.2",
"pixelmatch": "^5.1.0",
"pngjs": "^3.4.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "puppetry",
"description": "Puppetry - codeless end-to-end test automation, integrated with CI/CD pipeline",
"version": "3.2.2",
"version": "3.2.3",
"author": "Dmitry Sheiko <me@dsheiko.com> (http://dsheiko.com)",
"engines": {
"node": ">=11.7.0"
Expand Down
8 changes: 6 additions & 2 deletions project-test/.puppetryrc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"cgmk6tfbh3v": {
"key": "cgmk6tfbh3v",
"value": true
},
"ltbk378vixm": {
"key": "ltbk378vixm",
"value": true
}
}
},
Expand All @@ -79,9 +83,9 @@
}
}
},
"savedAt": 1590424783,
"savedAt": 1591373051,
"modified": false,
"lastOpenSuite": "page.assertRest.json",
"lastOpenSuite": "page.assertDialog.json",
"variables": {},
"targets": {
"js4k1ukyk9a": {
Expand Down
10 changes: 10 additions & 0 deletions src/component/Schema/Assert/AssertAttribute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ export class AssertAttribute extends AbstractComponent {
<Row gutter={24}>

<Col span={8} >

{ /* Force template parsing */"" }
<FormItem className="is-hidden">
{ getFieldDecorator( "assert.assertionType", {
initialValue: "string"
})(
<Input />
) }
</FormItem>

<FormItem label="Result">
{ getFieldDecorator( "assert.assertion", {
initialValue: assertion,
Expand Down
9 changes: 9 additions & 0 deletions src/component/Schema/Assert/AssertConsoleMessage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ export class AssertConsoleMessage extends AbstractComponent {
</FormItem>
<span>messages sent to the console like the following</span></span>

{ /* Force template parsing */"" }
<FormItem className="is-hidden">
{ getFieldDecorator( "assert.assertionType", {
initialValue: "string"
})(
<Input />
) }
</FormItem>

<Row gutter={24} >
<Col span={8} >
<FormItem label="Message type">
Expand Down
9 changes: 9 additions & 0 deletions src/component/Schema/Assert/AssertDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ export class AssertDialog extends AbstractComponent {
value = record.assert.value || "";
return ( <React.Fragment>

{ /* Force template parsing */"" }
<FormItem className="is-hidden">
{ getFieldDecorator( "assert.assertionType", {
initialValue: "string"
})(
<Input />
) }
</FormItem>

<span className="assert-there-were"><span>Assert there</span><FormItem>
{ getFieldDecorator( "assert.not", {
initialValue: not,
Expand Down
10 changes: 10 additions & 0 deletions src/component/Schema/Assert/AssertProperty.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ export class AssertProperty extends AbstractComponent {
<Row gutter={24}>

<Col span={8} >

{ /* Force template parsing */"" }
<FormItem className="is-hidden">
{ getFieldDecorator( "assert.assertionType", {
initialValue: "string"
})(
<Input />
) }
</FormItem>

<FormItem label="Result">
{ getFieldDecorator( "assert.assertion", {
initialValue: assertion,
Expand Down
10 changes: 10 additions & 0 deletions src/component/Schema/Assert/AssertString.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export class AssertString extends AbstractComponent {
<Row gutter={24}>

<Col span={8} >

{ /* Force template parsing */"" }
<FormItem className="is-hidden">
{ getFieldDecorator( "assert.assertionType", {
initialValue: "string"
})(
<Input />
) }
</FormItem>

<FormItem label="Result">
{ getFieldDecorator( "assert.assertion", {
initialValue: assertion,
Expand Down
2 changes: 1 addition & 1 deletion src/component/Schema/Assert/AssertText.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class AssertText extends AbstractComponent {
<Col span={8} >

<FormItem className="is-hidden">
{ getFieldDecorator( "assert.type", {
{ getFieldDecorator( "assert.assertionType", {
initialValue: "text"
})(
<Input />
Expand Down
4 changes: 3 additions & 1 deletion src/component/Schema/Params/Element/assertStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export const assertStyle = {
template: ( command ) => buildAssertionTpl(
`await bs.page.$eval( '${ command.targetSeletor }',
( el, prop, pseudoEl ) => window.getComputedStyle( el, pseudoEl || null )
.getPropertyValue( prop ), "${ command.params.name }", "${ command.params.pseudo }" )`,
.getPropertyValue( prop ), "${ command.params.name }", ${
command.params.pseudo ? JSON.stringify( command.params.pseudo ) : null
} )`,
command,
`// Asserting that "${ command.params.name }" CSS property's `
+ `value of ${ command.target } satisfies the given constraint`
Expand Down
25 changes: 14 additions & 11 deletions src/service/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ export function justify( text ) {
return ( "\n" + text ).split( "\n" ).map( line => " " + line ).join( "\n" );
}

function parseTpl( value, id, type = "string" ) {
// @see ./src/component/Schema/Params/Element/assertText.js
if ( typeof type !== "undefined" || type === "text" ) {
return JSON.stringify( value.replace( /\n+/gm, "\n" ) );
}
function parseTpl( value, id, type ) {
if ( typeof type === "undefined" || type !== "string" ) {
return JSON.stringify( value );
}
// @see ./src/component/Schema/Params/Element/assertText.js
if ( typeof type !== "undefined" && type === "text" ) {
value = value.replace( /\n+/gm, "\n" );
}
const parser = new ExpressionParser( id );
return parser.stringify( value );
Expand Down Expand Up @@ -135,15 +135,16 @@ function createCbBody({ assert, target, method, id, params }) {
+ ` ${ getExpectation( method, params, assert ) }, "${ source }" );` );

case "contains":
return justify( `expect( result ).toIncludeSubstring( ${ parseTpl( value, id, options.type ) }`
return justify( `expect( result ).toIncludeSubstring( ${ parseTpl( value, id, options.assertionType ) }`
+ `, "${ source }" );` );
case "!contains":
return justify( `expect( result ).not.toIncludeSubstring( ${ parseTpl( value, id, options.type ) }`
return justify( `expect( result ).not.toIncludeSubstring( ${ parseTpl( value, id, options.assertionType ) }`
+ `, "${ source }" );` );
case "equals":
return justify( `expect( result ).toBeEqual( ${ parseTpl( value, id, options.type ) }, "${ source }" );` );
return justify( `expect( result ).toBeEqual( ${ parseTpl( value, id, options.assertionType ) }, "${ source }" );` );
case "!equals":
return justify( `expect( result ).not.toBeEqual( ${ parseTpl( value, id, options.type ) }, "${ source }" );` );
return justify( `expect( result )`
+ `.not.toBeEqual( ${ parseTpl( value, id, options.assertionType ) }, "${ source }" );` );
case "empty":
return justify( `expect( result ).toBeEmpty( "${ source }" );` );
case "!empty":
Expand All @@ -166,10 +167,12 @@ function createCbBody({ assert, target, method, id, params }) {
return justify( `expect( result ).not.toHavePropertyTrue( "${ params.name }", "${ source }" );` );

case "haveString":
return justify( `expect( result )${ negate( not ) }.toHaveString( ${ parseTpl( value, id, options.type ) }`
return justify( `expect( result )${ negate( not ) }`
+ `.toHaveString( ${ parseTpl( value, id, options.assertionType ) }`
+ `, ${ getExpectation( method, params, assert ) }, "${ source }" );` );
case "haveSubstring":
return justify( `expect( result )${ negate( not ) }.toHaveSubstring( ${ parseTpl( value, id, options.type ) }`
return justify( `expect( result )${ negate( not ) }`
+ `.toHaveSubstring( ${ parseTpl( value, id, options.assertionType ) }`
+ `, ${ getExpectation( method, params, assert ) }, "${ source }" );` );

case "position":
Expand Down

0 comments on commit 6ecc111

Please sign in to comment.