String context = driver.getContext();
if (context.startsWith("NATIVE")) {
driver.findElementByXPath(locator).click();
} else {
WebElement rootElement = driver.findElementByXPath("//*[1]");
int rootElementWidth = rootElement.getSize().width;
int rootElementHeight = rootElement.getSize().height;
WebElement element = driver.findElementByXPath("the xpath of you want to click");
Dimension elementSize = element.getSize();
Point elementPoint = element.getLocation();
int centerX = elementPoint.x + elementSize.width/2+1;
int centerY = elementPoint.y + elementSize.height/2+1;
driver.context("NATIVE_APP");
WebElement webviewElement = driver.findElementByXPath("//*[@class='android.webkit.WebView']");
Dimension webviewSize = webviewElement.getSize();
Point webviewPoint = webviewElement.getLocation();
int webviewWidth = webviewSize.width;
int webviewHeight = webviewSize.height;
int startX = webviewPoint.x;
int startY = webviewPoint.y;
logger.info("web size:" + rootElementWidth + "x"+ rootElementHeight);
logger.info("native webview size:" + webviewWidth + "x" + webviewHeight);
logger.info("element location:" + centerX + "x" + centerY);
logger.info("click:" + (startX + centerX*webviewWidth/rootElementWidth) + "x"
+ (startY + centerY*webviewHeight/rootElementHeight));
TouchAction t = new TouchAction(driver);
t.press(startX + centerX*webviewWidth/rootElementWidth, startY + centerY*webviewHeight/rootElementHeight).release().perform();
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
if (contextName.startsWith("WEB")){
driver.context(contextName);
}
}
}
本来是发求助贴的,结果写到一半灵光一闪,找到问题解决方案了,
所以发贴还是有大大用处的,至少可以帮助自己梳理一下问题
另外还踩坑了,尝试在 webview 界面点击超出 webview 屏幕之外的坐标位置时,
appium 报的错误居然是未实现,一直以为是我的点击坐标的方法不正确导致的,
害我尝试了好多次才领悟到可能是超过了范围导致的。
Caused by: org.openqa.selenium.WebDriverException: Method has not yet been implemented (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 17 milliseconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'CNHQ-16071101N', ip: '10.24.42.87', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_101'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{appPackage=com.suning.mobile.epa, appActivity=com.suning.mobile.epa.ui.init.SplashActivity, noReset=true, noSign=true, autoLaunch=true, newCommandTimeout=600, automationName=UIAutomator2, javascriptEnabled=true, platformName=Android, deviceName=android, platform=ANY}]
Session ID: a1d21255-31e8-42f6-8905-6debf666b706
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at io.appium.java_client.PerformsTouchActions.performTouchAction(PerformsTouchActions.java:39)
at io.appium.java_client.TouchAction.perform(TouchAction.java:381)
at cn.suning.automation.keyword.MobileElementKeyword.elementClick(MobileElementKeyword.java:229)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at cn.suning.automation.execute.KeywordExecuter.run(KeywordExecuter.java:94)
对比一年前的控件 + 偏移量的方法,还是有进步的。