From 606d1627a1832af7eaa7980eb2b68a196199defa Mon Sep 17 00:00:00 2001 From: amit3200 Date: Thu, 26 Oct 2023 18:43:59 +0530 Subject: [PATCH 1/3] Removing Redundant Calls for PoA --- packages/webdriver-utils/src/driver.js | 26 +++++++++---------- packages/webdriver-utils/src/index.js | 1 + .../src/metadata/desktopMetaData.js | 2 +- .../src/providers/automateProvider.js | 4 --- packages/webdriver-utils/test/driver.test.js | 14 ++++++++++ .../test/metadata/desktopMetaData.test.js | 2 +- .../test/providers/automateProvider.test.js | 5 ---- 7 files changed, 29 insertions(+), 25 deletions(-) diff --git a/packages/webdriver-utils/src/driver.js b/packages/webdriver-utils/src/driver.js index 950720173..87760778e 100644 --- a/packages/webdriver-utils/src/driver.js +++ b/packages/webdriver-utils/src/driver.js @@ -10,6 +10,16 @@ export default class Driver { this.passedCapabilities = passedCapabilities; } + static requestPostOptions(command) { + return { + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8' + }, + body: JSON.stringify(command) + }; + } + async getCapabilites() { return await Cache.withCache(Cache.caps, this.sessionId, async () => { try { @@ -43,13 +53,7 @@ export default class Driver { if (!command.script.includes('browserstack_executor')) { command.script = `/* percy_automate_script */ \n ${command.script}`; } - const options = { - method: 'POST', - headers: { - 'Content-Type': 'application/json;charset=utf-8' - }, - body: JSON.stringify(command) - }; + const options = Driver.requestPostOptions(command); const baseUrl = `${this.executorUrl}/session/${this.sessionId}/execute/sync`; const response = JSON.parse((await request(baseUrl, options)).body); return response; @@ -68,13 +72,7 @@ export default class Driver { } async findElement(using, value) { - const options = { - method: 'POST', - headers: { - 'Content-Type': 'application/json;charset=utf-8' - }, - body: JSON.stringify({ using, value }) - }; + const options = Driver.requestPostOptions({ using, value }); const baseUrl = `${this.executorUrl}/session/${this.sessionId}/element`; const response = JSON.parse((await request(baseUrl, options)).body); return response.value; diff --git a/packages/webdriver-utils/src/index.js b/packages/webdriver-utils/src/index.js index 7f20b2415..e977cf4ac 100644 --- a/packages/webdriver-utils/src/index.js +++ b/packages/webdriver-utils/src/index.js @@ -24,6 +24,7 @@ export default class WebdriverUtils { const comparisonData = await automate.screenshot(snapshotName, options); comparisonData.metadata.cliScreenshotStartTime = startTime; comparisonData.metadata.cliScreenshotEndTime = Date.now(); + log.debug(`[${snapshotName}] : Comparison Data: ${JSON.stringify(comparisonData)}`); return comparisonData; } catch (e) { log.error(`[${snapshotName}] : Error - ${e.message}`); diff --git a/packages/webdriver-utils/src/metadata/desktopMetaData.js b/packages/webdriver-utils/src/metadata/desktopMetaData.js index ef0fd1653..67df56131 100644 --- a/packages/webdriver-utils/src/metadata/desktopMetaData.js +++ b/packages/webdriver-utils/src/metadata/desktopMetaData.js @@ -49,7 +49,7 @@ export default class DesktopMetaData { async screenResolution() { return await Cache.withCache(Cache.resolution, this.driver.sessionId, async () => { - const data = await this.driver.executeScript({ script: 'return [(window.screen.width * window.devicePixelRatio).toString(), (window.screen.height * window.devicePixelRatio).toString()];', args: [] }); + const data = await this.driver.executeScript({ script: 'return [parseInt(window.screen.width * window.devicePixelRatio).toString(), parseInt(window.screen.height * window.devicePixelRatio).toString()];', args: [] }); const screenInfo = data.value; return `${screenInfo[0]} x ${screenInfo[1]}`; }); diff --git a/packages/webdriver-utils/src/providers/automateProvider.js b/packages/webdriver-utils/src/providers/automateProvider.js index 98f6de64d..5c3b40fd3 100644 --- a/packages/webdriver-utils/src/providers/automateProvider.js +++ b/packages/webdriver-utils/src/providers/automateProvider.js @@ -187,10 +187,6 @@ export default class AutomateProvider extends GenericProvider { const resolution = await this.metaData.screenResolution(); const orientation = (this.metaData.orientation() || automateCaps.deviceOrientation)?.toLowerCase(); - // for android window size only constitutes of browser viewport, hence adding nav / status / url bar heights - [this.header, this.footer] = await this.getHeaderFooter(deviceName, osVersion, browserName); - height = this.metaData.device() && osName?.toLowerCase() === 'android' ? height + this.header + this.footer : height; - return { name: deviceName, osName, diff --git a/packages/webdriver-utils/test/driver.test.js b/packages/webdriver-utils/test/driver.test.js index 6f513e2b6..74dc58b79 100644 --- a/packages/webdriver-utils/test/driver.test.js +++ b/packages/webdriver-utils/test/driver.test.js @@ -161,4 +161,18 @@ describe('Driver', () => { expect(res).toEqual('mockVal'); }); }); + + describe('requestPostOptions', () => { + const command = { simple: 'test' }; + const expectedResponse = { + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8' + }, + body: JSON.stringify(command) + }; + it('returns post options', () => { + expect(Driver.requestPostOptions(command)).toEqual(expectedResponse); + }); + }); }); diff --git a/packages/webdriver-utils/test/metadata/desktopMetaData.test.js b/packages/webdriver-utils/test/metadata/desktopMetaData.test.js index 9bec6dc39..4088405c4 100644 --- a/packages/webdriver-utils/test/metadata/desktopMetaData.test.js +++ b/packages/webdriver-utils/test/metadata/desktopMetaData.test.js @@ -108,7 +108,7 @@ describe('DesktopMetaData', () => { screenInfo = await desktopMetaData.screenResolution(); expect(screenInfo).toEqual('1980 x 1080'); expect(executeScriptSpy) - .toHaveBeenCalledWith({ script: 'return [(window.screen.width * window.devicePixelRatio).toString(), (window.screen.height * window.devicePixelRatio).toString()];', args: [] }); + .toHaveBeenCalledWith({ script: 'return [parseInt(window.screen.width * window.devicePixelRatio).toString(), parseInt(window.screen.height * window.devicePixelRatio).toString()];', args: [] }); }); }); diff --git a/packages/webdriver-utils/test/providers/automateProvider.test.js b/packages/webdriver-utils/test/providers/automateProvider.test.js index 5dc205913..c8972fd7c 100644 --- a/packages/webdriver-utils/test/providers/automateProvider.test.js +++ b/packages/webdriver-utils/test/providers/automateProvider.test.js @@ -244,7 +244,6 @@ describe('AutomateProvider', () => { let windowSizeSpy; let orientationSpy; let resolutionSpy; - let getHeaderFooterSpy; let percyBuildInfo = { id: '123', url: 'https://percy.io/abc/123' @@ -256,7 +255,6 @@ describe('AutomateProvider', () => { percyScreenshotBeginSpy = spyOn(AutomateProvider.prototype, 'percyScreenshotBegin').and.returnValue({ value: '{"buildHash":"12e3","sessionHash":"abc1d","capabilities":{"browserName":"chrome","browserVersion":"113.0","os":"win11","os_version":"11","deviceOrientation":false,"resolution":["1920","1080"]},"success":true,"deviceName":"x.x.x.x"}' }); spyOn(Driver.prototype, 'getCapabilites'); - getHeaderFooterSpy = spyOn(GenericProvider.prototype, 'getHeaderFooter').and.returnValue(Promise.resolve([0, 0])); windowSizeSpy = spyOn(DesktopMetaData.prototype, 'windowSize') .and.returnValue(Promise.resolve({ width: 1000, height: 1000 })); resolutionSpy = spyOn(DesktopMetaData.prototype, 'screenResolution') @@ -275,7 +273,6 @@ describe('AutomateProvider', () => { expect(windowSizeSpy).toHaveBeenCalledTimes(1); expect(resolutionSpy).toHaveBeenCalledTimes(1); expect(orientationSpy).toHaveBeenCalledTimes(1); - expect(getHeaderFooterSpy).toHaveBeenCalledTimes(1); expect(res).toEqual({ name: 'Windows_11_chrome_113', osName: 'Windows', @@ -296,7 +293,6 @@ describe('AutomateProvider', () => { percyScreenshotBeginSpy = spyOn(AutomateProvider.prototype, 'percyScreenshotBegin').and.returnValue({ value: '{"buildHash":"12e3","sessionHash":"abc1d","capabilities":{"browserName":"chrome_android","browserVersion":"chrome_android","os":"android","os_version":"11","deviceOrientation":"portrait","resolution":["1920","1080"]},"success":true,"deviceName":"Samsung Galaxy S21"}' }); spyOn(Driver.prototype, 'getCapabilites'); - getHeaderFooterSpy = spyOn(GenericProvider.prototype, 'getHeaderFooter').and.returnValue(Promise.resolve([0, 0])); windowSizeSpy = spyOn(MobileMetaData.prototype, 'windowSize') .and.returnValue(Promise.resolve({ width: 1000, height: 1000 })); resolutionSpy = spyOn(MobileMetaData.prototype, 'screenResolution') @@ -315,7 +311,6 @@ describe('AutomateProvider', () => { expect(windowSizeSpy).toHaveBeenCalledTimes(1); expect(resolutionSpy).toHaveBeenCalledTimes(1); expect(orientationSpy).toHaveBeenCalledTimes(1); - expect(getHeaderFooterSpy).toHaveBeenCalledTimes(1); expect(res).toEqual({ name: 'Samsung Galaxy S21', osName: 'Android', From 0664cf40123502d57cb851acf3f6f3ddd67f923c Mon Sep 17 00:00:00 2001 From: amit3200 Date: Thu, 26 Oct 2023 19:32:58 +0530 Subject: [PATCH 2/3] Removing call from genericProvider --- .../src/providers/genericProvider.js | 21 +----- .../test/providers/automateProvider.test.js | 1 - .../test/providers/genericProvider.test.js | 67 +------------------ 3 files changed, 2 insertions(+), 87 deletions(-) diff --git a/packages/webdriver-utils/src/providers/genericProvider.js b/packages/webdriver-utils/src/providers/genericProvider.js index bacf51903..04ac51d1d 100644 --- a/packages/webdriver-utils/src/providers/genericProvider.js +++ b/packages/webdriver-utils/src/providers/genericProvider.js @@ -3,10 +3,7 @@ import utils from '@percy/sdk-utils'; import MetaDataResolver from '../metadata/metaDataResolver.js'; import Tile from '../util/tile.js'; import Driver from '../driver.js'; -import Cache from '../util/cache.js'; -const { request } = utils; -const DEVICES_CONFIG_URL = 'https://storage.googleapis.com/percy-utils/devices.json'; const log = utils.logger('webdriver-utils:genericProvider'); export default class GenericProvider { @@ -156,9 +153,7 @@ export default class GenericProvider { let { width, height } = await this.metaData.windowSize(); const resolution = await this.metaData.screenResolution(); const orientation = this.metaData.orientation(); - [this.header, this.footer] = await this.getHeaderFooter(); - // for android window size only constitutes of browser viewport, hence adding nav / status / url bar heights - height = this.metaData.osName() === 'android' ? height + this.header + this.footer : height; + return { name: this.metaData.deviceName(), osName: this.metaData.osName(), @@ -268,18 +263,4 @@ export default class GenericProvider { } return elementsArray; } - - async getHeaderFooter(deviceName, osVersion, browserName) { - // passing 0 as key, since across different pages and tests, this config will remain same - const devicesConfig = await Cache.withCache(Cache.devicesConfig, 0, async () => { - return (await request(DEVICES_CONFIG_URL)).body; - }); - let deviceKey = `${deviceName}-${osVersion}`; - return devicesConfig[deviceKey] - ? ( - devicesConfig[deviceKey][browserName] - ? [devicesConfig[deviceKey][browserName].header, devicesConfig[deviceKey][browserName].footer] - : [0, 0] - ) : [0, 0]; - } } diff --git a/packages/webdriver-utils/test/providers/automateProvider.test.js b/packages/webdriver-utils/test/providers/automateProvider.test.js index c8972fd7c..6f0d0fca7 100644 --- a/packages/webdriver-utils/test/providers/automateProvider.test.js +++ b/packages/webdriver-utils/test/providers/automateProvider.test.js @@ -201,7 +201,6 @@ describe('AutomateProvider', () => { beforeEach(async () => { spyOn(Driver.prototype, 'getCapabilites'); - spyOn(GenericProvider.prototype, 'getHeaderFooter').and.returnValue(Promise.resolve([123, 456])); browserstackExecutorSpy = spyOn(AutomateProvider.prototype, 'browserstackExecutor') .and.returnValue(Promise.resolve({ value: '{ "result": "{\\"dom_sha\\": \\"abc\\", \\"sha\\": [\\"abc-1\\", \\"xyz-2\\"]}", "success":true }' })); executeScriptSpy = spyOn(Driver.prototype, 'executeScript') diff --git a/packages/webdriver-utils/test/providers/genericProvider.test.js b/packages/webdriver-utils/test/providers/genericProvider.test.js index ccecda797..5b2116813 100644 --- a/packages/webdriver-utils/test/providers/genericProvider.test.js +++ b/packages/webdriver-utils/test/providers/genericProvider.test.js @@ -36,7 +36,6 @@ describe('GenericProvider', () => { describe('getTiles', () => { beforeEach(() => { spyOn(Driver.prototype, 'takeScreenshot').and.returnValue(Promise.resolve('123b=')); - spyOn(GenericProvider.prototype, 'getHeaderFooter').and.returnValue(Promise.resolve([123, 456])); spyOn(GenericProvider.prototype, 'getWindowHeight').and.returnValue(Promise.resolve(1947)); }); @@ -90,7 +89,6 @@ describe('GenericProvider', () => { .and.returnValue('111'); spyOn(MobileMetaData.prototype, 'screenResolution') .and.returnValue('1980 x 1080'); - spyOn(GenericProvider.prototype, 'getHeaderFooter').and.returnValue(Promise.resolve([123, 456])); }); it('returns correct tag for android', async () => { @@ -103,7 +101,7 @@ describe('GenericProvider', () => { osName: 'android', osVersion: 'mockOsVersion', width: 1000, - height: 1000 + 123 + 456, + height: 1000, orientation: 'landscape', browserName: 'mockBrowserName', browserVersion: '111', @@ -346,67 +344,4 @@ describe('GenericProvider', () => { expect(elementsArray).toEqual([]); }); }); - - describe('getHeaderFooter', () => { - let provider; - - beforeEach(async () => { - provider = new GenericProvider('123', 'http:executorUrl', { browserName: 'safari', deviceName: 'iPhone 12 Pro', platform: 'iOS' }, {}); - spyOn(MobileMetaData.prototype, 'deviceName').and.returnValue('iPhone 12 Pro'); - spyOn(MobileMetaData.prototype, 'osVersion').and.returnValue('13'); - }); - - it('should return the matching header and footer', async () => { - await provider.createDriver(); - let mockResponseObject = { - body: { - 'iPhone 12 Pro-13': { - safari: { - header: 141, - footer: 399 - } - } - }, - status: 200, - headers: { 'content-type': 'application/json' } - }; - spyOn(utils.request, 'fetch').and.returnValue( - Promise.resolve(mockResponseObject) - ); - const [header, footer] = await provider.getHeaderFooter('iPhone 12 Pro', '13', 'safari'); - expect(header).toEqual(141); - expect(footer).toEqual(399); - }); - - it('should return 0,0 for unmatched device name', async () => { - await provider.createDriver(); - let mockResponseObject = { - 'iPhone 13 Pro-14': {} - }; - spyOn(Cache, 'withCache').and.returnValue( - Promise.resolve(mockResponseObject) - ); - const [header, footer] = await provider.getHeaderFooter('iPhone 13 Pro', '14', 'safari'); - expect(header).toEqual(0); - expect(footer).toEqual(0); - }); - - it('should return 0,0 for unmatched browser name', async () => { - await provider.createDriver(); - let mockResponseObject = { - 'iPhone 12 Pro-13': { - chrome: { - header: 141, - footer: 399 - } - } - }; - spyOn(Cache, 'withCache').and.returnValue( - Promise.resolve(mockResponseObject) - ); - const [header, footer] = await provider.getHeaderFooter(); - expect(header).toEqual(0); - expect(footer).toEqual(0); - }); - }); }); From 564e13bd872cb3e7541e646a70c458a3cebf450b Mon Sep 17 00:00:00 2001 From: amit3200 Date: Thu, 26 Oct 2023 19:44:05 +0530 Subject: [PATCH 3/3] Fixing lint --- packages/webdriver-utils/test/providers/genericProvider.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/webdriver-utils/test/providers/genericProvider.test.js b/packages/webdriver-utils/test/providers/genericProvider.test.js index 5b2116813..95aca30b6 100644 --- a/packages/webdriver-utils/test/providers/genericProvider.test.js +++ b/packages/webdriver-utils/test/providers/genericProvider.test.js @@ -2,9 +2,7 @@ import GenericProvider from '../../src/providers/genericProvider.js'; import Driver from '../../src/driver.js'; import MetaDataResolver from '../../src/metadata/metaDataResolver.js'; import DesktopMetaData from '../../src/metadata/desktopMetaData.js'; -import Cache from '../../src/util/cache.js'; import MobileMetaData from '../../src/metadata/mobileMetaData.js'; -import utils from '@percy/sdk-utils'; describe('GenericProvider', () => { let genericProvider;