Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sync params to screenshot call. #237

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = async function percyScreenshot(driver, name, options = {}) {
scrollableXpath,
topScrollviewOffset,
bottomScrollviewOffset,
scrollableId
scrollableId,
sync
} = options;
// allow working with or without standalone mode for wdio
if (!driver || typeof driver === 'string') {
Expand All @@ -54,6 +55,7 @@ module.exports = async function percyScreenshot(driver, name, options = {}) {
topScrollviewOffset = name.topScrollviewOffset;
bottomScrollviewOffset = name.bottomScrollviewOffset;
scrollableId = name.scrollableId;
sync = name.sync;
options = name;
}
try {
Expand All @@ -78,7 +80,8 @@ module.exports = async function percyScreenshot(driver, name, options = {}) {
return TimeIt.run('percyScreenshot', async () => {
try {
if (utils.percy?.type === 'automate') {
return await percyOnAutomate(driver, name, options);
const percyOnAutomateResponse = await percyOnAutomate(driver, name, options);
return percyOnAutomateResponse?.body?.data;
}
const provider = ProviderResolver.resolve(driver);
const response = await provider.screenshot(name, {
Expand All @@ -100,10 +103,11 @@ module.exports = async function percyScreenshot(driver, name, options = {}) {
scrollableXpath,
topScrollviewOffset,
bottomScrollviewOffset,
scrollableId
scrollableId,
sync
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add return statement inside percyOnAutomate function as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

});
log.debug(`[${name}] -> end`);
return response;
return response?.body?.data;
} catch (e) {
log.error(`[${name}] failed to take screenshot`);
log.debug(`[${name}] ${e}, \n ${e.stack}`);
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": "@percy/appium-app",
"description": "Appium client library for visual testing with Percy",
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"author": "Perceptual Inc.",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions percy/percyOnAutomate.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = async function percyOnAutomate(driver, name, options) {
}

// Post the driver details to the automate screenshot endpoint with snapshot options and other info
await module.exports.request({
return await module.exports.request({
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO,
sessionId,
Expand All @@ -78,5 +78,5 @@ module.exports = async function percyOnAutomate(driver, name, options) {

/* istanbul ignore next */ // since can't test this function
module.exports.request = async function request(data) {
await utils.captureAutomateScreenshot(data);
return await utils.captureAutomateScreenshot(data);
}; // To mock in test case
17 changes: 11 additions & 6 deletions percy/providers/appAutomateProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class AppAutomateProvider extends GenericProvider {
scrollableXpath,
topScrollviewOffset,
bottomScrollviewOffset,
scrollableId
scrollableId,
sync
} = {}) {
let response = null;
let error;
sync = sync || null;
try {
let result = await this.percyScreenshotBegin(name);
this.setDebugUrl(result);
Expand All @@ -60,13 +62,14 @@ class AppAutomateProvider extends GenericProvider {
scrollableXpath,
topScrollviewOffset,
bottomScrollviewOffset,
scrollableId
scrollableId,
sync
});
} catch (e) {
error = e;
throw e;
} finally {
await this.percyScreenshotEnd(name, response?.body?.link, `${error}`);
await this.percyScreenshotEnd(name, response?.body?.link, sync, `${error}`);
}
return response;
}
Expand All @@ -84,19 +87,21 @@ class AppAutomateProvider extends GenericProvider {
return result;
} catch (e) {
log.debug(`[${name}] Could not mark App Automate session as percy`);
log.debug(`[${name}] ${e}`);
return null;
}
});
}

async percyScreenshotEnd(name, percyScreenshotUrl, statusMessage = null) {
async percyScreenshotEnd(name, percyScreenshotUrl, sync, statusMessage = null) {
return await TimeIt.run('percyScreenshotEnd', async () => {
try {
await this.browserstackExecutor('percyScreenshot', {
name,
percyScreenshotUrl,
status: percyScreenshotUrl ? 'success' : 'failure',
statusMessage,
sync,
state: 'end'
});
} catch (e) {
Expand Down Expand Up @@ -155,8 +160,8 @@ class AppAutomateProvider extends GenericProvider {
JSON.parse(response.result).forEach(tileData => {
tiles.push(new Tile({
statusBarHeight: statBarHeight,
navBarHeight: navBarHeight,
fullscreen: fullscreen,
navBarHeight,
fullscreen,
headerHeight: tileData.header_height,
footerHeight: tileData.footer_height,
sha: tileData.sha.split('-')[0] // drop build id
Expand Down
8 changes: 6 additions & 2 deletions percy/providers/genericProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ class GenericProvider {
considerRegionAppiumElements,
customConsiderRegions,
scrollableXpath,
scrollableId
scrollableId,
sync
}) {
fullscreen = fullscreen || false;
sync = sync || null;

this.metadata = await MetadataResolver.resolve(this.driver, {
deviceName,
Expand All @@ -76,6 +78,7 @@ class GenericProvider {
log.debug(`${name} : Tag ${JSON.stringify(tag)}`);
log.debug(`${name} : Tiles ${JSON.stringify(tiles)}`);
log.debug(`${name} : Debug url ${this.debugUrl}`);
log.debug(`${name} : sync ${sync}`);
return await utils.postComparison({
name,
tag,
Expand All @@ -88,7 +91,8 @@ class GenericProvider {
considerElementsData: considerRegions
},
environmentInfo: ENV_INFO,
clientInfo: CLIENT_INFO
clientInfo: CLIENT_INFO,
sync
});
}

Expand Down
21 changes: 19 additions & 2 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,15 @@ describe('percyScreenshot', () => {
const driver = driverFunc({ enabled: true });
spyOn(percyScreenshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true))
utils.percy.type = 'automate';
spyOn(percyOnAutomate, 'request').and.callFake(() => {});
const mockresponse = {};
spyOn(percyOnAutomate, 'request').and.callFake(() => mockresponse);

await percyScreenshot(driver, 'Screenshot 1');
const response = await percyScreenshot(driver, 'Screenshot 1');
expect(percyOnAutomate.request).toHaveBeenCalledWith(jasmine.objectContaining({
sessionId: 'sessionID', commandExecutorUrl: 'https://localhost/wd/hub', snapshotName: 'Screenshot 1'
}));

expect(response).toEqual(undefined);
});

it('should call POA percyScreenshot with ignoreRegion and considerRegion', async () => {
Expand Down Expand Up @@ -287,6 +290,20 @@ describe('percyScreenshot', () => {
}));
});

it('should return CLI response', async() => {
const mockResponse = {
success: true,
body: { data: { name: 'test_snapshot', some_data: 'some_data', some_obj: { some_obj: 'some_obj' }}}
}
const driver = driverFunc({ enabled: true });
spyOn(percyScreenshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true))
utils.percy.type = 'automate';

spyOn(percyOnAutomate, 'request').and.callFake(() => mockResponse);
const response = await percyScreenshot(driver, 'Screenshot 1', { sync: true })
expect(response).toEqual(mockResponse.body.data)
})

it('should handle error POA', async () => {
const driver = driverFunc({ enabled: true, ignoreErrors: false });
spyOn(percyScreenshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true))
Expand Down
4 changes: 2 additions & 2 deletions test/percy/providers/appAutomateProvider.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('AppAutomateProvider', () => {

expect(percyScreenshotBeginSpy).toHaveBeenCalledWith('abc');
expect(superScreenshotSpy).toHaveBeenCalledWith('abc', jasmine.any(Object));
expect(percyScreenshotEndSpy).toHaveBeenCalledWith('abc', 'link to screenshot', 'undefined');
expect(percyScreenshotEndSpy).toHaveBeenCalledWith('abc', 'link to screenshot', null, 'undefined');
});

it('passes exception message to percyScreenshotEnd in case of exception', async () => {
Expand All @@ -47,7 +47,7 @@ describe('AppAutomateProvider', () => {
expect(percyScreenshotBeginSpy).toHaveBeenCalledWith('abc');

expect(percyScreenshotEndSpy).toHaveBeenCalledWith(
'abc', undefined, `Error: ${errorMessage}`);
'abc', undefined, null, `Error: ${errorMessage}`);
});
});

Expand Down
Loading
Loading