Created
October 18, 2017 12:05
-
-
Save henrrich/4aa63c725cd03b2421b94594b79ee10a to your computer and use it in GitHub Desktop.
appium code snippets for converting webelement coordinates inside webview to coordinates in native context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Point convertWebviewLocationToTouchCoordinates(WebElement webViewElement) { | |
//Find the center point of element on webview | |
Point webviewLocation = webViewElement.getLocation(); | |
Dimension size = webViewElement.getSize(); | |
int elementWebviewCenterX = webviewLocation.getX() + Math.round(size.getWidth() / 2); | |
int elementWebviewCenterY = webviewLocation.getY() + Math.round(size.getHeight() / 2); | |
System.out.println("Element WebView Location: ( " + elementWebviewCenterX + ", " + elementWebviewCenterY + ")"); | |
return convertWebviewLocationToTouchCoordinates(elementWebviewCenterX, elementWebviewCenterY); | |
} | |
public Point convertWebviewLocationToTouchCoordinates(int elementWebviewX, int elementWebviewY) { | |
// We need to compare the view size of web and native. | |
// First we gather [Information] of web view. | |
// [Information] - Element coordinates of element and web view height and width. | |
JavascriptExecutor js = (JavascriptExecutor) driver; | |
// Web View Dimensions | |
int screenWebViewWidth = ((Long) js.executeScript("return window.innerWidth || document.body.clientWidth")).intValue(); | |
int screenWebViewHeight = ((Long) js.executeScript("return window.innerHeight || document.body.clientHeight")).intValue(); | |
System.out.println("Webview (width, height) value calculated is: ( " + screenWebViewWidth + ", " + screenWebViewHeight + ")"); | |
driver.context("NATIVE_APP"); | |
double screenWidth = driver.manage().window().getSize().getWidth(); | |
double screenHeight = driver.manage().window().getSize().getHeight(); | |
// From the webview coordinates we will be calculating the native view coordinates using the width and height. | |
double elementNativeViewX = (elementWebviewX * screenWidth) / screenWebViewWidth; | |
double elementNativeViewY = (elementWebviewY * screenHeight) / screenWebViewHeight; | |
System.out.println("Native View Coordinates: ( " + elementNativeViewX + ", " + elementNativeViewY + ")"); | |
return new Point((int) elementNativeViewX, (int) (elementNativeViewY)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment