问题描述:
在用appium做客户端自动化的时候,由于微信是使用的x5内核,会导致我们跳转到微信的webview获取不到资源,
也就无法做自动化操作,以下是我搜集到的解决方法:
1.在手机端打开x5调试:
用微信打开debugx5.qq.com, 这是个微信的x5内核调试页面. 你可以在任何聊天窗口内输入这个网址. 并打开它.
勾选"是否打开TBS内核Inspector调试功能"
下载TBS后检测设备,启动调试即可
2.配置:
在编写用例的时候要设置ChromeOptions参数
设置ChromeDriver参数
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("androidProcess", "com.tencent.mm:tools");
capability.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
3. 使用:
由于x5内核升级导致新版的chromedriver不能创建session,会报:session not created exception: please close '' and try again
网上解决方法是降低chromedriver的版本降到2.20以下,但是会导致不稳定现象。chromedriver.exe的位置在node运行时加载路径中找(打印的log地址)
以下是使用方法:
x5内核切换context使用方法
public void test() throws InterruptedException {
AppiumServiceBuilder builder =
new AppiumServiceBuilder().withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withIPAddress("127.0.0.1")
.usingPort(8000);
AppiumDriverLocalService service = AppiumDriverLocalService.buildService(builder);
service.start();
if (service == null || !service.isRunning()){
throw new AppiumServerHasNotBeenStartedLocallyException("An appium server node is not started!");
}
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("appPackage", "com.tencent.mm");
capability.setCapability("appActivity", ".ui.LauncherUI");
capability.setCapability("deviceName", "device");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("androidProcess", "com.tencent.mm:tools");
capability.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
capability.setCapability("noReset", true);
capability.setCapability("noSign", true);
AndroidDriver driver = new AndroidDriver(service.getUrl(), capability);
Thread.sleep(8000);
driver.findElementByXPath("//*[@text='我']").click();
Thread.sleep(2000);
driver.findElementByXPath("//*[@text='收藏']").click();
Thread.sleep(2000);
driver.findElementById("com.tencent.mm:id/ag").click();
Thread.sleep(5000);
Set<String> handles = driver.getContextHandles();
for (String handle : handles){
System.out.println("handle :" + handle);
}
driver.context("WEBVIEW_com.tencent.mm:tools");
Thread.sleep(5000);
System.out.println("getPageSource :" + driver.getPageSource());
driver.findElementById("post-user").click();
driver.context("NATIVE_APP");
}
4.主意事项:
要保证WEBVIEW_com.tencent.mm:tools 进程是开启的(开启调试即可)
QQ图片20180129192831.jpg
网友评论