1. 程式人生 > >【轉】appium常用方法整理

【轉】appium常用方法整理

利用 context finished too 取不到值 結束 刪除 per author

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常用方法整理