Skip to content

Commit

Permalink
adding return statement for poa and updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prklm10 committed Jan 28, 2024
1 parent 6beda7a commit ef21f98
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,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.data;
}
const provider = ProviderResolver.resolve(driver);
const response = await provider.screenshot(name, {
Expand All @@ -106,7 +107,7 @@ module.exports = async function percyScreenshot(driver, name, options = {}) {
sync
});
log.debug(`[${name}] -> end`);
return response;
return response.data;
} catch (e) {
log.error(`[${name}] failed to take screenshot`);
log.debug(`[${name}] ${e}, \n ${e.stack}`);
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
2 changes: 1 addition & 1 deletion percy/providers/appAutomateProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AppAutomateProvider extends GenericProvider {
} = {}) {
let response = null;
let error;
sync = sync || false;
sync = sync || null;
try {
let result = await this.percyScreenshotBegin(name);
this.setDebugUrl(result);
Expand Down
2 changes: 1 addition & 1 deletion percy/providers/genericProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GenericProvider {
sync
}) {
fullscreen = fullscreen || false;
sync = sync || false;
sync = sync || null;

this.metadata = await MetadataResolver.resolve(this.driver, {
deviceName,
Expand Down
26 changes: 18 additions & 8 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -244,36 +244,44 @@ 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 () => {
it('should call POA percyScreenshot with ignoreRegion and considerRegion and sync', async () => {
const element = { value: '123', elementId: '123' };
const element2 = { value: '456', elementId: '456' };
const driver = driverFunc({ enabled: true });
spyOn(percyScreenshot, 'isPercyEnabled').and.returnValue(Promise.resolve(true))
utils.percy.type = 'automate';
spyOn(percyOnAutomate, 'request').and.callFake(() => {});
const mockresponse = {
success: 'true',
data: 'sync_data'
};
const mockedPostCall = spyOn(percyOnAutomate, 'request').and.callFake(() => mockresponse);

await percyScreenshot(driver, 'Screenshot 2', {
ignore_region_appium_elements: [element], consider_region_appium_elements: [element]
ignore_region_appium_elements: [element], consider_region_appium_elements: [element], sync: true
});
expect(percyOnAutomate.request).toHaveBeenCalledWith(jasmine.objectContaining({
expect(mockedPostCall).toHaveBeenCalledWith(jasmine.objectContaining({
sessionId: 'sessionID',
commandExecutorUrl: 'https://localhost/wd/hub',
snapshotName: 'Screenshot 2',
options: {
ignore_region_elements: ['123'],
consider_region_elements: ['123']
consider_region_elements: ['123'],
sync: true
}
}));

await percyScreenshot(driver, 'Screenshot 3', {
const data = await percyScreenshot(driver, 'Screenshot 3', {
ignoreRegionAppiumElements: [element2], considerRegionAppiumElements: [element2]
});
expect(percyOnAutomate.request).toHaveBeenCalledWith(jasmine.objectContaining({
Expand All @@ -285,6 +293,8 @@ describe('percyScreenshot', () => {
consider_region_elements: ['456']
}
}));

expect(data).toEqual(mockresponse.data);
});

it('should handle error POA', async () => {
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', false, '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, false, `Error: ${errorMessage}`);
'abc', undefined, null, `Error: ${errorMessage}`);
});
});

Expand Down

0 comments on commit ef21f98

Please sign in to comment.