Appium 个人分享-混合框架内【WebView 的滑动问题】

中年_Brain · 2014年08月13日 · 最后由 草珊瑚 回复于 2015年12月09日 · 2410 次阅读

appium 滑动在 4.4 的系统内是很好用的,那么 4.1~4.3 系统怎么办?

论坛的大神隐藏的太深,没有一个人出来帮忙,小弟不才,研究出来一个解决方案,希望对后面的同学有些启发。

其他手势的操作同理,大家自己去写。

    // 支持4.1~4.4
    // 支持WEBVIEW滑动,对于原生滑动,我另外封装了一个
    public void webview_swipe(AppiumDriver driver, Double sX, Double sY,
            Double eX, Double eY, Double duration) throws Exception { //
        JavascriptExecutor js;
        HashMap<String, Double> swipeObject;
        try {
            // 跳过引导页
            logger.info("----------开始滑动----------");

            int X;
            int Y;
            int sX2;
            int sY2;
            int eX2;
            int eY2;

            // 支持 4.1~4.3 的模式 ,调用 ADB直接滑动
            if (autoName.equals("Selendroid")) {
                X = driver.manage().window().getSize().getWidth();
                Y = driver.manage().window().getSize().getHeight();

                sX2 = (int) (X * sX);
                sY2 = (int) (Y * sY);
                eX2 = (int) (X * eX);
                eY2 = (int) (Y * eY);

                // logger.info("滑动11111111");
                Runtime.getRuntime().exec(
                        "adb -s " + devName + " shell input swipe " + sX2 + " "
                                + sY2 + " " + eX2 + " " + eY2);
                TimeUnit.SECONDS.sleep(1);
            } else {
                // 滑动
                js = (JavascriptExecutor) driver;
                swipeObject = new HashMap<String, Double>();
                swipeObject.put("startX", sX);
                swipeObject.put("startY", sY);
                swipeObject.put("endX", eX);
                swipeObject.put("endY", eY);
                swipeObject.put("duration", duration);
                js.executeScript("mobile: swipe", swipeObject);
                TimeUnit.SECONDS.sleep(1);
            }
            logger.info("----------开始滑动  ok----------");
        } catch (Exception ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            throw ex;
        } finally {
            // 释放变量
        }

    }


===========================================================================================
    // 为了支持4.1~4.4
    // 重写 滑动方法
    // 支持原生界面滑动
    public void tap_swipe(AppiumDriver driver, int sX, int sY, int eX, int eY,
            int duration) throws Exception { //
        try {
            logger.info("----------开始滑动----------");
            // 支持 4.1~4.3 的模式 ,调用 ADB直接滑动
            if (autoName.equals("Selendroid")) {
                Runtime.getRuntime().exec(
                        "adb -s " + devName + " shell input swipe " + sX + " "
                                + sY + " " + eX + " " + eY);
            } else {
                driver.swipe(sX, sY, eX, eY, duration * 1000);
            }
            TimeUnit.SECONDS.sleep(duration);

            logger.info("----------开始滑动  ok----------");
        } catch (Exception ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            throw ex;
        } finally {
            // 释放变量
        }

    }
共收到 6 条回复 时间 点赞

自己解决,又能总结分享,这样多好!!!!!!!

请教下楼主,autoName devName 这两个变量分别指的是什么?怎么取到的?求指点。。。

#3 楼 @seagull1985 这 2 个变量,在你初始化代码的时候,是你的参数,你把它变成全局变量,这样在后面应用的时候,就可以用来做标识。

#3 楼 @seagull1985 autoName 是指的 appium 参数 automationName, 而 devName 指的是 deviceName

您好,请教下楼主,我在使用 driver.manage().window().getSize(),总是会报错:麻烦您帮我看下,是版本不支持嘛?
org.openqa.selenium.WebDriverException: unknown error: operation is unsupported on Android
(Session info: webview=33.0.0.0)
(Driver info: chromedriver=2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 32 milliseconds
Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06'
System info: host: 'GTP-LAB6324', ip: '192.168.69.44', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_55'
Session ID: 9c45ec78-59e0-4597-a6f3-9be36b0e8ff3
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{platform=LINUX, app=E:\GLM_appium\apps\GLM.apk, javascriptEnabled=true, browserName=, networkConnectionEnabled=true, udid=192.168.56.101:5555, desired={autoAcceptAlerts=true, app=E:\GLM_appium\apps\GLM.apk, platformVersion=4.4, deviceName=Android Emulator, platformName=Android, browserName=, unicodeKeyboard=true, resetKeyboard=true, udid=192.168.56.101:5555}, locationContextEnabled=false, autoAcceptAlerts=true, platformVersion=4.4.4, databaseEnabled=false, platformName=Android, deviceName=192.168.56.101:5555, webStorageEnabled=false, unicodeKeyboard=true, resetKeyboard=true, warnings={}, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
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 org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.getSize(RemoteWebDriver.java:876)
at com.glm.task.Team_Task.swipeToDown(Team_Task.java:119)
at com.glm.testcase.team.TestP0_TeamSearcher.setUp(TestP0_TeamSearcher.java:36)

info: <-- POST /wd/hub/session/9c45ec78-59e0-4597-a6f3-9be36b0e8ff3/execute 200
1249.488 ms - 72
info: --> GET /wd/hub/session/9c45ec78-59e0-4597-a6f3-9be36b0e8ff3/window/curren
t/size {}
info: JSONWP Proxy: Proxying [GET /wd/hub/session/9c45ec78-59e0-4597-a6f3-9be36b
0e8ff3/window/current/size] to [GET http://127.0.0.1:9515/wd/hub/session/7ec9537
7e7edf1f5576beb0b362f881d/window/current/size] with body: {}
info: JSONWP Proxy: Got response with status 200: {"sessionId":"7ec95377e7edf1f5
576beb0b362f881d","status":13,"value":{"message":"unknown error: operation is un
supported on Android\n (Session info: webview=33.0.0.0)\n (Driver info: chrome
driver=2.1...
info: <-- GET /wd/hub/session/9c45ec78-59e0-4597-a6f3-9be36b0e8ff3/window/curren
t/size 200 23.960 ms - 290
info: [debug] Didn't get a new command in 60 secs, shutting down...

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