appium樣例代碼

com.appium.driver包下創(chuàng)建InitDriver.java類:

package com.appium.driver;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;

public class InitDriver {
    
    
    public static AndroidDriver<AndroidElement> initDriverWebapp() throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, "chinablue");
        caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
        caps.setCapability(MobileCapabilityType.UDID, "DU3ADH154V007404");
        caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
        caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.2");
        
        URL url = new URL("http://127.0.0.1:4723/wd/hub");
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(url,caps);
        return driver;
    }

    public static AndroidDriver<AndroidElement> initDriver() throws MalformedURLException{
        File apk_path = new File("apps/zhihu.apk");
        DesiredCapabilities caps = new DesiredCapabilities();
        // 與appium服務(wù)器相關(guān)的caps
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, "chinablue");
        caps.setCapability(MobileCapabilityType.APP, apk_path.getAbsolutePath());
//      手機(jī)網(wǎng)頁(yè)測(cè)試
//      caps.setCapability(MobileCapabilityType.BROWSER_NAME, "chinablue");
//      caps.setCapability(MobileCapabilityType.UDID, "127.0.0.1:62001");
//      服務(wù)端等待客戶端發(fā)送腳本命令時(shí)間
        caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 600);
        // 與android相關(guān)的caps
//      默認(rèn)就是false
        caps.setCapability(AndroidMobileCapabilityType.NO_SIGN, false);
//      支持輸入時(shí)使用中文
        caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
        caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
//      判斷連接的Android設(shè)備應(yīng)答超時(shí),默認(rèn)5s
        caps.setCapability(AndroidMobileCapabilityType.DEVICE_READY_TIMEOUT, 10);
//      如果apk的起始activity和apk開(kāi)機(jī)后穩(wěn)定顯示的activity不是一個(gè)時(shí),需要指定activity
//      caps.setCapability(AndroidMobileCapabilityType.APP_WAIT_ACTIVITY, "");
//      啟動(dòng)手機(jī)已有apk。此時(shí)指定apk包名和起始activity即可
//      caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "");
//      caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "");
        
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://127.0.0.1:4723/wd/hub"),caps);
        return driver;
    }
    
    public static AndroidDriver<AndroidElement> initDriverInstalledApp(String appPackage,String appActivity ) throws MalformedURLException{
        File apk_path = new File("apps/zhihu.apk");
        DesiredCapabilities caps = new DesiredCapabilities();

        caps.setCapability(MobileCapabilityType.DEVICE_NAME, "chinablue");
        caps.setCapability(MobileCapabilityType.UDID, "127.0.0.1:62001");
        caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
        caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, appPackage);
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, appActivity);
        
        AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
                new URL("http://127.0.0.1:4723/wd/hub"),caps);
        return driver;
    }
}

com.appium.driver包下創(chuàng)建AppiumUtils.java類:

package com.appium.driver;

import java.io.File;
import java.io.IOException;
import java.time.Duration;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidKeyCode;

public class AppiumUtils {

    /**
     * 獲取輸入框的text文本 將光標(biāo)置于輸入框的最后 根據(jù)文本長(zhǎng)度循環(huán)調(diào)用物理鍵盤刪除掃除,逐個(gè)字符進(jìn)行刪除
     * 
     * @throws InterruptedException
     * 
     *             注意:此方法對(duì)于密碼輸入框此方法無(wú)效。因密碼輸入框的特性是密碼值不會(huì)寫入到text屬性中
     *             思路:可根據(jù)密碼長(zhǎng)度強(qiáng)制刪除密碼最長(zhǎng)長(zhǎng)度次
     */
    public static void clearText(AndroidDriver<AndroidElement> driver, AndroidElement element)
            throws InterruptedException {
        element.click();
        String text = element.getText();
        // 將光標(biāo)置于文本最后
        driver.pressKeyCode(AndroidKeyCode.KEYCODE_MOVE_END);
        for (int i = 0; i < text.length(); i++) {
            driver.pressKeyCode(AndroidKeyCode.BACKSPACE);
            Thread.sleep(200);
        }
    }

    /**
     * @param driver
     * @param element
     * @param passwdMaxLength
     * @throws InterruptedException
     */
    public static void clearWithPwd(AndroidDriver<AndroidElement> driver, AndroidElement element, int passwdMaxLength)
            throws InterruptedException {
        element.click();
        driver.pressKeyCode(AndroidKeyCode.KEYCODE_MOVE_END);
        for (int i = 0; i < passwdMaxLength; i++) {
            driver.pressKeyCode(AndroidKeyCode.BACKSPACE);
            Thread.sleep(200);
        }
    }

    public static boolean isElementExist(AndroidDriver<AndroidElement> driver, By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (Exception e) {
            // TODO: handle exception
            return false;
        }
    }
    

    

//  public static void swipeToUp(AndroidDriver<AndroidElement> driver,int during,int num){
//      int width = driver.manage().window().getSize().width;
//      int height = driver.manage().window().getSize().height;
//      for (int i = 0; i < num; i++) {
//          driver.swipe(width/2, height*3/4, width/2, height*1/4, during);
//          
//      }
//  }

    public static void swipeToLeft(AndroidDriver<AndroidElement> driver,int during){
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction leftSwipe = new TouchAction(driver);
        leftSwipe.press(width/4, height/2).waitAction(Duration.ofMillis(during)).moveTo(width*3/4, height/2).release();
        leftSwipe.perform();
    }

    public static void swipeToUp(AndroidDriver<AndroidElement> driver, int during) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction upSwipe = new TouchAction(driver);
        upSwipe.press(width/2, height/4).waitAction(Duration.ofMillis(during)).moveTo(width/2, height*3/4).release();
        upSwipe.perform();

    }
    
    public static void swipeToDown(AndroidDriver<AndroidElement> driver, int during) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction downSwipe = new TouchAction(driver);
        downSwipe.press(width/2, height*3/4).waitAction(Duration.ofMillis(during)).moveTo(width/2, height/4).release();
        downSwipe.perform();

    }
    
    public static void swipeToRight(AndroidDriver<AndroidElement> driver, int during) {
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        TouchAction rightSwipe = new TouchAction(driver);
        rightSwipe.press(width*3/4, height/2).waitAction(Duration.ofMillis(during)).moveTo(width/4, height/2).release();
        rightSwipe.perform();

    }   
    
    enum SwipeDirection{
        up,down,right,left;
        public static SwipeDirection getSwipeDirection(String direction){
            return valueOf(direction);
        }
    }
    public static void swipe(AndroidDriver<AndroidElement> driver, int during, String direction){
        switch (SwipeDirection.getSwipeDirection(direction.toLowerCase())) {
        case up:
            swipeToUp(driver, during);
            break;
        case down:
            swipeToDown(driver, during);
            break;
        case right:
            swipeToRight(driver, during);
            break;
        case left:
            swipeToLeft(driver, during);
            break;
        default:
            System.out.println("方向參數(shù)只能是  up/down/right/left");
            break;
        }
    }
    
    /**
     * @param driver
     * @param by
     * @return
     * 功能:定位元素并且獲取元素結(jié)束點(diǎn)坐標(biāo)
     */
    public static Point getElementCoor(AndroidDriver<AndroidElement> driver,By by){
        AndroidElement element = driver.findElement(by);
//      // 獲取元素起始點(diǎn)坐標(biāo)
//      int startx = element.getLocation().getX();
//      int starty = element.getLocation().getY();
//      // 獲取元素的寬高
//      int width = element.getSize().getWidth();
//      int height = element.getSize().getHeight();
//      // 計(jì)算出元素結(jié)束點(diǎn)坐標(biāo)
//      int endx = startx + width;
//      int endy = starty + height;
        
        return getElementCoor(element);
        
    }
    
    /**
     * @param driver
     * @return:返回一個(gè)Point對(duì)象
     */
    public static Point getElementCoor(AndroidElement element){
        // 獲取元素起始點(diǎn)坐標(biāo)
        int startx = element.getLocation().getX();
        int starty = element.getLocation().getY();
        // 獲取元素的寬高
        int width = element.getSize().getWidth();
        int height = element.getSize().getHeight();
        // 計(jì)算出元素結(jié)束點(diǎn)坐標(biāo)
        int endx = startx + width;
        int endy = starty + height;
        
        return new Point(endx,endy);    
    }
    
    /**
     * 截圖方法
     * @param driver
     * @param filename
     * @throws IOException
     */
    public void getScreenShotcut(AndroidDriver<AndroidElement> driver,String filename) throws IOException{
        File file = driver.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("images\\"+filename+".png"));
    }
    

    
}

樣例代碼:

package com.appium.zhihu;

import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.mobile.NetworkConnection;
import org.openqa.selenium.mobile.NetworkConnection.ConnectionType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.appium.driver.AppiumUtils;
import com.appium.driver.InitDriver;
import com.sun.javafx.scene.traversal.Direction;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidKeyCode;
import io.appium.java_client.functions.ExpectedCondition;
import net.bytebuddy.asm.Advice.This;

public class ZhihuLogin {
    public static AndroidDriver<AndroidElement> driver;
    public ZhihuLogin(AndroidDriver<AndroidElement> driver){
        this.driver = driver;
    }
    
    public void login(){
//      driver.findElement(By.xpath("http://*[@text='未登錄']")).click();
        
        driver.findElementByAndroidUIAutomator("new UiSelector().text(\"未登錄\")").click();
        delayTime(200);
//      driver.findElement(By.xpath("http://android.support.v7.widget.LinearLayoutCompat/android.support.v7.widget.LinearLayoutCompat/android.widget.ImageView[1]")).click();
        driver.findElement(By.xpath("http://*[@resource-id='com.zhihu.android:id/login_phone']")).click();
        delayTime(200);
        driver.findElement(By.xpath("http://*[@text='密碼登錄']")).click();
        delayTime(200);
        driver.findElement(By.xpath("http://*[@text='輸入手機(jī)號(hào)或郵箱']")).sendKeys("15099947428");
        delayTime(200);
        driver.findElement(By.xpath("http://android.support.v7.widget.LinearLayoutCompat/android.widget.EditText[2]")).sendKeys("chinablue2018");
        delayTime(200);
        driver.findElement(By.xpath("http://*[@text='登錄']")).click();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 拿到界面上的所有資源
        if(driver.getPageSource().contains("已購(gòu)")){
            System.out.println("登陸成功");
        }else{
            System.out.println("登陸失敗");
        }
    }
    
    public static void logout(){
        clickMenu(5);
        if(AppiumUtils.isElementExist(driver, By.name("設(shè)置"))){
            driver.findElement(By.name("設(shè)置")).click();
        }else{
            AppiumUtils.swipe(driver, 500, "down");
            driver.findElement(By.name("設(shè)置")).click();
        }
        while(true){
            if(AppiumUtils.isElementExist(driver, By.id("com.zhihu.android:id/func_text"))){
                driver.findElement(By.id("com.zhihu.android:id/func_text")).click();
                driver.findElement(By.name("確定")).click();
                
                break;
            }else{
                AppiumUtils.swipe(driver, 500, "down");
            }
        }
        
    }
    
    /**
     * 
     * @param order
     */
    public static void clickMenu(int order){
        //android.widget.LinearLayout/*[@class='android.support.v7.widget.LinearLayoutCompat'][1]
        driver.findElement(By.xpath("http://android.widget.HorizontalScrollView/android.widget.LinearLayout/descendant::android.support.v7.widget.LinearLayoutCompat["+order+"]")).click();

    }
    
    public static void delayTime(long timeout){
        try {
            Thread.sleep(timeout);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void attention(){
        List<AndroidElement> titles = driver.findElements(By.xpath("http://*[@resource-id='com.zhihu.android:id/title']"));
        System.out.println("當(dāng)前頁(yè)面文章標(biāo)題數(shù)為:"+titles.size());
        for(AndroidElement title: titles){
            title.click();
            delayTime(5000);
            driver.pressKeyCode(4);
        }
    }
    
    public void loginByUiautomator(){
//      此方法只能用于Android原生APP
        driver.findElementByAndroidUIAutomator("new UiSelector().text(\"未登錄\")").clear();
        
    }
    
    
    public void nightMode(){
        clickMenu(5);
        AndroidElement nightMode = driver.findElement(By.id("com.zhihu.android:id/night_mode_switch"));
        String status = nightMode.getAttribute("checked");
        System.out.println(status);
        AppiumUtils.swipe(driver, 500, "down");
        nightMode.click();
        status = nightMode.getAttribute("checked");
        if(status.equals("true")){
            System.out.println("夜間模式打開(kāi)");
        }else{
            System.out.println("夜間模式關(guān)閉");
        }
        
    }
    
    
    /**
     * 獲取元素的起始點(diǎn)坐標(biāo)、結(jié)束點(diǎn)坐標(biāo)、中心點(diǎn)坐標(biāo)
     */
    public void getCoor(){
        
//      int end_x = AppiumUtils.getElementCoor(driver, By.id("")).getX();
//      int end_y = AppiumUtils.getElementCoor(driver, By.id("")).getY();
//      上述寫法定位元素次數(shù)有所增加,故需要優(yōu)化        
        Point p = AppiumUtils.getElementCoor(driver, By.id(""));
        int endx = p.getX();
        int endy = p.getY();            
    }
    
    public static void tap(){
        AndroidElement element = driver.findElement(By.id(""));
        element.replaceValue("");
//      element.tap();      
    }
    
    
    /**
     * 打開(kāi)另一個(gè)應(yīng)用 快手app
     */
    public void startKuaiShou(){
        Activity activity = new Activity("com.smile.gifmaker", "com.yxcorp.gifshow.HomeActivity");
//      activity.setAppWaitPackage("com.smile.gifmaker");
//      activity.setAppWaitActivity("com.yxcorp.gifshow.HomeActivity");
//      activity.setStopApp(false);
        driver.startActivity(activity);
        delayTime(5000);
        
        // 獲取當(dāng)前activity,可以判斷界面跳轉(zhuǎn)(前提是跳轉(zhuǎn)后界面的activity發(fā)生了變化)
        String currentActivity = driver.currentActivity();
        System.out.println(currentActivity);
                
    }
    
    
    /**
     * 獲取網(wǎng)絡(luò)狀態(tài):getNetworkConnection().toString();或getNetworkConnection().value();
     * 設(shè)置網(wǎng)絡(luò)狀態(tài):setNetworkConnection()
     */
    public void networkGetAndSet(){

        
//      new NetworkConnection() {
//          
//          public ConnectionType setNetworkConnection(ConnectionType type) {
//              // TODO Auto-generated method stub
//              return null;
//          }
//          
//          public ConnectionType getNetworkConnection() {
//              // TODO Auto-generated method stub
//              return null;
//          }
//      };
        NetworkConnection mobileDriver = (NetworkConnection)driver;
        
        String networkInfo = mobileDriver.getNetworkConnection().toString();
        System.out.println(networkInfo);
        
        
    }
    
    public static void getOrientation(){
        ScreenOrientation orientation = driver.getOrientation();
        // 獲取屏幕方向信息
        System.out.println(orientation.value());
        delayTime(5000);
        // 設(shè)置屏幕方向,如果app本身不支持橫豎屏切換,那么會(huì)報(bào)錯(cuò)
        driver.rotate(ScreenOrientation.LANDSCAPE);
        System.out.println(orientation.value());
    }
    
    /**
     * qq應(yīng)該
     * com.tencent.mobileqq
     * com.tencent.mobileqq.activity.SplashActivity
     */
    public void appInstallandRemove(){
        if(driver.isAppInstalled("com.tencent.mobileqq")){
            driver.removeApp("com.tencent.mobileqq");
        }
        driver.installApp("D:\\eclipse\\workspace0122\\appiumtest\\apps\\qq.apk");
        System.out.println(driver.isAppInstalled("com.tencent.mobileqq"));
    }
    
    /**
     * 顯示等待的兩個(gè)使用場(chǎng)景
     * 場(chǎng)景1:等待某個(gè)元素出現(xiàn)
     * 場(chǎng)景2:等待屬性值出現(xiàn)
     */
    public void waitUtilElement(){
        // 場(chǎng)景1:等待某個(gè)元素在30s內(nèi)出現(xiàn),不出現(xiàn)就拋出異常
        WebDriverWait wait = new WebDriverWait(driver, 30);
        AndroidElement element = (AndroidElement) wait.until(
                ExpectedConditions.presenceOfElementLocated(By.id("")));
        // 場(chǎng)景2:自定義顯示等待  getText
        WebDriverWait wait1 = new WebDriverWait(driver, 60);
        wait1.until(new ExpectedCondition<Boolean>() {

            public Boolean apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return driver.findElement(
                        By.id("")).getText().contains("注冊(cè)或登陸");
            }
        });
        // 場(chǎng)景3:自定義顯示等待  getAttribute
        WebDriverWait wait2 = new WebDriverWait(driver, 60);
        wait2.until(new ExpectedCondition<Boolean>() {

            public Boolean apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return driver.findElement(
                        By.id("")).getAttribute("checked").equals("true");
            }
        });
        // 場(chǎng)景4:自定義顯示等待  getAttribute  判斷元素是否被置灰
        WebDriverWait wait3 = new WebDriverWait(driver, 60);
        wait3.until(new ExpectedCondition<Boolean>() {

            public Boolean apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return driver.findElement(
                        By.id("")).getAttribute("enabled").equals("false");
            }
        });
    }
    
    /**
     * 如果手機(jī)鎖屏,對(duì)屏幕進(jìn)行解鎖
     */
    public void lockToUnlock(){
        if(driver.isLocked()){
            driver.unlockDevice();
        }
        
        // 長(zhǎng)按某個(gè)鍵
        driver.longPressKeyCode(3);
        
    }
    
    public void touchaction(){
        AndroidElement element = driver.findElement(By.id(""));
        TouchAction touch = new TouchAction(driver);
        // 長(zhǎng)按元素
        touch.longPress(element).release().perform();
        // 長(zhǎng)按某個(gè)坐標(biāo)點(diǎn)
        touch.longPress(300, 500).release().perform();
        // 在元素的某一個(gè)點(diǎn)上長(zhǎng)按
        touch.longPress(element, 20, 20).release().perform();
        
        touch.press(element).release().perform();
        touch.press(300, 500).release().perform();
        
        //實(shí)現(xiàn)拖拽操作  實(shí)現(xiàn)手勢(shì)解鎖
        
    }
    
    
    public void changeLanguageHit(){
        driver.pressKeyCode(AndroidKeyCode.HOME);
        driver.findElement(By.name("設(shè)置")).click();
        AppiumUtils.isElementExist(driver, By.xpath(""));
    }
    
    /**
     * 1. 通過(guò)id定位當(dāng)前屏幕的所有設(shè)置,并且獲取這個(gè)設(shè)置的文本存入到list。如果這個(gè)list大小小于15,那就向上滑動(dòng)
     * 2. 再次獲取當(dāng)前屏幕的所有設(shè)置,并且將所有設(shè)置文本存入list,并且判重。再次判斷l(xiāng)ist大小是否大于15,如果大于,直接list.get(索引值).click()完成點(diǎn)擊
     * 
     */
    public void helpClick(){
        driver.pressKeyCode(AndroidKeyCode.HOME);
        driver.findElement(By.name("設(shè)置")).click();
        List<String> settingTexts = new ArrayList<String>();
        List<AndroidElement> settings = new ArrayList<AndroidElement>();
        while(true){
            List<AndroidElement> titles = driver.findElements(By.id("android:id/title"));
            for(AndroidElement title:titles){
                String text = title.getText();
                if(!settingTexts.contains(text)){
                    settingTexts.add(text);
                    settings.add(title);
                }
            }
            System.out.println("*********");
            System.out.println(settingTexts.size());
            System.out.println("*********");
            if(settingTexts.size()>33){
                System.out.println("--------------");
                for(String a:settingTexts){
                    System.out.println(a);
                }
                System.out.println("--------------");
                settings.get(30).click();
                break;
            }else{
                AppiumUtils.swipe(driver, 1000, "down");
            }
            delayTime(3000);
        }

    }
    /**
     * 點(diǎn)擊倒數(shù)第七個(gè)
     * 先滑動(dòng)到底部,獲取最后一屏的所有title,然后點(diǎn)擊倒數(shù)第7個(gè)
     * 如何判斷滑動(dòng)到最后一屏?
     * 答:每次滑動(dòng)前后的界面是否一致,如何一致則表示滑動(dòng)到底部了,
     *    其中每個(gè)設(shè)置的文本作為判斷滑動(dòng)屏幕是否一樣的依據(jù)
     */
    public void settingInputWayClick(){
        driver.pressKeyCode(AndroidKeyCode.HOME);
        driver.findElement(By.xpath("http://*[@resource-id='com.huawei.android.launcher:id/hotseat']/android.view.View/android.view.View/android.widget.TextView[4]")).click();
        ArrayList<String> oldTexts = new ArrayList<String>();
        ArrayList<String> newTexts = new ArrayList<String>();
//      ArrayList<AndroidElement> androidelement = new ArrayList<AndroidElement>();
        List<AndroidElement> titles = driver.findElements(By.id("android:id/title"));
        for(AndroidElement title:titles){
            String text = title.getText();
            oldTexts.add(text);
        }
        AppiumUtils.swipe(driver, 500, "down");
        for(AndroidElement title:titles){
            String text = title.getText();
            newTexts.add(text);
        }
    }
    
    /**
     * 拖拽操作:知乎app拖拽到快手app的位置
     */
    public void drag(){
        driver.pressKeyCode(AndroidKeyCode.HOME);
        TouchAction touch = new TouchAction(driver);    
        AndroidElement zhihu = driver.findElement(By.name("知乎"));
        AndroidElement contact = driver.findElement(By.name("快手"));
        touch.longPress(zhihu).moveTo(contact).release().perform();
    }
    
    public void shoushisuo(){
        driver.pressKeyCode(AndroidKeyCode.HOME);
        driver.findElement(By.name("設(shè)置")).click();;
        driver.findElement(By.name("安全")).click();;
        driver.findElement(By.name("屏幕鎖定")).click();;
        driver.findElement(By.name("圖案")).click();
        
        AndroidElement element = driver.findElement(By.id("com.android.settings:id/lockPattern"));
        Point location = element.getLocation();
        int startx = location.getX();
        int starty = location.getY();
        
        Dimension size = element.getSize();
        int height = size.getHeight();
        int width = size.getWidth();
        
        // 將9個(gè)點(diǎn)加入到集合中
        // 繪制圖案、注意絕對(duì)坐標(biāo)和相對(duì)坐標(biāo). movoTo(1,1)
        TouchAction touch  = new TouchAction(driver);
        touch.press(300,500).moveTo(20,30).moveTo(100, 20).release().perform();
    }
    
    public static void main(String[] args) {
        AndroidDriver<AndroidElement> driver = null;
        try {
             driver = InitDriver.initDriver();
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ZhihuLogin zhihu = new ZhihuLogin(driver);
        
        
        
        // 此后所有的find查找元素 隱式等待時(shí)間都是10s
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        delayTime(15000);
        zhihu.login();
//      zhihu.settingInputWayClick();
//      zhihu.shoushisuo();
//      delayTime(5000);
//      zhihu.appInstallandRemove();
//      zhihu.getOrientation();
        

        
//      zhihu.delayTime(6000);
        
//      zhihu.nightMode();
        
//      for (int i = 0; i < 3; i++) {       
//      AppiumUtils.swipe(driver, 300,"down");
//      zhihu.delayTime(2000);  
//      }
        
//      zhihu.clickMenu(1);
//      zhihu.delayTime(6000);
//      logout();
        
            
//      for (int i = 0; i < 3; i++) {       
//          AppiumUtils.swipe(driver, 300,"down");
//          zhihu.delayTime(2000);  
//      }
//      
//      AppiumUtils.swipe(driver, 300,"left");
//      for (int i = 0; i < 3; i++) {
//          AppiumUtils.swipe(driver, 300,"down");
//          zhihu.delayTime(2000);  
//      }
//      
//      AppiumUtils.swipe(driver, 300,"right");
//      for (int i = 0; i < 3; i++) {
//          AppiumUtils.swipe(driver, 300,"down");
//          zhihu.delayTime(2000);  
//      }
        
        
//      while(true){
//          AppiumUtils.swipeToDown(driver, 100);
//          zhihu.delayTime(100);   
//      }
//      
//      for (int i = 0; i < 8; i++) {
//          AppiumUtils.swipeToUp(driver, 200);
//          zhihu.delayTime(3000);  
//      }   
        
        
        
        
        
//      zhihu.attention();
        
        driver.quit();
        
    }
    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 做Android端功能自動(dòng)化已有2年多的時(shí)間了,使用過(guò)的功能自動(dòng)化框架有Robotium、Uiautomat...
    海波筆記閱讀 17,217評(píng)論 3 66
  • ModelForm 以已創(chuàng)建的model為原型創(chuàng)建表單時(shí)可以繼承forms.ModelForm大多數(shù)model的字...
    wangfp閱讀 240評(píng)論 0 0
  • springMVC SpringMVC:前端控制器,映射器,適配器,控制器,試圖解析器 前端控制器 前端控制器在w...
    奔跑_孤傲的路上閱讀 172評(píng)論 0 0
  • 王二坐了一天一夜的火車,本來(lái)身體是非常疲憊的,但已經(jīng)被久別重逢的欣喜給沖散了。爺孫倆都很興奮,王二的肚子忽然咕地響...
    大圣是我閱讀 221評(píng)論 0 2

友情鏈接更多精彩內(nèi)容