Skip to content

Commit

Permalink
Dont use browser object directly
Browse files Browse the repository at this point in the history
  • Loading branch information
pankaj443 committed Apr 23, 2024
1 parent deeb5c9 commit 0b92478
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
10 changes: 8 additions & 2 deletions percy/driver/driverWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppiumDriver {
// in real world when you get wd driver passed so need to ignore coverage on it

// In typescript for wdio we get driver.constuctor.name as 'BoundBrowser'
if (driver.constructor.name.includes('Browser') && !Undefined(driver.getSession)) {
if (driver.constructor.name.includes('Browser') && !Undefined(driver.capabilities)) {
this.type = 'wdio';
} else if ((driver.constructor.name === '' ||
driver.constructor.name === 'Object') && // Object check is only added for tests
Expand All @@ -30,7 +30,7 @@ class AppiumDriver {
return await TimeIt.run('getCapabilities', async () => {
if (this.wd) return await this.driver.sessionCapabilities();
/* istanbul ignore next */ // not sure why its marking it when its covered
if (this.wdio) return await this.driver.getSession();
if (this.wdio) return await this.driver.capabilities;
});
});
}
Expand Down Expand Up @@ -108,6 +108,12 @@ class AppiumDriver {
});
}

async getWindowSize() {
return await TimeIt.run('getWindowSize', async () => {
return await this.driver.getWindowSize();
});
}

get commandExecutorUrl() {
if (this.wd) return `${this.driver.configUrl.protocol}//${this.remoteHostname}${this.driver.configUrl.path}`;
/* istanbul ignore next */
Expand Down
31 changes: 13 additions & 18 deletions percy/metadata/iosMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ class IosMetadata extends Metadata {
const data = await this.staticData();
return data.statusBarHeight * data.pixelRatio;
}
/* istanbul ignore next */
if (typeof browser !== 'undefined') {
const viewportRect = await this.viewportRect();
return viewportRect.top;

if (this.driver.wdio) {
return (await this.viewportRect()).top;
} else {
const caps = await this.caps();
// In Ios the height of statusBarHeight in caps needs to be multiplied by pixel ratio
return (caps.statBarHeight || 1) * (caps.pixelRatio || 1);
}
}
Expand All @@ -35,18 +33,18 @@ class IosMetadata extends Metadata {
const data = await this.staticData();
return { width: data.screenWidth, height: data.screenHeight };
}
/* istanbul ignore next */
if (typeof browser !== 'undefined') {

var height, width;

Check warning on line 37 in percy/metadata/iosMetadata.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected var, use let or const instead
if (this.driver.wdio) {
const viewportRect = await this.viewportRect();
const height = viewportRect.top + viewportRect.height;
const width = viewportRect.width;
return { width, height };
height = viewportRect.top + viewportRect.height;
width = viewportRect.width;
} else {
const caps = await this.caps();
const height = caps.statBarHeight * caps.pixelRatio + caps.viewportRect?.height;
const width = caps.viewportRect?.width;
return { width, height };
height = caps.statBarHeight * caps.pixelRatio + caps.viewportRect?.height;
width = caps.viewportRect?.width;
}
return { width, height };
}

async viewportRect() {
Expand All @@ -71,13 +69,10 @@ class IosMetadata extends Metadata {
}

async scaleFactor() {
/* istanbul ignore next */
if (typeof browser !== 'undefined') {
if (this.driver.wdio) {
const viewportRect = await this.viewportRect();
const actualWidth = viewportRect.width;
/* eslint-disable */
const windowSize = await browser.getWindowSize();
/* eslint-enable */
const windowSize = await this.driver.getWindowSize();
const width = windowSize.width;
return actualWidth / width;
} else {
Expand Down
9 changes: 1 addition & 8 deletions percy/metadata/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ class Metadata {
// items that need caps are moved to getters as caps are not stored on wd driver object
// So we need to make a lazy call to avoid making session get call in app automate context
async caps() {
/* istanbul ignore next */
if (typeof browser !== 'undefined') {
/* eslint-disable */
return await browser.capabilities;
/* eslint-enable */
} else {
return await this.driver.getCapabilities();
}
return await this.driver.getCapabilities();
}

async osName() {
Expand Down
6 changes: 4 additions & 2 deletions test/mocks/appium/wdio_driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Browser {
appAutomate = appAutomate || false;
const ios = platform === 'iOS' || false;
const android = !ios;
deviceName = deviceName || android ? 'GenericAndroid' : 'iPhone 8 Plus'; // some device from static config
deviceName = deviceName || android ? 'GenericAndroid' : 'iPhone 14'; // some device from static config
enabled = enabled === undefined ? true : enabled;
ignoreErrors = ignoreErrors === undefined ? false : ignoreErrors;

Expand Down Expand Up @@ -45,7 +45,7 @@ class Browser {
};

this.capabilities = sessionCaps;

this.getWindowSize = jasmine.createSpy().and.resolveTo({ width: 390, height: 844, x: 0, y: 0 });
this.getSession = jasmine.createSpy().and.returnValue(sessionCaps);
this.takeScreenshot = jasmine.createSpy().and.resolveTo('some screenshot data');
this.getSystemBars = jasmine.createSpy().and.resolveTo({
Expand Down Expand Up @@ -77,6 +77,8 @@ class Browser {
])
});
}
} else if (str.includes('viewportRect')) {
return { width: 100, height: 200 };
}
});
this.getOrientation = jasmine.createSpy().and.returnValue('PORTRAIT');
Expand Down

0 comments on commit 0b92478

Please sign in to comment.