java+appium+android--长按、九宫格解锁操作

    /**
     * 长按功能
     * @param id 需要长按的元素id
     */
    public void longClick(String id){

        WebElement el = getDriver().findElement(MobileBy.id(id));
        // 获取控件的长度、高度
        int elW= el.getSize().getWidth();
        int elH= el.getSize().getHeight();
        // 获取控件左上角坐标
        int x = el.getLocation().getX();
        int y = el.getLocation().getY();
        TouchAction touchAction = new TouchAction((AndroidDriver) getDriver());
        PointOption pointOption = PointOption.point(x+elW/2,y+elH/2);
        touchAction.longPress(pointOption).perform().release();


    }
public class ScratchableLateSwipe {



    static AndroidDriver driver;

    public static void main(String[] args) throws MalformedURLException, InterruptedException {

        //打开测试App

        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();

        desiredCapabilities.setCapability("platformName", "Android");

        desiredCapabilities.setCapability("appPackage", "com.xxzb.fenwoo");

        desiredCapabilities.setCapability("deviceName", "Android Emulator");

        desiredCapabilities.setCapability("appActivity", "com.xxzb.fenwoo.activity.addition.WelcomeActivity");

        //添加noReset 不清除数据  值取布尔值

        //默认不加的值为false

        desiredCapabilities.setCapability("noReset", true);

        URL remoteUrl = new URL("http://localhost:4723/wd/hub");

        driver = new AndroidDriver(remoteUrl, desiredCapabilities);

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        //九宫格解锁 -- 多次连续滑动

        Thread.sleep(5000);



        WebElement webElement = driver.findElement(MobileBy.id("com.xxzb.fenwoo:id/lpv_password"));



        // 获取屏幕的宽度和高度

        int windowWidth = driver.manage().window().getSize().getWidth();

        int windowHeight = driver.manage().window().getSize().getHeight();

        // 获取控件的宽度和高度

        int elementWidth = webElement.getSize().getWidth();

        int elementHeight = webElement.getSize().getHeight();

        // 获取当前元素element的起始点和终点

        int x = webElement.getLocation().getX();

        int y= webElement.getLocation().getY();



        System.out.println("windowWidth:" + windowWidth);

        System.out.println("windowHeight:" + windowHeight);

        System.out.println("elementWidth:" + elementWidth);

        System.out.println("elementHeight:" + elementHeight);

        System.out.println("x:" + x);

        System.out.println("y:" + y);



        // 6个点的位置

        PointOption p1 = PointOption.point(x+elementWidth*5/6,y+elementHeight/6);

        PointOption p2 = PointOption.point(x+elementWidth/2,y+elementHeight/6);

        PointOption p3 = PointOption.point(x+elementWidth/6,y+elementHeight/2);

        PointOption p4 = PointOption.point(x+elementWidth/6,y+elementHeight*5/6);

        PointOption p5 = PointOption.point(x+elementWidth/2,y+elementHeight*5/6);

        PointOption p6 = PointOption.point(x+elementWidth*5/6,y+elementHeight/2);





        //初始化TouchAction对象开始进行滑动操作

        TouchAction touchAction = new TouchAction(driver);

        touchAction.press(p1).moveTo(p2).moveTo(p3).moveTo(p4).moveTo(p5).moveTo(p6).moveTo(p1).release().perform();

 

posted @ 2021-03-24 18:30  追大奔的兔子  阅读(76)  评论(0)    收藏  举报