Skip to content

Commit

Permalink
Making logic more functional for IOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit3200 committed Oct 25, 2023
1 parent 854ee79 commit bd070e8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/webdriver-utils/src/providers/genericProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions packages/webdriver-utils/test/providers/genericProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit bd070e8

Please sign in to comment.