Appium Appium,在转换为 WEBVIEW 时卡住的问题

爻施 · 2016年06月24日 · 最后由 Mr Yang 回复于 2018年12月22日 · 2699 次阅读

因为我的代码中有频繁的切换 Native 和 WebView 的操作,一般在切换 Native 的时候,没有任何问题,但是每次都会在切换成 Webview 时卡住,以下是日志:

info: <-- POST /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/element 200 36.275 ms - 88 {"status":0,"value":{"ELEMENT":"28"},"sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
info: --> POST /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/element/28/click {"id":"28"}
info: [debug] Pushing command to appium work queue: ["element:click",{"elementId":"28"}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"28"}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: click
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":true,"status":0}
info: [debug] Responding to client with success: {"status":0,"value":true,"sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
info: <-- POST /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/element/28/click 200 3132.665 ms - 76 {"status":0,"value":true,"sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
info: --> GET /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/appium/device/current_activity {}
info: [debug] Getting focused package and activity
info: [debug] executing cmd: D:\dev_soft\android-sdks\platform-tools\adb.exe -s 4f62d22f shell "dumpsys window windows"
info: [debug] Responding to client with success: {"status":0,"value":".activity.MainActivity","sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
info: <-- GET /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/appium/device/current_activity 200 132.476 ms - 96 {"status":0,"value":".activity.MainActivity","sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
info: --> GET /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/context {}
info: [debug] Responding to client with success: {"status":0,"value":"NATIVE_APP","sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
info: <-- GET /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/context 200 1.207 ms - 84 {"status":0,"value":"NATIVE_APP","sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
info: --> GET /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/context {}
info: [debug] Responding to client with success: {"status":0,"value":"NATIVE_APP","sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
info: <-- GET /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/context 200 1.770 ms - 84 {"status":0,"value":"NATIVE_APP","sessionId":"7c4d5563-abdc-422b-bd79-1a39109a01b5"}
[hupan] 2016-06-23 17:46:06,759 - com.at.appium.base.AppBase INFO   - Current content is NATIVE_APP, Now switch to WEBVIEW_com.hupan.app
info: --> POST /wd/hub/session/7c4d5563-abdc-422b-bd79-1a39109a01b5/context {"name":"WEBVIEW_com.hupan.app"}
info: [debug] Getting a list of available webviews
info: [debug] executing cmd: D:\dev_soft\android-sdks\platform-tools\adb.exe -s 4f62d22f shell "cat /proc/net/unix"
info: [debug] WEBVIEW_9375 mapped to pid 9375
info: [debug] Getting process name for webview
info: [debug] executing cmd: D:\dev_soft\android-sdks\platform-tools\adb.exe -s 4f62d22f shell "ps"
info: [debug] WEBVIEW_6631 mapped to pid 6631
info: [debug] Getting process name for webview
info: [debug] executing cmd: D:\dev_soft\android-sdks\platform-tools\adb.exe -s 4f62d22f shell "ps"
info: [debug] WEBVIEW_9309 mapped to pid 9309
info: [debug] Getting process name for webview
info: [debug] executing cmd: D:\dev_soft\android-sdks\platform-tools\adb.exe -s 4f62d22f shell "ps"
info: [debug] Parsed pid: 9375 pkg: com.hupan.app:TcmsService
info: [debug] from: u0_a143,9375,219,558860,44732,ffffffff,400b58b8,S,com.hupan.app:TcmsService
info: [debug] returning process name: com.hupan.app:TcmsService
info: [debug] Parsed pid: 6631 pkg: com.tencent.mobileqq
info: [debug] from: u0_a96,6631,219,643188,102544,ffffffff,400b58b8,S,com.tencent.mobileqq
info: [debug] returning process name: com.tencent.mobileqq
info: [debug] Parsed pid: 9309 pkg: com.hupan.app
info: [debug] from: u0_a143,9309,219,715504,90316,ffffffff,6245c3fa,R,com.hupan.app
info: [debug] returning process name: com.hupan.app
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.hupan.app:TcmsService,WEBVIEW_com.tencent.mobileqq,WEBVIEW_com.hupan.app
info: [debug] ["WEBVIEW_com.hupan.app:TcmsService","WEBVIEW_com.tencent.mobileqq","WEBVIEW_com.hupan.app"]
info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.hupan.app:TcmsService,WEBVIEW_com.tencent.mobileqq,WEBVIEW_com.hupan.app
info: [debug] Connecting to chrome-backed webview
info: [debug] Found existing Chromedriver for context 'WEBVIEW_com.hupan.app'. Using it.
info: JSONWP Proxy: Proxying [GET /url] to [GET http://127.0.0.1:9515/wd/hub/session/c46338b3915a427b9c9167e64f9c44eb/url] with no body
info: [debug] Didn't get a new command in 300 secs, shutting down...
info: Shutting down appium session

每次会开在 info: JSONWP Proxy: Proxying [GET /url] to [GET http://127.0.0.1:9515/wd/hub/session/c46338b3915a427b9c9167e64f9c44eb/url] with no body 这一行,然后一直到超时退出,有没有大神遇到过这种问题:

public void switchContent(AndroidSrc androidSrc){
        if(androidSrc.getWebView().equals(WebViewType.NATIVE)&&driver.getContext().equals(AndroidWebView.HUPAN_WEBVIEW)){
            log.info("Current content is "+driver.getContext()+" , Now switch to "+AndroidWebView.NATIVE_WEBVIEW  );
            driver.context(AndroidWebView.NATIVE_WEBVIEW);
        }else if (androidSrc.getWebView().equals(WebViewType.WEBVIEW)&&driver.getContext().equals(AndroidWebView.NATIVE_WEBVIEW)){
            log.info("Current content is "+driver.getContext()+", Now switch to "+AndroidWebView.HUPAN_WEBVIEW);
            driver.context(AndroidWebView.HUPAN_WEBVIEW);
        }
    }

这是我切换 context 的代码

共收到 3 条回复 时间 点赞

楼主啊 为什么我现在切换还是不行啊 我附上我的代码 帮我看一下,切换的时候就卡住了 然后一直到超时,一直卡住切换那里

DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("app","");
            capabilities.setCapability("appPackage","com.tencent.mm");
            capabilities.setCapability("appActivity", ".ui.LauncherUI");
            capabilities.setCapability("deviceName","S889PBJR99999999");
            capabilities.setCapability("fastReset","false");
            capabilities.setCapability("fullRest","false");
            capabilities.setCapability("noRest","true");
            capabilities.setCapability("automationName","Appium");
            capabilities.setCapability("platformName","Android");
            capabilities.setCapability("platformVersion","5.1");
            capabilities.setCapability("recreateChromeDriverSessions", "True");

            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("androidProcess   ","com.tencent.mm:tools");
            capabilities.setCapability(ChromeOptions.CAPABILITY,options);

            AndroidDriver driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);

            //等待元素加载出来
            Thread.sleep(5000);
            driver.findElement(By.xpath("//*[@text='发现']")).click();
            int width = driver.manage().window().getSize().width;
            int height = driver.manage().window().getSize().height;
            Thread.sleep(2000);
            driver.swipe(width /2, height / 4 + 200,width / 2,height / 4,200);
            Thread.sleep(2000);
            driver.findElement(By.xpath("//*[@text='小程序']")).click();
            Thread.sleep(2000);
            driver.findElement(By.xpath("//*[@text='京东秒杀']")).click();
            System.out.println("当前context:" + driver.getContext());
            System.out.println("所有句柄" + driver.getContextHandles());
            System.out.println(driver.getPageSource());

            Thread.sleep(6000);
            //driver.findElement(By.name("去秒杀")).click();
            //Thread.sleep(6000);

            for(String context : (Set<String>)driver.getContextHandles())
            {
                System.out.println(context);
                if(context.contains("WEBVIEW_com.tencent.mm:tools"))
                {
                    driver.context("WEBVIEW_com.tencent.mm:tools");
                    System.out.println("已经进入webview");
                    System.out.println("当前context" + driver.getContext());
                    System.out.println(driver.getContext());
                }
            }
            driver.closeApp();
            driver.quit();

楼主解决问题了没? 我遇到和你一样的问题

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册