其他分享
首页 > 其他分享> > [Appium] 第一个Mac安卓测试小脚本

[Appium] 第一个Mac安卓测试小脚本

作者:互联网

1.打开mumu模拟器

 自行设置尺寸像手机屏幕,我的是宽520 高820

2. 打开Appium Server GUI

直接点击启动服务

3. 使用idea或eclipse创建空maven项目jdk选1.8并添加如下依赖


    <dependencies>
        <!-- appium的依赖-->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.4</version>
        </dependency>
    </dependencies>

4. 下面是我写的测试脚本,测试app是企业微信(安装包官网自行下载)

package com.minhw;

import io.appium.java_client.AppiumDriver;
//import io.appium.java_client.AndroidKeyCode;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.UnreachableBrowserException;

public class Main {
    
    /**
     * 所有和AppiumDriver相关的操作都必须写在该函数中
     * @param driver
     */
    public void test(AppiumDriver driver) {
        try {
            Thread.sleep(6000);		//等待6s,待应用完全启动
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS); //设置尝试定位控件的最长时间为8s,也就是最多尝试8s
        /*
         * 余下的测试逻辑请按照题目要求进行编写
         */
        driver.findElement(By.xpath("//*[@text='工作台']")).click();
//		(JavascriptExecutor)
//		driver.executeScript("mobile: scrollTo", new HashMap<String, String>() {{
//			put("element", driver.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]")).getText());
//			}});
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
//        System.out.println(width);
//        System.out.println(height);
        for (int i = 0; i < 2; i++) {
            driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, 100);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        WebElement ricElement = driver.findElement(By.xpath("//*[@text='日程']"));
        ricElement.click();

        driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/icw\"]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.TextView[1]")).
                click();
        driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/ix0\"]/android.widget.RelativeLayout[2]")).
                click();
//		        nr
        driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/bqv\"]")).
                click();
        driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/bqv\"]")).
                sendKeys("这是一个待办test");
//		        ren
        driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/isw\"]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]")).

                click();
        try {
            driver.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ListView[1]/android.widget.RelativeLayout[4]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.widget.ImageView[1]")).click();
        } finally {
            // TODO: handle finally clause
            driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/hkb\"]")).click();
//					选人确定
        }
        driver.findElement(By.id("com.tencent.wework:id/ist")).click();
        driver.findElement(By.id("com.tencent.wework:id/inp")).
                click();
        int width1 = driver.manage().window().getSize().width;
        int height1 = driver.manage().window().getSize().height;
        System.out.println(width1);
        System.out.println(height1);
        for (int i = 0; i < 1; i++) {
            driver.swipe(width1 / 2, height1 * 3 / 4, width1 / 2, height1 / 4, 100);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/ckm\"]")).

                click();

        driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/iv0\"]")).

                click();

        try {
            driver.findElement(By.xpath("//*[@resource-id=\"com.tencent.wework:id/iti\"]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]")).click();
            System.out.println("日程保存成功");
        } catch (
                Exception e) {
            // TODO: handle exception
            System.err.println("日程保存失败");
        }
    }

    /**
     * AppiumDriver的初始化逻辑必须写在该函数中
     *
     * @return
     */
    public AppiumDriver initAppiumTest() {

        AppiumDriver driver = null;
//        File classpathRoot = new File(System.getProperty("user.dir"));
//        File appDir = new File(classpathRoot, "apk");
//        File app = new File(appDir, "qichezhijia.apk");

        // 设置自动化相关参数
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", "");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("platformVersion", "6.0.1");
        capabilities.setCapability("appPackage", "com.tencent.wework");
        capabilities.setCapability("appActivity", ".launch.LaunchSplashActivity");
//        capabilities.setCapability("noSign", "true");

        // 设置apk路径
//        capabilities.setCapability("app", app.getAbsolutePath());

        // 设置使用unicode键盘,支持输入中文和特殊字符
        capabilities.setCapability("unicodeKeyboard", "true");
        // 设置用例执行完成后重置键盘
        capabilities.setCapability("resetKeyboard", "true");
        // 初始化
        try {
            driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return driver;
    }

    public void start() {
        test(initAppiumTest());
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.start();
    }

}

 目录结构如下图

 5. 最后运行main就开始执行脚本了。

标签:widget,安卓,driver,Appium,Mac,findElement,import,android,id
来源: https://blog.csdn.net/qq_37845699/article/details/121343679