-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #354 from GuillaumeGomez/variables
Add `assert-variable`, `assert-variable-false`, `store-property` and `store-value` commands
- Loading branch information
Showing
8 changed files
with
427 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// All `compare*` commands. | ||
|
||
const { getAndSetElements } = require('./utils.js'); | ||
|
||
// Possible inputs: | ||
// | ||
// * (ident, "CSS selector" | "XPath", "property") | ||
function parseStoreProperty(parser) { | ||
const elems = parser.elems; | ||
|
||
if (elems.length === 0) { | ||
return {'error': 'expected a tuple, found nothing'}; | ||
} else if (elems.length !== 1 || elems[0].kind !== 'tuple') { | ||
return { | ||
'error': `expected a tuple, found \`${parser.getRawArgs()}\``, | ||
}; | ||
} | ||
const tuple = elems[0].getRaw(); | ||
if (tuple.length !== 3) { | ||
let err = `expected 3 elements in the tuple, found ${tuple.length} element`; | ||
if (tuple.length > 1) { | ||
err += 's'; | ||
} | ||
return {'error': err}; | ||
} else if (tuple[0].kind !== 'ident') { | ||
return { | ||
'error': `expected first argument to be an ident, found ${tuple[0].getArticleKind()} \ | ||
(\`${tuple[0].getText()}\`)`, | ||
}; | ||
} else if (tuple[1].kind !== 'string') { | ||
return { | ||
'error': `expected second argument to be a string, found ${tuple[1].getArticleKind()} \ | ||
(\`${tuple[1].getText()}\`)`, | ||
}; | ||
} else if (tuple[2].kind !== 'string') { | ||
return { | ||
'error': `expected third argument to be a CSS selector or an XPath, found \ | ||
${tuple[2].getArticleKind()} (\`${tuple[2].getText()}\`)`, | ||
}; | ||
} | ||
|
||
const selector = tuple[1].getSelector(); | ||
if (selector.error !== undefined) { | ||
return selector; | ||
} | ||
const warnings = []; | ||
const isPseudo = !selector.isXPath && selector.pseudo !== null; | ||
if (isPseudo) { | ||
warnings.push(`Pseudo-elements (\`${selector.pseudo}\`) don't have attributes so \ | ||
the check will be performed on the element itself`); | ||
} | ||
|
||
const varName = 'parseStoreProperty'; | ||
const code = `\ | ||
${getAndSetElements(selector, varName, false)} | ||
const jsHandle = await ${varName}.evaluateHandle((e, p) => { | ||
return String(e[p]); | ||
}, ${tuple[2].getText()}); | ||
arg.variables["${tuple[0].getText()}"] = await jsHandle.jsonValue();`; | ||
|
||
return { | ||
'instructions': [code], | ||
'wait': false, | ||
'warnings': warnings.length !== 0 ? warnings : undefined, | ||
}; | ||
} | ||
|
||
// Possible inputs: | ||
// | ||
// * (ident, "string" | number) | ||
function parseStoreValue(parser) { | ||
const elems = parser.elems; | ||
|
||
if (elems.length === 0) { | ||
return {'error': 'expected a tuple, found nothing'}; | ||
} else if (elems.length !== 1 || elems[0].kind !== 'tuple') { | ||
return {'error': `expected a tuple, found \`${parser.getRawArgs()}\``}; | ||
} | ||
const tuple = elems[0].getRaw(); | ||
if (tuple.length !== 2) { | ||
let err = `expected 2 elements in the tuple, found ${tuple.length} element`; | ||
if (tuple.length > 1) { | ||
err += 's'; | ||
} | ||
return {'error': err}; | ||
} else if (tuple[0].kind !== 'ident') { | ||
return { | ||
'error': 'expected first argument to be an ident, ' + | ||
`found ${tuple[0].getArticleKind()} (\`${tuple[0].getText()}\`)`, | ||
}; | ||
} else if (tuple[1].kind !== 'number' && tuple[1].kind !== 'string') { | ||
return { | ||
'error': `expected second argument to be a number or a string, found \ | ||
${tuple[1].getArticleKind()} (\`${tuple[1].getText()}\`)`, | ||
}; | ||
} | ||
return { | ||
'instructions': [`arg.variables["${tuple[0].getText()}"] = ${tuple[1].getText()};`], | ||
'wait': false, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
'parseStoreProperty': parseStoreProperty, | ||
'parseStoreValue': parseStoreValue, | ||
}; |
Oops, something went wrong.