From 782688a2e4a49941927c86797267ad1fd80f6597 Mon Sep 17 00:00:00 2001 From: rishigupta1599 Date: Thu, 14 Dec 2023 00:38:09 +0530 Subject: [PATCH 1/6] Fixing ignoreRegions for POA Full Page --- .../src/providers/genericProvider.js | 59 +++++++++++++------ 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/packages/webdriver-utils/src/providers/genericProvider.js b/packages/webdriver-utils/src/providers/genericProvider.js index 2d13c688a..5d31fd1a9 100644 --- a/packages/webdriver-utils/src/providers/genericProvider.js +++ b/packages/webdriver-utils/src/providers/genericProvider.js @@ -38,7 +38,7 @@ export default class GenericProvider { this.pageYShiftFactor = 0; this.currentTag = null; this.removeElementShiftFactor = 50000; - this.initialScrollFactor = { value: [0, 0] }; + this.initialScrollFactor = null; } addDefaultOptions() { @@ -69,15 +69,27 @@ export default class GenericProvider { } } + async getScrollDetails() { + return await this.driver.executeScript({ script: 'return [parseInt(window.scrollX), parseInt(window.scrollY)];', args: [] }); + } + + async getInitialScrollFactor() { + if (this.initialScrollFactor) { + return this.initialScrollFactor; + } + this.initialScrollFactor = await this.getScrollDetails(); + return this.initialScrollFactor; + } + async getInitialPosition() { if (this.currentTag.osName === 'iOS') { - this.initialScrollFactor = await this.driver.executeScript({ script: 'return [parseInt(window.scrollX), parseInt(window.scrollY)];', args: [] }); + this.initialScrollFactor = await this.getInitialScrollFactor(); } } - async scrollToInitialPosition(x, y) { + async scrollToInitialPosition() { if (this.currentTag.osName === 'iOS') { - await this.driver.executeScript({ script: `window.scrollTo(${x}, ${y})`, args: [] }); + await this.driver.executeScript({ script: `window.scrollTo(${this.initialScrollFactor.value[0]}, ${this.initialScrollFactor.value[1]})`, args: [] }); } } @@ -245,15 +257,22 @@ export default class GenericProvider { async getRegionObjectFromBoundingBox(selector, element) { const scaleFactor = await this.metaData.devicePixelRatio(); + let scrollX = 0, scrollY = 0; + if (this.options?.fullPage) { + const scrollParams = await this.getInitialScrollFactor(); + scrollX = scrollParams.value[0]; + scrollY = scrollParams.value[1]; + } + let headerAdjustment = 0; if (this.currentTag.osName === 'iOS') { headerAdjustment = this.statusBarHeight; } const coOrdinates = { - top: Math.floor(element.y * scaleFactor) + Math.floor(headerAdjustment), - bottom: Math.ceil((element.y + element.height) * scaleFactor) + Math.ceil(headerAdjustment), - left: Math.floor(element.x * scaleFactor), - right: Math.ceil((element.x + element.width) * scaleFactor) + top: Math.floor((element.y + scrollY) * scaleFactor) + Math.floor(headerAdjustment), + bottom: Math.ceil((element.y + element.height + scrollY) * scaleFactor) + Math.ceil(headerAdjustment), + left: Math.floor((element.x + scrollX) * scaleFactor), + right: Math.ceil((element.x + element.width + scrollX) * scaleFactor) }; const jsonObject = { @@ -280,15 +299,14 @@ export default class GenericProvider { return regionsArray; } - async updatePageShiftFactor(location, scaleFactor) { - const scrollFactors = await this.driver.executeScript({ script: 'return [parseInt(window.scrollX), parseInt(window.scrollY)];', args: [] }); + async updatePageShiftFactor(location, scaleFactor, scrollFactors) { if (this.currentTag.osName === 'iOS' || (this.currentTag.osName === 'OS X' && parseInt(this.currentTag.browserVersion) > 13 && this.currentTag.browserName.toLowerCase() === 'safari')) { this.pageYShiftFactor = this.statusBarHeight; } else { this.pageYShiftFactor = this.statusBarHeight - (scrollFactors.value[1] * scaleFactor); } this.pageXShiftFactor = this.currentTag.osName === 'iOS' ? 0 : (-(scrollFactors.value[0] * scaleFactor)); - if (this.currentTag.osName === 'iOS') { + if (this.currentTag.osName === 'iOS' && !this.options?.fullPage) { if (scrollFactors.value[0] !== this.initialScrollFactor.value[0] || scrollFactors.value[1] !== this.initialScrollFactor.value[1]) { this.pageXShiftFactor = (-1 * this.removeElementShiftFactor); this.pageYShiftFactor = (-1 * this.removeElementShiftFactor); @@ -303,16 +321,23 @@ export default class GenericProvider { const rect = await this.driver.rect(elementId); const location = { x: rect.x, y: rect.y }; const size = { height: rect.height, width: rect.width }; + let scrollX = 0, scrollY = 0; + const scrollFactors = await this.getScrollDetails(); + if (this.options?.fullPage) { + scrollX = scrollFactors.value[0]; + scrollY = scrollFactors.value[1]; + } + // Update pageShiftFactor Element is not visible in viewport // In case of iOS if the element is not visible in viewport it gives 0 for x-y coordinate. // In case of iOS if the element is partially visible it gives negative x-y coordinate. // Subtracting ScrollY/ScrollX ensures if the element is visible in viewport or not. - await this.updatePageShiftFactor(location, scaleFactor); + await this.updatePageShiftFactor(location, scaleFactor, scrollFactors); const coOrdinates = { - top: Math.floor(location.y * scaleFactor) + Math.floor(this.pageYShiftFactor), - bottom: Math.ceil((location.y + size.height) * scaleFactor) + Math.ceil(this.pageYShiftFactor), - left: Math.floor(location.x * scaleFactor) + Math.floor(this.pageXShiftFactor), - right: Math.ceil((location.x + size.width) * scaleFactor) + Math.ceil(this.pageXShiftFactor) + top: Math.floor((location.y + scrollY) * scaleFactor) + Math.floor(this.pageYShiftFactor), + bottom: Math.ceil((location.y + size.height + scrollY) * scaleFactor) + Math.ceil(this.pageYShiftFactor), + left: Math.floor((location.x + scrollX) * scaleFactor) + Math.floor(this.pageXShiftFactor), + right: Math.ceil((location.x + size.width + scrollX) * scaleFactor) + Math.ceil(this.pageXShiftFactor) }; const jsonObject = { @@ -336,7 +361,7 @@ export default class GenericProvider { log.debug(e.toString()); } } - await this.scrollToInitialPosition(this.initialScrollFactor.value[0], this.initialScrollFactor.value[1]); + await this.scrollToInitialPosition(); return regionsArray; } From 82a9e52cdb6f968e7ba674545829f20a652a0f78 Mon Sep 17 00:00:00 2001 From: rishigupta1599 Date: Thu, 14 Dec 2023 14:54:58 +0530 Subject: [PATCH 2/6] Adding specs --- .../test/providers/genericProvider.test.js | 438 +++++++++++++----- 1 file changed, 322 insertions(+), 116 deletions(-) diff --git a/packages/webdriver-utils/test/providers/genericProvider.test.js b/packages/webdriver-utils/test/providers/genericProvider.test.js index 41dd140f5..9befb1983 100644 --- a/packages/webdriver-utils/test/providers/genericProvider.test.js +++ b/packages/webdriver-utils/test/providers/genericProvider.test.js @@ -266,12 +266,12 @@ describe('GenericProvider', () => { describe('updatePageShiftFactor', () => { let provider; - - describe('When iOS', () => { + let scrollFactors; + describe('When iOS singlepage screenshot', () => { beforeEach(async () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); await provider.createDriver(); - spyOn(Driver.prototype, 'executeScript').and.returnValue({ value: [0, 10] }); + scrollFactors = { value: [0, 10] }; provider.currentTag = { osName: 'iOS' }; provider.pageYShiftFactor = 0; provider.statusBarHeight = 0; @@ -282,13 +282,13 @@ describe('GenericProvider', () => { provider.initialScrollFactor = { value: [0, 10] }; }); it('should update pageYShiftFactor for iOS when location.y is 0', async () => { - await provider.updatePageShiftFactor({ y: 0 }, 2); + await provider.updatePageShiftFactor({ y: 0 }, 2, scrollFactors); expect(provider.pageYShiftFactor).toBe(-20); }); it('should not update pageYShiftFactor for iOS when location.y is not 0', async () => { // Location.y is not 0 - await provider.updatePageShiftFactor({ y: 5 }, 2); + await provider.updatePageShiftFactor({ y: 5 }, 2, scrollFactors); expect(provider.pageYShiftFactor).toBe(0); }); }); @@ -298,23 +298,43 @@ describe('GenericProvider', () => { provider.initialScrollFactor = { value: [0, 30] }; }); it('should update pageYShiftFactor to negative value even if location.y is 0', async () => { - await provider.updatePageShiftFactor({ y: 0 }, 2); + await provider.updatePageShiftFactor({ y: 0 }, 2, scrollFactors); expect(provider.pageYShiftFactor).toBe(-50000); }); it('should update pageYShiftFactor to negative value even if location.y is not 0', async () => { // Location.y is not 0 - await provider.updatePageShiftFactor({ y: 5 }, 2); + await provider.updatePageShiftFactor({ y: 5 }, 2, scrollFactors); expect(provider.pageYShiftFactor).toBe(-50000); }); }); }); - describe('When OS X', () => { + describe('When iOS fullpage screenshot', () => { + beforeEach(async () => { + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); + await provider.createDriver(); + scrollFactors = { value: [0, 10] }; + provider.currentTag = { osName: 'iOS' }; + provider.pageYShiftFactor = 0; + }); + + describe('when element is present in DOM', () => { + beforeEach(() => { + provider.statusBarHeight = 10; + }); + it('should update pageYShiftFactor for iOS to statusBarHeight', async () => { + await provider.updatePageShiftFactor({ y: 0 }, 2, scrollFactors); + expect(provider.pageYShiftFactor).toBe(10); + }); + }); + }); + + describe('When OS X singlepage', () => { beforeEach(async () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'OS X' }, {}); await provider.createDriver(); - spyOn(Driver.prototype, 'executeScript').and.returnValue({ value: [0, 10] }); + scrollFactors = { value: [0, 10] }; provider.currentTag = { osName: 'OS X' }; provider.pageYShiftFactor = 0; provider.statusBarHeight = 0; @@ -329,7 +349,7 @@ describe('GenericProvider', () => { }); it('should not update pageYShiftFactor for OS X if scrolled', async () => { - await provider.updatePageShiftFactor({ y: 0 }, 1); + await provider.updatePageShiftFactor({ y: 0 }, 1, scrollFactors); expect(provider.pageYShiftFactor).toBe(0); }); }); @@ -344,14 +364,55 @@ describe('GenericProvider', () => { }); it('should update pageYShiftFactor for OS X platforms accordingly if scrolled', async () => { - await provider.updatePageShiftFactor({ y: 0 }, 1); + await provider.updatePageShiftFactor({ y: 0 }, 1, scrollFactors); expect(provider.pageYShiftFactor).toBe(-10); }); }); }); }); - describe('When Other', () => { + describe('When OS X fullpage screenshot', () => { + beforeEach(async () => { + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'OS X' }, {}, 'client', 'environment', { fullPage: true }); + await provider.createDriver(); + scrollFactors = { value: [0, 10] }; + provider.currentTag = { osName: 'OS X' }; + provider.pageYShiftFactor = 0; + }); + + describe('When Safari browserVersion > 13', () => { + describe('when element is present in DOM', () => { + beforeEach(() => { + provider.currentTag.browserName = 'safari'; + provider.currentTag.browserVersion = 15; + provider.statusBarHeight = 0; + }); + + it('should update pageYShiftFactor for OS X to statusBarHeight', async () => { + await provider.updatePageShiftFactor({ y: 0 }, 1, scrollFactors); + expect(provider.pageYShiftFactor).toBe(0); + }); + }); + }); + + describe('When Safari browserVersion <= 13', () => { + describe('when element is present in DOM', () => { + beforeEach(() => { + provider.currentTag.browserName = 'safari'; + provider.currentTag.browserVersion = 13; + provider.statusBarHeight = 20; + }); + + it('should update pageYShiftFactor for OS X platforms accordingly if scrolled', async () => { + scrollFactors = { value: [0, 10] }; + await provider.updatePageShiftFactor({ y: 0 }, 1, scrollFactors); + expect(provider.pageYShiftFactor).toBe(10); + }); + }); + }); + }); + + describe('When Other singlepage', () => { beforeEach(async () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); await provider.createDriver(); @@ -360,14 +421,30 @@ describe('GenericProvider', () => { }); it('should not update pageYShiftFactor for non-iOS platforms', async () => { - spyOn(Driver.prototype, 'executeScript').and.returnValue({ value: [0, 0] }); - await provider.updatePageShiftFactor({ y: 0 }, 1); + scrollFactors = { value: [0, 0] }; + await provider.updatePageShiftFactor({ y: 0 }, 1, scrollFactors); expect(provider.pageYShiftFactor).toBe(0); }); it('should update pageYShiftFactor for non-iOS platforms accordingly if scrolled', async () => { - spyOn(Driver.prototype, 'executeScript').and.returnValue({ value: [0, 10] }); - await provider.updatePageShiftFactor({ y: 0 }, 1); + scrollFactors = { value: [0, 10] }; + await provider.updatePageShiftFactor({ y: 0 }, 1, scrollFactors); + expect(provider.pageYShiftFactor).toBe(-10); + }); + }); + + describe('When Other fullpage', () => { + beforeEach(async () => { + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); + await provider.createDriver(); + provider.currentTag = { osName: 'Android' }; + provider.pageYShiftFactor = 0; + provider.statusBarHeight = 0; + }); + + it('should update pageYShiftFactor for non-iOS platforms accordingly if scrolled', async () => { + scrollFactors = { value: [0, 10] }; + await provider.updatePageShiftFactor({ y: 0 }, 1, scrollFactors); expect(provider.pageYShiftFactor).toBe(-10); }); }); @@ -376,124 +453,257 @@ describe('GenericProvider', () => { describe('getRegionObject', () => { let provider; let mockLocation = { x: 10, y: 20, width: 100, height: 200 }; + let scrollFactors = { value: [0, 0] }; + describe('When singlepage screenshot', () => { + describe('When on Tile 0', () => { + beforeEach(async () => { + // mock metadata + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); + provider.currentTag = { osName: 'Windows' }; + await provider.createDriver(); + spyOn(DesktopMetaData.prototype, 'devicePixelRatio') + .and.returnValue(1); + spyOn(GenericProvider.prototype, 'getScrollDetails') + .and.returnValue(scrollFactors); + spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); + }); - describe('When on Tile 0', () => { - beforeEach(async () => { - // mock metadata - provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); - provider.currentTag = { osName: 'Windows' }; - await provider.createDriver(); - spyOn(DesktopMetaData.prototype, 'devicePixelRatio') - .and.returnValue(1); - spyOn(Driver.prototype, 'executeScript') - .and.returnValue({ value: [0, 0] }); - spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); + it('should return a JSON object with the correct selector and coordinates for tile 0', async () => { + await provider.createDriver(); + + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObject(selector, 'mockElementId'); + + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y, + bottom: mockLocation.y + mockLocation.height, + left: mockLocation.x, + right: mockLocation.x + mockLocation.width + }); + }); }); - it('should return a JSON object with the correct selector and coordinates for tile 0', async () => { - await provider.createDriver(); + describe('When on Tile 1', () => { + beforeEach(async () => { + // mock metadata + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); + provider.currentTag = { osName: 'iOS' }; + await provider.createDriver(); + spyOn(DesktopMetaData.prototype, 'devicePixelRatio') + .and.returnValue(1); + spyOn(GenericProvider.prototype, 'getScrollDetails') + .and.returnValue(scrollFactors); + spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); + provider.pageYShiftFactor = -10; + provider.initialScrollFactor = scrollFactors; + }); + + afterEach(() => { + provider.pageYShiftFactor = 0; + provider.currentTag = null; + }); + it('should return a JSON object with the correct selector and coordinates', async () => { + await provider.createDriver(); + + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObject(selector, 'mockElementId'); + + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y + provider.pageYShiftFactor, + bottom: mockLocation.y + mockLocation.height + provider.pageYShiftFactor, + left: mockLocation.x, + right: mockLocation.x + mockLocation.width + }); + }); + }); + }); + + describe('When fullpage screenshot', () => { + describe('When no scroll', () => { + beforeEach(async () => { + // mock metadata + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); + provider.currentTag = { osName: 'Windows' }; + await provider.createDriver(); + spyOn(DesktopMetaData.prototype, 'devicePixelRatio') + .and.returnValue(1); + spyOn(Driver.prototype, 'executeScript') + .and.returnValue({ value: [0, 0] }); + spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); + }); + + it('should return a JSON object with the correct selector and coordinates for tile 0', async () => { + await provider.createDriver(); + + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObject(selector, 'mockElementId'); + + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y, + bottom: mockLocation.y + mockLocation.height, + left: mockLocation.x, + right: mockLocation.x + mockLocation.width + }); + }); + }); + + describe('When there is a scroll', () => { + let scrollX = 10; + let scrollY = 20; + beforeEach(async () => { + // mock metadata + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); + provider.currentTag = { osName: 'iOS' }; + await provider.createDriver(); + spyOn(DesktopMetaData.prototype, 'devicePixelRatio') + .and.returnValue(1); + spyOn(GenericProvider.prototype, 'getScrollDetails') + .and.returnValue({ value: [scrollX, scrollY] }); + spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); + provider.pageYShiftFactor = -10; + }); - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObject(selector, 'mockElementId'); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y, - bottom: mockLocation.y + mockLocation.height, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width + afterEach(() => { + provider.pageYShiftFactor = 0; + provider.currentTag = null; + }); + it('should return a JSON object with the correct selector and coordinates', async () => { + await provider.createDriver(); + + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObject(selector, 'mockElementId'); + + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y + scrollY + provider.pageYShiftFactor, + bottom: mockLocation.y + mockLocation.height + scrollY + provider.pageYShiftFactor, + left: mockLocation.x + scrollX, + right: mockLocation.x + mockLocation.width + scrollX + }); }); }); }); + }); - describe('When on Tile 1', () => { + describe('getRegionObjectFromBoundingBox', () => { + let provider; + let mockLocation = { x: 10, y: 20, width: 100, height: 200 }; + + describe('When singlepage screenshot', () => { beforeEach(async () => { // mock metadata provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); - provider.currentTag = { osName: 'iOS' }; + provider.currentTag = { osName: 'Windows' }; await provider.createDriver(); spyOn(DesktopMetaData.prototype, 'devicePixelRatio') .and.returnValue(1); - spyOn(Driver.prototype, 'executeScript') - .and.returnValue({ value: [0, 0] }); - spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); - provider.pageYShiftFactor = -10; + provider.statusBarHeight = 0; }); - afterEach(() => { - provider.pageYShiftFactor = 0; - provider.currentTag = null; + describe('When not an iOS', () => { + it('should return a JSON object with the correct selector and coordinates', async () => { + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y, + bottom: mockLocation.y + mockLocation.height, + left: mockLocation.x, + right: mockLocation.x + mockLocation.width + }); + }); }); - it('should return a JSON object with the correct selector and coordinates', async () => { - await provider.createDriver(); - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObject(selector, 'mockElementId'); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y + provider.pageYShiftFactor, - bottom: mockLocation.y + mockLocation.height + provider.pageYShiftFactor, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width + describe('When iOS', () => { + beforeEach(() => { + provider.currentTag = { osName: 'iOS' }; + provider.statusBarHeight = 132; + }); + it('should return a JSON object with the correct selector and coordinates with added statusBarHeight', async () => { + await provider.createDriver(); + + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); + + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y + provider.statusBarHeight, + bottom: mockLocation.y + mockLocation.height + provider.statusBarHeight, + left: mockLocation.x, + right: mockLocation.x + mockLocation.width + }); }); }); }); - }); - describe('getRegionObjectFromBoundingBox', () => { - let provider; - let mockLocation = { x: 10, y: 20, width: 100, height: 200 }; - beforeEach(async () => { - // mock metadata - provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); - provider.currentTag = { osName: 'Windows' }; - await provider.createDriver(); - spyOn(DesktopMetaData.prototype, 'devicePixelRatio') - .and.returnValue(1); - provider.statusBarHeight = 0; - }); - - describe('When not an iOS', () => { - it('should return a JSON object with the correct selector and coordinates', async () => { - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y, - bottom: mockLocation.y + mockLocation.height, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width - }); + describe('When fullpage screenshot', () => { + beforeEach(async () => { + // mock metadata + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); + provider.currentTag = { osName: 'Windows' }; + await provider.createDriver(); + spyOn(GenericProvider.prototype, 'getInitialScrollFactor') + .and.returnValue({ value: [0, 0] }); + spyOn(DesktopMetaData.prototype, 'devicePixelRatio') + .and.returnValue(1); + provider.statusBarHeight = 0; }); - }); - describe('When iOS', () => { - beforeEach(() => { - provider.currentTag = { osName: 'iOS' }; - provider.statusBarHeight = 132; + describe('When not an iOS', () => { + it('should return a JSON object with the correct selector and coordinates', async () => { + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y, + bottom: mockLocation.y + mockLocation.height, + left: mockLocation.x, + right: mockLocation.x + mockLocation.width + }); + }); }); - it('should return a JSON object with the correct selector and coordinates with added statusBarHeight', async () => { - await provider.createDriver(); - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y + provider.statusBarHeight, - bottom: mockLocation.y + mockLocation.height + provider.statusBarHeight, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width + describe('When iOS', () => { + let scrollX = 10; + let scrollY = 20; + beforeEach(() => { + provider.currentTag = { osName: 'iOS' }; + provider.statusBarHeight = 132; + spyOn(GenericProvider.prototype, 'getInitialScrollFactor') + .and.returnValue({ value: [scrollX, scrollY] }); + }); + it('should return a JSON object with the correct selector and coordinates with added statusBarHeight', async () => { + await provider.createDriver(); + + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); + + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y + scrollY + provider.statusBarHeight, + bottom: mockLocation.y + mockLocation.height + scrollY + provider.statusBarHeight, + left: mockLocation.x + scrollX, + right: mockLocation.x + mockLocation.width + scrollX + }); }); }); }); @@ -576,24 +786,21 @@ describe('GenericProvider', () => { describe('when not IOS', () => { it('should not get the initial scroll position', async () => { await provider.getInitialPosition(); - expect(provider.initialScrollFactor).toEqual({ value: [0, 0] }); + expect(provider.initialScrollFactor).toEqual(null); }); }); describe('when IOS', () => { - let executeScriptSpy; beforeEach(() => { provider.currentTag = { osName: 'iOS' }; - executeScriptSpy = spyOn(Driver.prototype, 'executeScript'); }); afterEach(() => { provider.currentTag = null; }); it('should get the initial scroll position', async () => { - spyOn(Driver.prototype, 'executeScript').and.returnValue({ value: [0, 200] }); + spyOn(GenericProvider.prototype, 'getInitialScrollFactor').and.returnValue({ value: [0, 200] }); await provider.getInitialPosition(); - expect(executeScriptSpy).toHaveBeenCalledWith({ script: 'return [parseInt(window.scrollX), parseInt(window.scrollY)];', args: [] }); expect(provider.initialScrollFactor).toEqual({ value: [0, 200] }); }); }); @@ -608,7 +815,7 @@ describe('GenericProvider', () => { }); describe('when not IOS', () => { it('should not scroll to position', async () => { - await provider.scrollToInitialPosition(0, 50); + await provider.scrollToInitialPosition(); expect(spyOn(Driver.prototype, 'executeScript')).toHaveBeenCalledTimes(0); }); }); @@ -625,7 +832,7 @@ describe('GenericProvider', () => { provider.currentTag = null; }); it('should scroll to position', async () => { - await provider.scrollToInitialPosition(0, 50); + await provider.scrollToInitialPosition(); expect(executeScriptSpy).toHaveBeenCalledTimes(1); expect(executeScriptSpy).toHaveBeenCalledWith({ script: 'window.scrollTo(0, 50)', args: [] }); }); @@ -653,7 +860,6 @@ describe('GenericProvider', () => { expect(getInitialPositionSpy).toHaveBeenCalledTimes(1); expect(getRegionObjectSpy).toHaveBeenCalledTimes(3); expect(scrollToInitialPositionSpy).toHaveBeenCalledTimes(1); - expect(scrollToInitialPositionSpy).toHaveBeenCalledWith(provider.initialScrollFactor.value[0], provider.initialScrollFactor.value[1]); expect(elementsArray).toEqual([{}, {}, {}]); }); From b5ff6ca4338e59884b2269f272293a58d97f1b4a Mon Sep 17 00:00:00 2001 From: rishigupta1599 Date: Thu, 14 Dec 2023 15:07:51 +0530 Subject: [PATCH 3/6] Fixing coverage --- .../test/providers/genericProvider.test.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/webdriver-utils/test/providers/genericProvider.test.js b/packages/webdriver-utils/test/providers/genericProvider.test.js index 9befb1983..6cd3f9fb8 100644 --- a/packages/webdriver-utils/test/providers/genericProvider.test.js +++ b/packages/webdriver-utils/test/providers/genericProvider.test.js @@ -1017,4 +1017,42 @@ describe('GenericProvider', () => { expect(executeScriptSpy).toHaveBeenCalledWith({ script: jsScript, args: [] }); }); }); + + describe('getScrollDetails', () => { + let provider; + let executeScriptSpy; + + beforeEach(async () => { + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); + await provider.createDriver(); + executeScriptSpy = spyOn(Driver.prototype, 'executeScript'); + }); + + it('should return scroll params', async () => { + await provider.getScrollDetails(); + expect(executeScriptSpy).toHaveBeenCalledWith({ script: 'return [parseInt(window.scrollX), parseInt(window.scrollY)];', args: [] }); + }); + }); + + describe('getInitialScrollFactor', () => { + let provider; + let getScrollDetailsSpy; + + beforeEach(async () => { + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); + await provider.createDriver(); + getScrollDetailsSpy = spyOn(GenericProvider.prototype, 'getScrollDetails'); + provider.initialScrollFactor = { value: [1, 1] }; + }); + + it('do not get scroll details if already present', async () => { + provider.getInitialScrollFactor(); + expect(getScrollDetailsSpy).not.toHaveBeenCalled(); + }); + it('gets scroll details if not present', async () => { + provider.initialScrollFactor = null; + provider.getInitialScrollFactor(); + expect(getScrollDetailsSpy).toHaveBeenCalled(); + }); + }); }); From 203a47a4ae60d819c24e846b5aeb06bef6c984ec Mon Sep 17 00:00:00 2001 From: rishigupta1599 Date: Thu, 14 Dec 2023 17:36:23 +0530 Subject: [PATCH 4/6] Cleaning specs --- .../test/providers/genericProvider.test.js | 264 ++++++------------ 1 file changed, 78 insertions(+), 186 deletions(-) diff --git a/packages/webdriver-utils/test/providers/genericProvider.test.js b/packages/webdriver-utils/test/providers/genericProvider.test.js index 6cd3f9fb8..f51188e7e 100644 --- a/packages/webdriver-utils/test/providers/genericProvider.test.js +++ b/packages/webdriver-utils/test/providers/genericProvider.test.js @@ -454,106 +454,66 @@ describe('GenericProvider', () => { let provider; let mockLocation = { x: 10, y: 20, width: 100, height: 200 }; let scrollFactors = { value: [0, 0] }; - describe('When singlepage screenshot', () => { - describe('When on Tile 0', () => { - beforeEach(async () => { - // mock metadata - provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); - provider.currentTag = { osName: 'Windows' }; - await provider.createDriver(); - spyOn(DesktopMetaData.prototype, 'devicePixelRatio') - .and.returnValue(1); - spyOn(GenericProvider.prototype, 'getScrollDetails') - .and.returnValue(scrollFactors); - spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); - }); - it('should return a JSON object with the correct selector and coordinates for tile 0', async () => { - await provider.createDriver(); - - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObject(selector, 'mockElementId'); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y, - bottom: mockLocation.y + mockLocation.height, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width - }); + function expectRegionObject(scrollX, scrollY) { + it('should return a JSON object with the correct selector and coordinates for tile', async () => { + // Call function with mock data + const selector = 'mock-selector'; + const result = await provider.getRegionObject(selector, 'mockElementId'); + + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y + scrollY + provider.pageYShiftFactor, + bottom: mockLocation.y + mockLocation.height + scrollY + provider.pageYShiftFactor, + left: mockLocation.x + scrollX + provider.pageXShiftFactor, + right: mockLocation.x + mockLocation.width + scrollX + provider.pageXShiftFactor }); }); + } + describe('When singlepage screenshot', () => { + beforeEach(async () => { + // mock metadata + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); + provider.currentTag = { osName: 'Windows' }; + await provider.createDriver(); + spyOn(DesktopMetaData.prototype, 'devicePixelRatio') + .and.returnValue(1); + spyOn(GenericProvider.prototype, 'getScrollDetails') + .and.returnValue(scrollFactors); + spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); + }); + + describe('When on Tile 0', () => { + expectRegionObject(0, 0); + }); describe('When on Tile 1', () => { beforeEach(async () => { // mock metadata - provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); provider.currentTag = { osName: 'iOS' }; - await provider.createDriver(); - spyOn(DesktopMetaData.prototype, 'devicePixelRatio') - .and.returnValue(1); - spyOn(GenericProvider.prototype, 'getScrollDetails') - .and.returnValue(scrollFactors); - spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); provider.pageYShiftFactor = -10; provider.initialScrollFactor = scrollFactors; }); - - afterEach(() => { - provider.pageYShiftFactor = 0; - provider.currentTag = null; - }); - it('should return a JSON object with the correct selector and coordinates', async () => { - await provider.createDriver(); - - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObject(selector, 'mockElementId'); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y + provider.pageYShiftFactor, - bottom: mockLocation.y + mockLocation.height + provider.pageYShiftFactor, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width - }); - }); + expectRegionObject(0, 0); }); }); describe('When fullpage screenshot', () => { - describe('When no scroll', () => { - beforeEach(async () => { - // mock metadata - provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); - provider.currentTag = { osName: 'Windows' }; - await provider.createDriver(); - spyOn(DesktopMetaData.prototype, 'devicePixelRatio') - .and.returnValue(1); - spyOn(Driver.prototype, 'executeScript') - .and.returnValue({ value: [0, 0] }); - spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); - }); - - it('should return a JSON object with the correct selector and coordinates for tile 0', async () => { - await provider.createDriver(); - - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObject(selector, 'mockElementId'); + beforeEach(async () => { + // mock metadata + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); + provider.currentTag = { osName: 'Windows' }; + await provider.createDriver(); + spyOn(DesktopMetaData.prototype, 'devicePixelRatio') + .and.returnValue(1); + spyOn(Driver.prototype, 'executeScript') + .and.returnValue({ value: [0, 0] }); + spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); + }); - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y, - bottom: mockLocation.y + mockLocation.height, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width - }); - }); + describe('When no scroll', () => { + expectRegionObject(0, 0); }); describe('When there is a scroll', () => { @@ -561,37 +521,12 @@ describe('GenericProvider', () => { let scrollY = 20; beforeEach(async () => { // mock metadata - provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); provider.currentTag = { osName: 'iOS' }; - await provider.createDriver(); - spyOn(DesktopMetaData.prototype, 'devicePixelRatio') - .and.returnValue(1); spyOn(GenericProvider.prototype, 'getScrollDetails') .and.returnValue({ value: [scrollX, scrollY] }); - spyOn(Driver.prototype, 'rect').and.returnValue(Promise.resolve(mockLocation)); provider.pageYShiftFactor = -10; }); - - afterEach(() => { - provider.pageYShiftFactor = 0; - provider.currentTag = null; - }); - it('should return a JSON object with the correct selector and coordinates', async () => { - await provider.createDriver(); - - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObject(selector, 'mockElementId'); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y + scrollY + provider.pageYShiftFactor, - bottom: mockLocation.y + mockLocation.height + scrollY + provider.pageYShiftFactor, - left: mockLocation.x + scrollX, - right: mockLocation.x + mockLocation.width + scrollX - }); - }); + expectRegionObject(scrollX, scrollY); }); }); }); @@ -599,32 +534,35 @@ describe('GenericProvider', () => { describe('getRegionObjectFromBoundingBox', () => { let provider; let mockLocation = { x: 10, y: 20, width: 100, height: 200 }; - - describe('When singlepage screenshot', () => { - beforeEach(async () => { - // mock metadata - provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); - provider.currentTag = { osName: 'Windows' }; - await provider.createDriver(); - spyOn(DesktopMetaData.prototype, 'devicePixelRatio') - .and.returnValue(1); - provider.statusBarHeight = 0; + beforeEach(async () => { + // mock metadata + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); + provider.currentTag = { osName: 'Windows' }; + await provider.createDriver(); + spyOn(DesktopMetaData.prototype, 'devicePixelRatio') + .and.returnValue(1); + provider.statusBarHeight = 0; + }); + + function expectRegionObjectFromBoundingBox(scrollX, scrollY) { + // Call function with mock data + it('should return a JSON object with the correct selector and coordinates', async () => { + const selector = 'mock-selector'; + const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); + // Assert expected result + expect(result.selector).toEqual(selector); + expect(result.coOrdinates).toEqual({ + top: mockLocation.y + scrollY + provider.statusBarHeight, + bottom: mockLocation.y + mockLocation.height + scrollY + provider.statusBarHeight, + left: mockLocation.x + scrollX, + right: mockLocation.x + mockLocation.width + scrollX + }); }); + } + describe('When singlepage screenshot', () => { describe('When not an iOS', () => { - it('should return a JSON object with the correct selector and coordinates', async () => { - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y, - bottom: mockLocation.y + mockLocation.height, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width - }); - }); + expectRegionObjectFromBoundingBox(0, 0); }); describe('When iOS', () => { @@ -632,79 +570,33 @@ describe('GenericProvider', () => { provider.currentTag = { osName: 'iOS' }; provider.statusBarHeight = 132; }); - it('should return a JSON object with the correct selector and coordinates with added statusBarHeight', async () => { - await provider.createDriver(); - - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y + provider.statusBarHeight, - bottom: mockLocation.y + mockLocation.height + provider.statusBarHeight, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width - }); - }); + + expectRegionObjectFromBoundingBox(0, 0); }); }); describe('When fullpage screenshot', () => { + let scrollX = 10; + let scrollY = 20; beforeEach(async () => { // mock metadata provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); provider.currentTag = { osName: 'Windows' }; await provider.createDriver(); spyOn(GenericProvider.prototype, 'getInitialScrollFactor') - .and.returnValue({ value: [0, 0] }); - spyOn(DesktopMetaData.prototype, 'devicePixelRatio') - .and.returnValue(1); - provider.statusBarHeight = 0; + .and.returnValue({ value: [scrollX, scrollY] }); }); describe('When not an iOS', () => { - it('should return a JSON object with the correct selector and coordinates', async () => { - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y, - bottom: mockLocation.y + mockLocation.height, - left: mockLocation.x, - right: mockLocation.x + mockLocation.width - }); - }); + expectRegionObjectFromBoundingBox(scrollX, scrollY); }); describe('When iOS', () => { - let scrollX = 10; - let scrollY = 20; beforeEach(() => { provider.currentTag = { osName: 'iOS' }; provider.statusBarHeight = 132; - spyOn(GenericProvider.prototype, 'getInitialScrollFactor') - .and.returnValue({ value: [scrollX, scrollY] }); - }); - it('should return a JSON object with the correct selector and coordinates with added statusBarHeight', async () => { - await provider.createDriver(); - - // Call function with mock data - const selector = 'mock-selector'; - const result = await provider.getRegionObjectFromBoundingBox(selector, mockLocation); - - // Assert expected result - expect(result.selector).toEqual(selector); - expect(result.coOrdinates).toEqual({ - top: mockLocation.y + scrollY + provider.statusBarHeight, - bottom: mockLocation.y + mockLocation.height + scrollY + provider.statusBarHeight, - left: mockLocation.x + scrollX, - right: mockLocation.x + mockLocation.width + scrollX - }); }); + expectRegionObjectFromBoundingBox(scrollX, scrollY); }); }); }); From c5b8e7bde7675060a226c449f4d15751ababb31d Mon Sep 17 00:00:00 2001 From: rishigupta1599 Date: Tue, 19 Dec 2023 12:37:50 +0530 Subject: [PATCH 5/6] Resolving comments --- .../src/providers/genericProvider.js | 20 ++++++------- .../test/providers/genericProvider.test.js | 30 +++++++++---------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/webdriver-utils/src/providers/genericProvider.js b/packages/webdriver-utils/src/providers/genericProvider.js index 5d31fd1a9..1c30229ce 100644 --- a/packages/webdriver-utils/src/providers/genericProvider.js +++ b/packages/webdriver-utils/src/providers/genericProvider.js @@ -38,7 +38,7 @@ export default class GenericProvider { this.pageYShiftFactor = 0; this.currentTag = null; this.removeElementShiftFactor = 50000; - this.initialScrollFactor = null; + this.initialScrollLocation = null; } addDefaultOptions() { @@ -73,23 +73,23 @@ export default class GenericProvider { return await this.driver.executeScript({ script: 'return [parseInt(window.scrollX), parseInt(window.scrollY)];', args: [] }); } - async getInitialScrollFactor() { - if (this.initialScrollFactor) { - return this.initialScrollFactor; + async getInitialScrollPosition() { + if (this.initialScrollLocation) { + return this.initialScrollLocation; } - this.initialScrollFactor = await this.getScrollDetails(); - return this.initialScrollFactor; + this.initialScrollLocation = await this.getScrollDetails(); + return this.initialScrollLocation; } async getInitialPosition() { if (this.currentTag.osName === 'iOS') { - this.initialScrollFactor = await this.getInitialScrollFactor(); + this.initialScrollLocation = await this.getInitialScrollPosition(); } } async scrollToInitialPosition() { if (this.currentTag.osName === 'iOS') { - await this.driver.executeScript({ script: `window.scrollTo(${this.initialScrollFactor.value[0]}, ${this.initialScrollFactor.value[1]})`, args: [] }); + await this.driver.executeScript({ script: `window.scrollTo(${this.initialScrollLocation.value[0]}, ${this.initialScrollLocation.value[1]})`, args: [] }); } } @@ -259,7 +259,7 @@ export default class GenericProvider { const scaleFactor = await this.metaData.devicePixelRatio(); let scrollX = 0, scrollY = 0; if (this.options?.fullPage) { - const scrollParams = await this.getInitialScrollFactor(); + const scrollParams = await this.getInitialScrollPosition(); scrollX = scrollParams.value[0]; scrollY = scrollParams.value[1]; } @@ -307,7 +307,7 @@ export default class GenericProvider { } this.pageXShiftFactor = this.currentTag.osName === 'iOS' ? 0 : (-(scrollFactors.value[0] * scaleFactor)); if (this.currentTag.osName === 'iOS' && !this.options?.fullPage) { - if (scrollFactors.value[0] !== this.initialScrollFactor.value[0] || scrollFactors.value[1] !== this.initialScrollFactor.value[1]) { + if (scrollFactors.value[0] !== this.initialScrollLocation.value[0] || scrollFactors.value[1] !== this.initialScrollLocation.value[1]) { this.pageXShiftFactor = (-1 * this.removeElementShiftFactor); this.pageYShiftFactor = (-1 * this.removeElementShiftFactor); } else if (location.y === 0) { diff --git a/packages/webdriver-utils/test/providers/genericProvider.test.js b/packages/webdriver-utils/test/providers/genericProvider.test.js index f51188e7e..6a3370561 100644 --- a/packages/webdriver-utils/test/providers/genericProvider.test.js +++ b/packages/webdriver-utils/test/providers/genericProvider.test.js @@ -279,7 +279,7 @@ describe('GenericProvider', () => { describe('when element is visible in viewport', () => { beforeEach(() => { - provider.initialScrollFactor = { value: [0, 10] }; + provider.initialScrollLocation = { value: [0, 10] }; }); it('should update pageYShiftFactor for iOS when location.y is 0', async () => { await provider.updatePageShiftFactor({ y: 0 }, 2, scrollFactors); @@ -295,7 +295,7 @@ describe('GenericProvider', () => { describe('when element is not visible in viewport and iOS scrolls automatically', () => { beforeEach(() => { - provider.initialScrollFactor = { value: [0, 30] }; + provider.initialScrollLocation = { value: [0, 30] }; }); it('should update pageYShiftFactor to negative value even if location.y is 0', async () => { await provider.updatePageShiftFactor({ y: 0 }, 2, scrollFactors); @@ -343,7 +343,7 @@ describe('GenericProvider', () => { describe('When Safari browserVersion > 13', () => { describe('when element is visible in viewport', () => { beforeEach(() => { - provider.initialScrollFactor = { value: [0, 10] }; + provider.initialScrollLocation = { value: [0, 10] }; provider.currentTag.browserName = 'safari'; provider.currentTag.browserVersion = 15; }); @@ -358,7 +358,7 @@ describe('GenericProvider', () => { describe('When Safari browserVersion <= 13', () => { describe('when element is visible in viewport', () => { beforeEach(() => { - provider.initialScrollFactor = { value: [0, 10] }; + provider.initialScrollLocation = { value: [0, 10] }; provider.currentTag.browserName = 'safari'; provider.currentTag.browserVersion = 13; }); @@ -493,7 +493,7 @@ describe('GenericProvider', () => { // mock metadata provider.currentTag = { osName: 'iOS' }; provider.pageYShiftFactor = -10; - provider.initialScrollFactor = scrollFactors; + provider.initialScrollLocation = scrollFactors; }); expectRegionObject(0, 0); }); @@ -583,7 +583,7 @@ describe('GenericProvider', () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); provider.currentTag = { osName: 'Windows' }; await provider.createDriver(); - spyOn(GenericProvider.prototype, 'getInitialScrollFactor') + spyOn(GenericProvider.prototype, 'getInitialScrollPosition') .and.returnValue({ value: [scrollX, scrollY] }); }); @@ -678,7 +678,7 @@ describe('GenericProvider', () => { describe('when not IOS', () => { it('should not get the initial scroll position', async () => { await provider.getInitialPosition(); - expect(provider.initialScrollFactor).toEqual(null); + expect(provider.initialScrollLocation).toEqual(null); }); }); @@ -691,9 +691,9 @@ describe('GenericProvider', () => { provider.currentTag = null; }); it('should get the initial scroll position', async () => { - spyOn(GenericProvider.prototype, 'getInitialScrollFactor').and.returnValue({ value: [0, 200] }); + spyOn(GenericProvider.prototype, 'getInitialScrollPosition').and.returnValue({ value: [0, 200] }); await provider.getInitialPosition(); - expect(provider.initialScrollFactor).toEqual({ value: [0, 200] }); + expect(provider.initialScrollLocation).toEqual({ value: [0, 200] }); }); }); }); @@ -716,7 +716,7 @@ describe('GenericProvider', () => { let executeScriptSpy; beforeEach(() => { provider.currentTag = { osName: 'iOS' }; - provider.initialScrollFactor = { value: [0, 50] }; + provider.initialScrollLocation = { value: [0, 50] }; executeScriptSpy = spyOn(Driver.prototype, 'executeScript'); }); @@ -926,7 +926,7 @@ describe('GenericProvider', () => { }); }); - describe('getInitialScrollFactor', () => { + describe('getInitialScrollPosition', () => { let provider; let getScrollDetailsSpy; @@ -934,16 +934,16 @@ describe('GenericProvider', () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); await provider.createDriver(); getScrollDetailsSpy = spyOn(GenericProvider.prototype, 'getScrollDetails'); - provider.initialScrollFactor = { value: [1, 1] }; + provider.initialScrollLocation = { value: [1, 1] }; }); it('do not get scroll details if already present', async () => { - provider.getInitialScrollFactor(); + provider.getInitialScrollPosition(); expect(getScrollDetailsSpy).not.toHaveBeenCalled(); }); it('gets scroll details if not present', async () => { - provider.initialScrollFactor = null; - provider.getInitialScrollFactor(); + provider.initialScrollLocation = null; + provider.getInitialScrollPosition(); expect(getScrollDetailsSpy).toHaveBeenCalled(); }); }); From ed6324b4f168ddab1adba77cee7a88f9a9316a43 Mon Sep 17 00:00:00 2001 From: rishigupta1599 Date: Tue, 19 Dec 2023 19:52:46 +0530 Subject: [PATCH 6/6] Resolving comments --- .../src/providers/genericProvider.js | 40 +++---- .../test/providers/genericProvider.test.js | 112 +++++++++--------- 2 files changed, 75 insertions(+), 77 deletions(-) diff --git a/packages/webdriver-utils/src/providers/genericProvider.js b/packages/webdriver-utils/src/providers/genericProvider.js index 1c30229ce..5db5cad35 100644 --- a/packages/webdriver-utils/src/providers/genericProvider.js +++ b/packages/webdriver-utils/src/providers/genericProvider.js @@ -69,11 +69,15 @@ export default class GenericProvider { } } + isIOS() { + return this.currentTag?.osName === 'iOS'; + } + async getScrollDetails() { return await this.driver.executeScript({ script: 'return [parseInt(window.scrollX), parseInt(window.scrollY)];', args: [] }); } - async getInitialScrollPosition() { + async getInitialScrollLocation() { if (this.initialScrollLocation) { return this.initialScrollLocation; } @@ -81,16 +85,8 @@ export default class GenericProvider { return this.initialScrollLocation; } - async getInitialPosition() { - if (this.currentTag.osName === 'iOS') { - this.initialScrollLocation = await this.getInitialScrollPosition(); - } - } - - async scrollToInitialPosition() { - if (this.currentTag.osName === 'iOS') { - await this.driver.executeScript({ script: `window.scrollTo(${this.initialScrollLocation.value[0]}, ${this.initialScrollLocation.value[1]})`, args: [] }); - } + async scrollToPosition(x, y) { + await this.driver.executeScript({ script: `window.scrollTo(${x}, ${y})`, args: [] }); } async screenshot(name, { @@ -224,6 +220,9 @@ export default class GenericProvider { document.head.appendChild(e);`; await this.driver.executeScript({ script: jsScript, args: [] }); + if (this.options?.fullPage || this.isIOS()) { + await this.getInitialScrollLocation(); + } } async undoTransformations(data) { @@ -259,13 +258,12 @@ export default class GenericProvider { const scaleFactor = await this.metaData.devicePixelRatio(); let scrollX = 0, scrollY = 0; if (this.options?.fullPage) { - const scrollParams = await this.getInitialScrollPosition(); - scrollX = scrollParams.value[0]; - scrollY = scrollParams.value[1]; + scrollX = this.initialScrollLocation.value[0]; + scrollY = this.initialScrollLocation.value[1]; } let headerAdjustment = 0; - if (this.currentTag.osName === 'iOS') { + if (this.isIOS()) { headerAdjustment = this.statusBarHeight; } const coOrdinates = { @@ -300,13 +298,13 @@ export default class GenericProvider { } async updatePageShiftFactor(location, scaleFactor, scrollFactors) { - if (this.currentTag.osName === 'iOS' || (this.currentTag.osName === 'OS X' && parseInt(this.currentTag.browserVersion) > 13 && this.currentTag.browserName.toLowerCase() === 'safari')) { + if (this.isIOS() || (this.currentTag.osName === 'OS X' && parseInt(this.currentTag.browserVersion) > 13 && this.currentTag.browserName.toLowerCase() === 'safari')) { this.pageYShiftFactor = this.statusBarHeight; } else { this.pageYShiftFactor = this.statusBarHeight - (scrollFactors.value[1] * scaleFactor); } - this.pageXShiftFactor = this.currentTag.osName === 'iOS' ? 0 : (-(scrollFactors.value[0] * scaleFactor)); - if (this.currentTag.osName === 'iOS' && !this.options?.fullPage) { + this.pageXShiftFactor = this.isIOS() ? 0 : (-(scrollFactors.value[0] * scaleFactor)); + if (this.isIOS() && !this.options?.fullPage) { if (scrollFactors.value[0] !== this.initialScrollLocation.value[0] || scrollFactors.value[1] !== this.initialScrollLocation.value[1]) { this.pageXShiftFactor = (-1 * this.removeElementShiftFactor); this.pageYShiftFactor = (-1 * this.removeElementShiftFactor); @@ -350,7 +348,6 @@ export default class GenericProvider { async getSeleniumRegionsByElement(elements) { const regionsArray = []; - await this.getInitialPosition(); for (let index = 0; index < elements.length; index++) { try { const selector = `element: ${index}`; @@ -361,7 +358,10 @@ export default class GenericProvider { log.debug(e.toString()); } } - await this.scrollToInitialPosition(); + + if (this.isIOS()) { + await this.scrollToPosition(this.initialScrollLocation.value[0], this.initialScrollLocation.value[1]); + } return regionsArray; } diff --git a/packages/webdriver-utils/test/providers/genericProvider.test.js b/packages/webdriver-utils/test/providers/genericProvider.test.js index 6a3370561..7eed7b621 100644 --- a/packages/webdriver-utils/test/providers/genericProvider.test.js +++ b/packages/webdriver-utils/test/providers/genericProvider.test.js @@ -583,8 +583,7 @@ describe('GenericProvider', () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); provider.currentTag = { osName: 'Windows' }; await provider.createDriver(); - spyOn(GenericProvider.prototype, 'getInitialScrollPosition') - .and.returnValue({ value: [scrollX, scrollY] }); + provider.initialScrollLocation = { value: [scrollX, scrollY] }; }); describe('When not an iOS', () => { @@ -668,101 +667,79 @@ describe('GenericProvider', () => { }); }); - describe('getInitialPosition', () => { + describe('scrollToPosition', () => { let provider; + let executeScriptSpy; beforeEach(async () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); provider.currentTag = { osName: 'Windows' }; await provider.createDriver(); - }); - describe('when not IOS', () => { - it('should not get the initial scroll position', async () => { - await provider.getInitialPosition(); - expect(provider.initialScrollLocation).toEqual(null); - }); + executeScriptSpy = spyOn(Driver.prototype, 'executeScript'); }); - describe('when IOS', () => { - beforeEach(() => { - provider.currentTag = { osName: 'iOS' }; - }); - - afterEach(() => { - provider.currentTag = null; - }); - it('should get the initial scroll position', async () => { - spyOn(GenericProvider.prototype, 'getInitialScrollPosition').and.returnValue({ value: [0, 200] }); - await provider.getInitialPosition(); - expect(provider.initialScrollLocation).toEqual({ value: [0, 200] }); - }); + it('should scroll to correct position', async () => { + await provider.scrollToPosition(10, 20); + expect(executeScriptSpy).toHaveBeenCalledWith({ script: 'window.scrollTo(10, 20)', args: [] }); }); }); - describe('scrollToInitialPosition', () => { + describe('isIOS', () => { let provider; beforeEach(async () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); provider.currentTag = { osName: 'Windows' }; - await provider.createDriver(); - }); - describe('when not IOS', () => { - it('should not scroll to position', async () => { - await provider.scrollToInitialPosition(); - expect(spyOn(Driver.prototype, 'executeScript')).toHaveBeenCalledTimes(0); - }); }); - describe('when IOS', () => { - let executeScriptSpy; - beforeEach(() => { - provider.currentTag = { osName: 'iOS' }; - provider.initialScrollLocation = { value: [0, 50] }; - executeScriptSpy = spyOn(Driver.prototype, 'executeScript'); - }); + it('when not iOS returns false', async () => { + let result = provider.isIOS(); + expect(result).toEqual(false); + }); - afterEach(() => { - provider.currentTag = null; - }); - it('should scroll to position', async () => { - await provider.scrollToInitialPosition(); - expect(executeScriptSpy).toHaveBeenCalledTimes(1); - expect(executeScriptSpy).toHaveBeenCalledWith({ script: 'window.scrollTo(0, 50)', args: [] }); - }); + it('when iOS returns true', async () => { + provider.currentTag = { osName: 'iOS' }; + let result = provider.isIOS(); + expect(result).toEqual(true); }); }); describe('getSeleniumRegionsByElement', () => { let getRegionObjectSpy; - let getInitialPositionSpy; - let scrollToInitialPositionSpy; + let scrollToPositionSpy; let provider; + const elements = ['mockElement_1', 'mockElement_2', 'mockElement_3']; beforeEach(async () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); await provider.createDriver(); getRegionObjectSpy = spyOn(GenericProvider.prototype, 'getRegionObject').and.returnValue({}); - getInitialPositionSpy = spyOn(GenericProvider.prototype, 'getInitialPosition'); - scrollToInitialPositionSpy = spyOn(GenericProvider.prototype, 'scrollToInitialPosition'); + scrollToPositionSpy = spyOn(GenericProvider.prototype, 'scrollToPosition'); }); it('should add regions for each element', async () => { - const elements = ['mockElement_1', 'mockElement_2', 'mockElement_3']; - const elementsArray = await provider.getSeleniumRegionsByElement(elements); - expect(getInitialPositionSpy).toHaveBeenCalledTimes(1); expect(getRegionObjectSpy).toHaveBeenCalledTimes(3); - expect(scrollToInitialPositionSpy).toHaveBeenCalledTimes(1); expect(elementsArray).toEqual([{}, {}, {}]); }); it('should ignore when error', async () => { getRegionObjectSpy.and.rejectWith(new Error('Element not found')); - const elements = ['mockElement_1', 'mockElement_2', 'mockElement_3']; const elementsArray = await provider.getSeleniumRegionsByElement(elements); expect(elementsArray).toEqual([]); }); + + it('should not scroll back to initial position for non iOS', async () => { + await provider.getSeleniumRegionsByElement(elements); + expect(scrollToPositionSpy).not.toHaveBeenCalled(); + }); + + it('should scroll back to initial position for iOS', async () => { + provider.currentTag = { osName: 'iOS' }; + provider.initialScrollLocation = { value: [10, 20] }; + await provider.getSeleniumRegionsByElement(elements); + expect(scrollToPositionSpy).toHaveBeenCalledTimes(1); + }); }); describe('getSeleniumRegionsByLocation', () => { @@ -860,11 +837,13 @@ describe('GenericProvider', () => { describe('doTransformations', () => { let provider; let executeScriptSpy; + let getInitialScrollLocationSpy; beforeEach(async () => { provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); await provider.createDriver(); executeScriptSpy = spyOn(Driver.prototype, 'executeScript'); + getInitialScrollLocationSpy = spyOn(GenericProvider.prototype, 'getInitialScrollLocation'); }); it('should do transfomation', async () => { @@ -887,6 +866,25 @@ describe('GenericProvider', () => { await provider.doTransformations(); expect(executeScriptSpy).toHaveBeenCalledWith({ script: jsScript, args: [] }); }); + + it('should not get initial scroll position for singlepage for non ios', async () => { + await provider.doTransformations(); + expect(getInitialScrollLocationSpy).not.toHaveBeenCalled(); + }); + + it('should get initial scroll position for singlepage for ios', async () => { + provider.currentTag = { osName: 'iOS' }; + await provider.createDriver(); + await provider.doTransformations(); + expect(getInitialScrollLocationSpy).toHaveBeenCalled(); + }); + + it('should get initial scroll position for singlepage', async () => { + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}, 'client', 'environment', { fullPage: true }); + await provider.createDriver(); + await provider.doTransformations(); + expect(getInitialScrollLocationSpy).toHaveBeenCalled(); + }); }); describe('undoTransformations', () => { @@ -926,7 +924,7 @@ describe('GenericProvider', () => { }); }); - describe('getInitialScrollPosition', () => { + describe('getInitialScrollLocation', () => { let provider; let getScrollDetailsSpy; @@ -938,12 +936,12 @@ describe('GenericProvider', () => { }); it('do not get scroll details if already present', async () => { - provider.getInitialScrollPosition(); + provider.getInitialScrollLocation(); expect(getScrollDetailsSpy).not.toHaveBeenCalled(); }); it('gets scroll details if not present', async () => { provider.initialScrollLocation = null; - provider.getInitialScrollPosition(); + provider.getInitialScrollLocation(); expect(getScrollDetailsSpy).toHaveBeenCalled(); }); });