Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
fix hybrid element position determination (#1906)
Browse files Browse the repository at this point in the history
* fix: fix hybrid element position determination

when switched to the webview of a hybrid app the elements on the screenshot were not aligned with the expected position. This will fix that by determining the position of the first webview on in the hybrid app

* Fix review comment

* fix: fix after review comment
  • Loading branch information
wswebcreation authored Sep 19, 2021
1 parent fd6ecea commit 0ac98ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
25 changes: 21 additions & 4 deletions app/main/appium-method-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export default class AppiumMethodHandler {

async _getContextsSourceAndScreenshot () {
let contexts, contextsError, currentContext, currentContextError, platformName,
source, sourceError, screenshot, screenshotError, statBarHeight, windowSize, windowSizeError;
source, sourceError, screenshot, screenshotError, statBarHeight, windowSize, windowSizeError, webViewPosition;

if (!await this._hasContextsCommand()) {
currentContext = null;
Expand Down Expand Up @@ -232,6 +232,17 @@ export default class AppiumMethodHandler {
}

if (currentContext !== NATIVE_APP) {
const isAndroid = _.toLower(platformName) === 'android';
try {
// Get the webview offset
const el = await this.driver.elementOrNull(
isAndroid ? 'xpath' : '-ios class chain',
isAndroid ? '//android.webkit.WebView' : '**/XCUIElementTypeWebView'
);
if (el) {
webViewPosition = await this.driver.getLocation(el.value);
}
} catch (ign) {}
await this.driver.context(currentContext);
}
// End of note
Expand All @@ -242,9 +253,15 @@ export default class AppiumMethodHandler {
*/
try {
if (currentContext !== NATIVE_APP) {
const webviewStatusAddressBarHeight = await this.driver.execute(getWebviewStatusAddressBarHeight, [{platformName, statBarHeight}]);

await this.driver.execute(setHtmlElementAttributes, [{platformName, webviewStatusAddressBarHeight}]);
// Fallback if the webview position can't be determined,
// then do it based on the web context
if (!webViewPosition) {
webViewPosition = {
x: 0,
y: await this.driver.execute(getWebviewStatusAddressBarHeight, [{platformName, statBarHeight}]),
};
}
await this.driver.execute(setHtmlElementAttributes, [{platformName, webviewStatusAddressBarHeight: webViewPosition.y}]);
}
} catch (ign) {}
}
Expand Down
22 changes: 10 additions & 12 deletions app/renderer/actions/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,29 +802,27 @@ export function setVisibleProviders () {
* @param {object} caps
*/
function addCustomCaps (caps) {
const {browserName = '', platformName = ''} = caps;
const safariCustomCaps = {
// Add the includeSafariInWebviews for future HTML detection
includeSafariInWebviews: true,
};
const chromeCustomCaps = {
const {platformName = ''} = caps;
const androidCustomCaps = {
// @TODO: remove when this is defaulted in the newest Appium 1.8.x release
ensureWebviewsHavePages: true,
// Make sure the screenshot is taken of the whole screen when the ChromeDriver is used
// for or Chrome, or for Hybrid apps
nativeWebScreenshot: true,
// Set the ChromeDriver to w3c:false because all internal calls are still JSONWP calls
chromeOptions: {
'w3c': false,
},
};
const androidCustomCaps = {
// @TODO: remove when this is defaulted in the newest Appium 1.8.x release
ensureWebviewsHavePages: true,
const iosCustomCaps = {
// Always add the includeSafariInWebviews for future HTML detection
// This will ensure that if you use AD to switch between App and browser
// that it can detect Safari as a webview
includeSafariInWebviews: true,
};
const iosCustomCaps = {};

return {
...caps,
...(browserName.toLowerCase() === 'safari' ? safariCustomCaps : {}),
...(browserName.toLowerCase() === 'chrome' ? chromeCustomCaps : {}),
...(platformName.toLowerCase() === 'android' ? androidCustomCaps : {}),
...(platformName.toLowerCase() === 'ios' ? iosCustomCaps : {}),
};
Expand Down

0 comments on commit 0ac98ec

Please sign in to comment.