Skip to content

Commit

Permalink
added config for waitForTimeout and waitForSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
amandeepsingh333 committed Sep 11, 2024
1 parent 4f97ecb commit 6a71cc4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ export const configSchema = {
maximum: 750,
minimum: 1
},
waitForSelector: {
type: 'string'
},
waitForTimeout: {
type: 'integer',
minimum: 1,
maximum: 30000
},
disableCache: {
type: 'boolean'
},
Expand Down Expand Up @@ -294,6 +302,8 @@ export const snapshotSchema = {
properties: {
allowedHostnames: { $ref: '/config/discovery#/properties/allowedHostnames' },
disallowedHostnames: { $ref: '/config/discovery#/properties/disallowedHostnames' },
waitForSelector: { $ref: '/config/discovery#/properties/waitForSelector' },
waitForTimeout: { $ref: '/config/discovery#/properties/waitForTimeout' },
requestHeaders: { $ref: '/config/discovery#/properties/requestHeaders' },
authorization: { $ref: '/config/discovery#/properties/authorization' },
disableCache: { $ref: '/config/discovery#/properties/disableCache' },
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createLogResource,
yieldAll,
snapshotLogName,
waitForTimeout as sleep,
withRetries
} from './utils.js';
import {
Expand Down Expand Up @@ -203,6 +204,20 @@ async function* captureSnapshotResources(page, snapshot, options) {
yield resizePage(snapshot.widths[0]);
yield page.goto(snapshot.url, { cookies });

// wait for any specified timeout
if (snapshot.discovery.waitForTimeout && snapshot.enableJavaScript) {
log.debug(`Wait for ${snapshot.discovery.waitForTimeout}ms timeout`);
await sleep(snapshot.discovery.waitForTimeout);
}

let timeout = parseInt(process.env.PERCY_PAGE_LOAD_TIMEOUT) || 30000;

// wait for any specified selector
if (snapshot.discovery.waitForSelector && snapshot.enableJavaScript) {
log.debug(`Wait for selector: ${snapshot.discovery.waitForSelector}`);
await page.eval(`await waitForSelector(${JSON.stringify(snapshot.discovery.waitForSelector)}, ${timeout})`);
}

if (snapshot.execute) {
// when any execute options are provided, inject snapshot options
/* istanbul ignore next: cannot detect coverage of injected code */
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ function getSnapshotOptions(options, { config, meta }) {
allowedHostnames: config.discovery.allowedHostnames,
disallowedHostnames: config.discovery.disallowedHostnames,
networkIdleTimeout: config.discovery.networkIdleTimeout,
waitForTimeout: config.discovery.waitForTimeout,
waitForSelector: config.discovery.waitForSelector,
devicePixelRatio: config.discovery.devicePixelRatio,
requestHeaders: config.discovery.requestHeaders,
authorization: config.discovery.authorization,
Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/discovery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,29 @@ describe('Discovery', () => {
});
});

describe('discovery parameters =>', () => {
fit('checks for waitForTimeout and waitForSelector at discovery', async () => {
percy.loglevel('debug');

await percy.snapshot({
name: 'test discovery',
url: 'http://localhost:8000',
enableJavaScript: true,
discovery: {
waitForTimeout: 100,
waitForSelector: 'body'
}
});

await percy.idle();

expect(logger.stderr).toEqual(jasmine.arrayContaining([
'[percy:core:discovery] Wait for selector: body',
'[percy:core:discovery] Wait for 100ms timeout'
]));
});
});

describe('Capture image srcset =>', () => {
it('make request call to capture resource', async () => {
let responsiveDOM = dedent`
Expand Down

0 comments on commit 6a71cc4

Please sign in to comment.