From bd070e832ea5c3af47e44dc62758e84cd36b2e22 Mon Sep 17 00:00:00 2001 From: amit3200 Date: Wed, 25 Oct 2023 12:44:55 +0530 Subject: [PATCH] Making logic more functional for IOS --- .../src/providers/genericProvider.js | 17 +++++++++ .../test/providers/genericProvider.test.js | 38 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/packages/webdriver-utils/src/providers/genericProvider.js b/packages/webdriver-utils/src/providers/genericProvider.js index f79748740..6059eae4a 100644 --- a/packages/webdriver-utils/src/providers/genericProvider.js +++ b/packages/webdriver-utils/src/providers/genericProvider.js @@ -38,6 +38,9 @@ export default class GenericProvider { this.footer = 0; this.pageYShiftFactor = 0; this.pageXShiftFactor = 0; + this.scrollXFactor = 0; + this.scrollYFactor = 0; + this.currentOs = null; } addDefaultOptions() { @@ -92,6 +95,9 @@ export default class GenericProvider { log.debug(`[${name}] : Tiles ${JSON.stringify(tiles)}`); const scrollFactors = await this.driver.executeScript({ script: 'return [parseInt(window.scrollX * window.devicePixelRatio), parseInt(window.scrollY * window.devicePixelRatio)];', args: [] }); + this.scrollXFactor = scrollFactors.value[0]; + this.scrollYFactor = scrollFactors.value[1]; + this.currentOs = tag.osName; this.pageYShiftFactor = tag.osName === 'iOS' ? tiles.tiles[0].statusBarHeight : (tiles.tiles[0].statusBarHeight - scrollFactors.value[1]); this.pageXShiftFactor = tag.osName === 'iOS' ? 0 : (-scrollFactors.value[0]); @@ -197,11 +203,22 @@ export default class GenericProvider { ]; } + updateYFactor(location) { + if (this.currentOs === 'iOS') { + if (location.y === 0) { + this.pageYShiftFactor += (-this.scrollYFactor); + } + } + } + async getRegionObject(selector, elementId) { const scaleFactor = await this.metaData.devicePixelRatio(); const rect = await this.driver.rect(elementId); const location = { x: rect.x, y: rect.y }; const size = { height: rect.height, width: rect.width }; + // Update YFactor Element is not visible in viewport + // In case of iOS if the element is not visible in viewport it gives 0,0 as coordinate. + this.updateYFactor(location); const coOrdinates = { top: Math.floor(location.y * scaleFactor) + this.pageYShiftFactor, bottom: Math.ceil((location.y + size.height) * scaleFactor) + this.pageYShiftFactor, diff --git a/packages/webdriver-utils/test/providers/genericProvider.test.js b/packages/webdriver-utils/test/providers/genericProvider.test.js index 554b65652..c757aba95 100644 --- a/packages/webdriver-utils/test/providers/genericProvider.test.js +++ b/packages/webdriver-utils/test/providers/genericProvider.test.js @@ -273,6 +273,44 @@ describe('GenericProvider', () => { }); }); + describe('updateYFactor', () => { + let provider; + + describe('When iOS', () => { + beforeEach(() => { + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); + provider.currentOs = 'iOS'; + provider.scrollYFactor = 10; + provider.pageYShiftFactor = 0; + }); + + it('should update pageYShiftFactor for iOS when location.y is 0', () => { + provider.updateYFactor({ y: 0 }); + expect(provider.pageYShiftFactor).toBe(-provider.scrollYFactor); + }); + + it('should not update pageYShiftFactor for iOS when location.y is not 0', () => { + // Location.y is not 0 + provider.updateYFactor({ y: 5 }); + expect(provider.pageYShiftFactor).toBe(0); + }); + }); + + describe('When Other', () => { + beforeEach(() => { + provider = new GenericProvider('123', 'http:executorUrl', { platform: 'win' }, {}); + provider.currentOs = 'Android'; + provider.scrollYFactor = 10; + provider.pageYShiftFactor = 0; + }); + + it('should not update pageYShiftFactor for non-iOS platforms', () => { + provider.updateYFactor({ y: 0 }); + expect(provider.pageYShiftFactor).toBe(0); + }); + }); + }); + describe('getRegionObject', () => { let provider; let mockLocation = { x: 10, y: 20, width: 100, height: 200 };