Appium Appium + Java8 踩坑记 (切换至 Webview -- 1)

shane51 · 2017年07月22日 · 最后由 securitytest 回复于 2017年09月26日 · 1356 次阅读

测试 App 时,不免会遇到有 WebView 的混合式 App,通常 Appium 建议采用切换 Context 的方法来进入 Webview,之后就跟自动化 Web 是一样的方法。虽说方法一样,但是总是会有一些很坑的 App 实现方式,让人掉进深坑。后面我会以一个国际知名航空公司的移动应用为例(约 60% 的 Webview + 40% 的 Native),讲述自己遇到的一些坑。。。

由于被测 App 不止一个 Webview,直接 switch 就会出现错误,那么就想到切换到最后一个 Webview,那不就对了嘛。所以很开心的有了如下代码:

public void switchToLastWebView() {
        Set<String> contextNames = driver.getContextHandles();
        Set<String> webViewContextNames =  contextNames
                .stream()
                .filter(contextName -> contextName.contains("WEBVIEW_"))
                .collect(Collectors.toSet());

        if (webViewContextNames.size() > 0){
            currentContextView = (String) webViewContextNames
                .toArray()[webViewContextNames.size()-1];
            driver.context(currentContextView);
        }
        LOGGER.log(Level.INFO, "current context:" + driver.getContext());
    }

运行一下看看吧,过了! 太棒了!!!

来来来,在运行一下。晴天霹雳。。。挂了。。。
不行,我不信,再运行一次。。。还是挂了。。。
那怎么办啊?
Debug 啊! 设个断点继续来。。
第一遍: Pass。
第二遍: Pass。。
第三遍: Pass。。。
怎么又不失败了,坑爹啊。。。

不能在 Debug 状态发现问题,该怎么发现问题呢?
加 Log 啊!
在哪加?加什么呢?
好问题!
先分析一下,首先打出切换之后当前的 context 是什么。 恰好,代码里面已经加了。

...
    LOGGER.log(Level.INFO, "current context:" + driver.getContext());
...

其次,当前总共有哪些 context

...
    LOGGER.log(Level.INFO, "All contexts:" + contextNames);
...

再次,当前总共有哪些 Webview 的 context:

...
     LOGGER.log(Level.INFO, "All webview contexts:" + webViewContextNames);
...

加入 Log 之后代码如下:

public void switchToLastWebView() {
       Set<String> contextNames = driver.getContextHandles();
       Set<String> webViewContextNames =  contextNames
               .stream()
               .filter(contextName -> contextName.contains("WEBVIEW_"))
               .collect(Collectors.toSet());
       String currentContextView = "";

       if (webViewContextNames.size() > 0){
           currentContextView = (String) webViewContextNames
               .toArray()[webViewContextNames.size()-1];
           driver.context(currentContextView);
       }
       LOGGER.log(Level.INFO, "All contexts:" + contextNames);
       LOGGER.log(Level.INFO, "All webview contexts:" + webViewContextNames);
       LOGGER.log(Level.INFO, "current context:" + driver.getContext());
   }

通过 console 中的 log 信息,发现,运行时有时候 All contexts 中显示的 WebView 的顺序与 All webview contexts 中的 Webview 的顺序不同,导致了从 webViewContextNames 取 webview 的时候,最后一个 webview 并不是 All contexts 中最新的 webview。所以,最终的问题是 webViewContextNames 返回了错误的顺序,那么最有可能出错的就是 filter 方法了。经过 stackoverflow:
https://stackoverflow.com/questions/29216588/how-to-ensure-order-of-processing-in-java8-streams

发现,如果需要保持顺序,那么我们应该使用 List。接下来我们把 webViewContextNames 类型换成List<String> (注意Collectors.toSet()也需要换成Collectors.toList() )

...
    List<String> webViewContextNames =  contextNames
            .stream()
            .filter(contextName -> contextName.contains("WEBVIEW_"))
            .collect(Collectors.toList());
...

最终的代码会变成:

public void switchToLastWebView() {
     Set<String> contextNames = driver.getContextHandles();
     List<String> webViewContextNames =  contextNames
             .stream()
             .filter(contextName -> contextName.contains("WEBVIEW_"))
             .collect(Collectors.toList());
     String currentContextView = "";

     if (webViewContextNames.size() > 0){
         currentContextView = (String) webViewContextNames
             .toArray()[webViewContextNames.size()-1];
         driver.context(currentContextView);
     }
     LOGGER.log(Level.INFO, "All contexts:" + contextNames);
     LOGGER.log(Level.INFO, "All webview contexts:" + webViewContextNames);
     LOGGER.log(Level.INFO, "current context:" + driver.getContext());
 }

同理对于从 WebView 切换到 Native 也是类似的方法:

public void switchToNativeView() {
     Set<String> contextNames = driver.getContextHandles();
     List<String> nativeViewContextNames = contextNames
             .stream()
             .filter(contextName -> contextName.contains("NATIVE_"))
             .collect(Collectors.toList());
     String currentContextView = "";

     if (nativeViewContextNames.size() > 0) {
         currentContextView = (String) nativeViewContextNames
             .toArray()[nativeViewContextNames.size() - 1];
         driver.context(currentContextView);
     }
     LOGGER.log(Level.INFO, "All contexts:" + contextNames);
     LOGGER.log(Level.INFO, "All native contexts:" + nativeViewContextNames);
     LOGGER.log(Level.INFO, "current context:" + driver.getContext());
 }

至此切换至 Webview 的第一个坑就填完了,现在可以稳定的切到最前面的 webview。
但是万一两个 webview 不是同时加载,是由先后顺序的陆续出来的呢? 好吧,又掉坑里了。下期再看如何填这个坑。

共收到 1 条回复 时间 点赞
shane51 Appium + Java8 踩坑记 (切换至 Webview -- 2 ) 中提及了此贴 07月22日 09:40
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册