先说说背景:我们的应用是混合应用,首页就是 h5 做的,需要通过首页的入口进入具体 h5 或者 native 功能。我刚开始写自动化测试,用的 macaca biz 的那一套框架,分层也是按照样例分的三层:页面元素层,页面操作层,整体功能流程层。
再说说现象:我写完案例跑下来发现首页的元素有时候找不到。
问题:想问问 driver.switchFromNativeToWebView();方法该怎么用?或者说该怎么做 native 切换 h5?
看过文章说是只需要在进入 H5 页面后,开始 H5 自动化之前执行如下方法:driver.switchFromNativeToWebView();
按照上面的说法,我在整体功能流程层加了这个方法,但是执行下来报错。
// 处理登录
LoginPage loginPage = new LoginPage("登录页");
driver.switchFromNativeToWebView();
loginPage.setDriver(driver);
if (loginPage.hasPageShown(LoginPageUI.LOGIN_BTN)) {
saveScreen(loginPage.pageDesc);
ResultGenerator.loadPageSucc(loginPage);
loginPage.login("test", "123");
} else {
ResultGenerator.loadPageFail(loginPage);

}

报错信息如下:
2017-09-14 10:42:57 Response:{"sessionId":"126c0c9a-d068-4728-8284-fbdf37421ff2","status":0}
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 38.591 sec <<< FAILURE!
test(com.javademo.cases.SampleTest) Time elapsed: 38.189 sec <<< ERROR!
java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.alibaba.fastjson.JSONArray
at macaca.client.commands.Context.getContexts(Context.java:27)
at macaca.client.MacacaClient.contexts(MacacaClient.java:143)
at macaca.java.biz.BaseMacacaClient.switchFromNativeToWebView(BaseMacacaClient.java:700)
at com.javademo.pages.HomePage.xxxxx(HomePage.java:16)
at com.javademo.cases.SampleTest.test(SampleTest.java:76)
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
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 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

我启动 app-inspector 去查找元素,发现可以找到 h5 元素。我又把 driver.switchFromNativeToWebView();给删除了,发现又好用了。。。不知道什么原因。。。android 平台和 ios 平台切换了一次或者重新启动 server 后,又找不到首页元素了。。。


↙↙↙阅读原文可查看相关链接,并与作者交流