Skip to content

Commit

Permalink
feat: stringify helpers only if used
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Jul 10, 2023
1 parent b95ff12 commit 28a3d92
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 1,376 deletions.
4 changes: 2 additions & 2 deletions __snapshots__/JSONStringifyExtension.test.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports['JSONStringifyExtension should print the script for a click step 1'] = `
]
}
`;
`

exports['JSONStringifyExtension should print an entire script 1'] = `
{
Expand Down Expand Up @@ -52,4 +52,4 @@ exports['JSONStringifyExtension should print an entire script 1'] = `
}
//# recorderSourceMap=BDORO
`;
`
4 changes: 2 additions & 2 deletions __snapshots__/JSONUtils.test.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ exports['JSONUtils should format JSON as JS 1'] = `
otherTest: 1.234,
nullTest: null
}
`;
`

exports['JSONUtils should properly escape <script> 1'] = `
'\\x3Cscript>test\\x3C/script>\\x3Cscript>test\\x3C/script>\\x3Cscript>test\\x3C/script>'
`;
`
10 changes: 4 additions & 6 deletions __snapshots__/LighthouseStringifyExtension.test.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const puppeteer = require('puppeteer'); // v20.7.4 or later
fs.writeFileSync(__dirname + '/flow.report.html', lhFlowReport)
await
`;
`

exports['LighthouseStringifyExtension handles ending navigation 1'] = `
const fs = require('fs');
Expand Down Expand Up @@ -124,11 +124,9 @@ const puppeteer = require('puppeteer'); // v20.7.4 or later
fs.writeFileSync(__dirname + '/flow.report.html', lhFlowReport)
await
`;
`

exports[
'LighthouseStringifyExtension handles multiple sequential navigations 1'
] = `
exports['LighthouseStringifyExtension handles multiple sequential navigations 1'] = `
const fs = require('fs');
const puppeteer = require('puppeteer'); // v20.7.4 or later
Expand Down Expand Up @@ -190,4 +188,4 @@ const puppeteer = require('puppeteer'); // v20.7.4 or later
fs.writeFileSync(__dirname + '/flow.report.html', lhFlowReport)
await
`;
`
8 changes: 3 additions & 5 deletions __snapshots__/PuppeteerReplayStringifyExtension.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
exports[
'PuppeteerReplayStringifyExtension should print the script for a click step 1'
] = `
exports['PuppeteerReplayStringifyExtension should print the script for a click step 1'] = `
await runner.runStep({
type: 'click',
target: 'main',
Expand All @@ -16,7 +14,7 @@ await runner.runStep({
]
});
`;
`

exports['PuppeteerReplayStringifyExtension should print an entire script 1'] = `
import url from 'url';
Expand Down Expand Up @@ -50,4 +48,4 @@ if (process && import.meta.url === url.pathToFileURL(process.argv[1]).href) {
}
//# recorderSourceMap=BIO
`;
`
30 changes: 10 additions & 20 deletions __snapshots__/PuppeteerStringifyExtension.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
exports[
'PuppeteerStringifyExtension should print the correct script for a click step 1'
] = `
exports['PuppeteerStringifyExtension should print the correct script for a click step 1'] = `
{
const targetPage = page;
await puppeteer.Locator.race([
Expand All @@ -15,11 +13,9 @@ exports[
});
}
`;
`

exports[
'PuppeteerStringifyExtension should print the correct script for asserted events 1'
] = `
exports['PuppeteerStringifyExtension should print the correct script for asserted events 1'] = `
{
const targetPage = page;
const promises = [];
Expand All @@ -40,11 +36,9 @@ exports[
await Promise.all(promises);
}
`;
`

exports[
'PuppeteerStringifyExtension should print the correct script with a chain selector 1'
] = `
exports['PuppeteerStringifyExtension should print the correct script with a chain selector 1'] = `
{
const targetPage = page;
await puppeteer.Locator.race([
Expand All @@ -59,11 +53,9 @@ exports[
});
}
`;
`

exports[
'PuppeteerStringifyExtension should print the correct script for a change step 1'
] = `
exports['PuppeteerStringifyExtension should print the correct script for a change step 1'] = `
{
const targetPage = page;
await puppeteer.Locator.race([
Expand All @@ -73,11 +65,9 @@ exports[
.fill('Hello World');
}
`;
`

exports[
'PuppeteerStringifyExtension should print the correct script for a change step for non-text inputs 1'
] = `
exports['PuppeteerStringifyExtension should print the correct script for a change step for non-text inputs 1'] = `
{
const targetPage = page;
await puppeteer.Locator.race([
Expand All @@ -87,4 +77,4 @@ exports[
.fill('#333333');
}
`;
`
54 changes: 18 additions & 36 deletions __snapshots__/SchemaUtils.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,35 @@
exports[
'SchemaUtils Selectors selectorToPElementSelector should convert .cls > div #id 1'
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert .cls > div #id 1'] = `
.cls > div #id
`;
`

exports[
'SchemaUtils Selectors selectorToPElementSelector should convert .cls > div #id,.cls > div #id 1'
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert .cls > div #id,.cls > div #id 1'] = `
.cls > div #id >>>> .cls > div #id
`;
`

exports[
'SchemaUtils Selectors selectorToPElementSelector should convert text/my text ("my text") 1'
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert text/my text ("my text") 1'] = `
::-p-text(my text \\(\\"my text\\"\\))
`;
`

exports[
'SchemaUtils Selectors selectorToPElementSelector should convert text/my text ("my text"),aria/Test my test[role="button"] 1'
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert text/my text ("my text"),aria/Test my test[role="button"] 1'] = `
::-p-text(my text \\(\\"my text\\"\\)) >>>> ::-p-aria(Test my test[role=\\"button\\"])
`;
`

exports[
'SchemaUtils Selectors selectorToPElementSelector should convert text/my) 1'
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert text/my) 1'] = `
::-p-text(my\\))
`;
`

exports[
'SchemaUtils Selectors selectorToPElementSelector should convert text/my() 1'
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert text/my() 1'] = `
::-p-text(my\\(\\))
`;
`

exports[
'SchemaUtils Selectors selectorToPElementSelector should convert text/" 1'
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert text/" 1'] = `
::-p-text(\\")
`;
`

exports[
"SchemaUtils Selectors selectorToPElementSelector should convert text/' 1"
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert text/\' 1'] = `
::-p-text(\\')
`;
`

exports[
'SchemaUtils Selectors selectorToPElementSelector should convert xpath///*[@id="id"] 1'
] = `
exports['SchemaUtils Selectors selectorToPElementSelector should convert xpath///*[@id="id"] 1'] = `
::-p-xpath(//*[@id=\\"id\\"])
`;
`
133 changes: 2 additions & 131 deletions __snapshots__/lighthouse.test.ts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
exports[
'Lighthouse user flow run via stringify produces a valid desktop flow report 1'
] = `
exports['Lighthouse user flow run via stringify produces a valid desktop flow report 1'] = `
const fs = require('fs');
const puppeteer = require('puppeteer'); // v20.7.4 or later
Expand Down Expand Up @@ -87,137 +85,10 @@ const puppeteer = require('puppeteer'); // v20.7.4 or later
await browser.close();
async function waitForElement(step, frame, timeout) {
const {
count = 1,
operator = '>=',
visible = true,
properties,
attributes,
} = step;
const compFn = {
'==': (a, b) => a === b,
'>=': (a, b) => a >= b,
'<=': (a, b) => a <= b,
}[operator];
await waitForFunction(async () => {
const elements = await querySelectorsAll(step.selectors, frame);
let result = compFn(elements.length, count);
const elementsHandle = await frame.evaluateHandle((...elements) => {
return elements;
}, ...elements);
await Promise.all(elements.map((element) => element.dispose()));
if (result && (properties || attributes)) {
result = await elementsHandle.evaluate(
(elements, properties, attributes) => {
for (const element of elements) {
if (attributes) {
for (const [name, value] of Object.entries(attributes)) {
if (element.getAttribute(name) !== value) {
return false;
}
}
}
if (properties) {
if (!isDeepMatch(properties, element)) {
return false;
}
}
}
return true;
function isDeepMatch(a, b) {
if (a === b) {
return true;
}
if ((a && !b) || (!a && b)) {
return false;
}
if (!(a instanceof Object) || !(b instanceof Object)) {
return false;
}
for (const [key, value] of Object.entries(a)) {
if (!isDeepMatch(value, b[key])) {
return false;
}
}
return true;
}
},
properties,
attributes
);
}
await elementsHandle.dispose();
return result === visible;
}, timeout);
}
async function querySelectorsAll(selectors, frame) {
for (const selector of selectors) {
const result = await querySelectorAll(selector, frame);
if (result.length) {
return result;
}
}
return [];
}
async function querySelectorAll(selector, frame) {
if (!Array.isArray(selector)) {
selector = [selector];
}
if (!selector.length) {
throw new Error('Empty selector provided to querySelectorAll');
}
let elements = [];
for (let i = 0; i < selector.length; i++) {
const part = selector[i];
if (i === 0) {
elements = await frame.$$(part);
} else {
const tmpElements = elements;
elements = [];
for (const el of tmpElements) {
elements.push(...(await el.$$(part)));
}
}
if (elements.length === 0) {
return [];
}
if (i < selector.length - 1) {
const tmpElements = [];
for (const el of elements) {
const newEl = (await el.evaluateHandle(el => el.shadowRoot ? el.shadowRoot : el)).asElement();
if (newEl) {
tmpElements.push(newEl);
}
}
elements = tmpElements;
}
}
return elements;
}
async function waitForFunction(fn, timeout) {
let isActive = true;
const timeoutId = setTimeout(() => {
isActive = false;
}, timeout);
while (isActive) {
const result = await fn();
if (result) {
clearTimeout(timeoutId);
return;
}
await new Promise(resolve => setTimeout(resolve, 100));
}
throw new Error('Timed out');
}
})().catch(err => {
console.error(err);
process.exit(1);
});
//# recorderSourceMap=BRHYGePtBO7BW
`;
`
Loading

0 comments on commit 28a3d92

Please sign in to comment.