【轉】appium常用方法整理

來源:互聯網
上載者:User

標籤:異常拋出   第一個   正數   http   save   you   android   mon   親測   

1、相對座標解鎖九宮格

應用情境

QQ解鎖螢幕如上,可見九個按鍵在同一個View下面,要實現解鎖,用press   moveTo  release  perform方法

實現代碼如下:

     WebElement jiugongge = pi.findByXpath("jiugongge");//擷取九宮格所在的位置元素        final TouchAction touchAction = new TouchAction(driver);        // 元素的起始x和y座標        Point start = jiugongge.getLocation();        int startX = start.x;        int startY = start.y;        System.out.println("startX : "+startX);        System.out.println("startY : "+startY);        // 元素的寬和高        Dimension q = jiugongge.getSize();        int width = q.getWidth();        int hight = q.getHeight();        System.out.println("width : "+width);        System.out.println("hight : "+hight);        //計算每個數字間隔的距離        int jianjuX = width/3;        int jianjuY = hight/3;        System.out.println("jianjuX : "+jianjuX);        System.out.println("jianjuY : "+jianjuY);        // 計算出控制項結束座標               int endX = width + startX;        int endY = hight + startY;        System.out.println("endX :"+endX);        System.out.println("endY :"+endY);        //1 的座標        int oneX = startX + jianjuX/2;        int oneY = startY + jianjuY/2;        System.out.println("oneX : "+oneX);        System.out.println("oneY : "+oneY);

      int twoX = oneX + jianjuX;
      int twoY = oneY;
      int threeX = twoX + jianjuX;
      int threeY = oneY;

    //Z型  上下滑動時,x相對座標為0,y的相對座標為高度的jianju,相對座標值為正數時向下you滑動,為負數時向上zuo滑動          touchAction.press(oneX, oneY).waitAction(500).moveTo(jianjuX, 0).moveTo(jianjuX, 0).moveTo(-jianjuX, jianjuY).moveTo(-jianjuX, jianjuY).moveTo(jianjuX, 0).moveTo(jianjuX, 0).waitAction(500).release();        touchAction.perform(); //運行會拋異常    

      //3-2-1-4-7-8-9   運行可以通過
      touchAction.press(threeX, threeY).waitAction(500).moveTo(-jianjuX, 0).moveTo(-jianjuX, 0)
      .moveTo(0, jianjuY).moveTo(0, jianjuY).moveTo(jianjuX, 0).moveTo(jianjuX, 0).waitAction(500).release();
      touchAction.perform();



基本思路:

1、找到元素所在位置;

2、求出第一個點的座標;

3、找出平均移動的間距;

4、利用TouchAction 的press()  moveTo()等方法實現相對移動

解釋:press(oneX, oneY)是按下時的座標,moveTo()的座標就是相對於按下的那個座標而言

上下滑動時,x相對座標為0,y的相對座標為高度的jianju,相對座標值為正數時向下右滑動,為負數時向上左滑動 

moveTo方法的官方解釋,移動是相對上一個點的座標進行相對移動

/**     * Move current touch to a new position relative to the current position on     * the screen. If the current position of this TouchAction is (xOld, yOld),     * then this method will move the TouchAction to (xOld + x, yOld + y).     *     * @param x change in x coordinate to move through.     * @param y change in y coordinate to move through.     * @return this TouchAction, for chaining.     */    public TouchAction moveTo(int x, int y) {        ActionParameter action = new ActionParameter("moveTo");        action.addParameter("x", x);        action.addParameter("y", y);        parameterBuilder.add(action);        return this;    }

 

備忘:

這塊解鎖Z型的會有一個異常拋出,當然我還不知道怎麼解決,但是大概知道了為什麼會有這個異常,

我試了其他形狀,只要不包括斜著移動就可以成功運行,有斜著移就會拋出異常

 

 2、在控制項上進行上下左右滑動

應用情境:

在第一個聊天的控制項上進行左滑刪除操作

實現代碼如下: 

/**     * 根據控制項定位     * 在控制項內上下左右滑動     * @param element      控制項定位方式     * @param heading 滑動方向 UP  DOWN     */    public void swipeControl(WebElement element, Heading heading) {                // 擷取控制項位置的座標軸        Point start = element.getLocation();        int startX = start.x;        int startY = start.y;        // 擷取控制項座標軸差        Dimension q = element.getSize();        int x = q.getWidth();        int y = q.getHeight();        // 計算出控制項結束座標                int endX = x + startX;        int endY = y + startY;        // 計算中間點座標                int centreX = (endX + startX) / 2;        int centreY = (endY + startY) / 2;        switch (heading) {            // 向you滑動            case RIGHT:                driver.swipe(endX - 10, endY, centreX, endY, 500);                break;            // 向zuo滑動            case LEFT:                driver.swipe(endX- 10,endY , centreX , endY -5, 1000);                break;            //向上滑動            case UP:                driver.swipe(endX,endY + 5,centreX,centreY,1000);                break;            //向下滑動            case DOWN:                driver.swipe(endX,endY - 5,centreX,centreY,1000);                break;        }    }
/**     * 控制滑動方向     */    public enum Heading {        RIGHT, LEFT,        UP, DOWN    }

 基本思路:

1、找到要滑動的元素;

2、得到元素的起始位置;

3、利用swipe(startx,starty,endx,endy,time)函數進行滑動,time為滑動的時間,毫秒為單位

解釋:在滑動的座標可以根據自己需要的進行控制

3、清楚控制項的值

應用情境:密碼框擷取不到值,直接用appium內建的clear函數不能清除乾淨

親測,QQ的密碼框的值用clear函數有時候不能清除乾淨

實現代碼如下:

/**     * 一個一個刪除edittext控制項裡的值     * @author lu     * @param driver      * @param text     */    public void clearText(String text ,int second) {        driver.pressKeyCode(123);//123:游標移動到輸入框最右邊        if(text.length()!=0)            driver.pressKeyCode(67);//67刪除        CommonUtils.sleep(second);    }

 4、操作

public static void snapshot(AndroidDriver driver, String filename) {//        CommonUtils.sleep(2);        boolean ret = ViewUtils.waitForWebViewInit(driver,"WEBVIEW_com.eshare.Purse");        if (ret) {            driver.context("NATIVE_APP");        }        String currentPath = System.getProperty("user.dir"); // get current work            // folder        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);        try {            System.out.println("save snapshot path is:" + currentPath + "/screen/" + filename);            FileUtils.copyFile(scrFile, new File(currentPath + "\\screen\\" + filename));        } catch (IOException e) {            System.out.println("Can‘t save screenshot");            e.printStackTrace();        } finally {            ret = false;            if (!ret) {                driver.context("WEBVIEW");            }            System.out.println("screen shot finished, it‘s in " + currentPath + " folder");            CommonUtils.sleep(2);        }

代碼如上

對於混合型APP(NATIVE_APP,WEBVIEW混合)

由代碼可見,我在之前,先判斷了當前是原生頁面還是webview頁面,操作之後又將其還原為WEBVIEW模式

這是因為,這個操作在webview模式下會提示一個異常錯誤

所以在前,先將其轉換為NATIVE_APP模式。

5、判斷頁面是否存在某個元素

public boolean isElementExist(String xpath ){        try{            driver.findElement(By.xpath(xpath));            return  true;        }catch(org.openqa.selenium.NoSuchElementException ex){            return false;        }    }

我之前有用過isDisplay()函數,但是這個函數只是判斷元素是否顯示,它使用的前提是元素存在

對於不存在元素,要判斷其是否存在,見如上代碼。 

【轉】appium常用方法整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.