Appium java-client获取手机型号

Appium java-client获取手机型号

测试平台:红米 Note 4X
操作系统:Android 7.0

AppiumTest.java

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import java.net.URL;

import java.net.MalformedURLException;

public class AppiumTest {
    protected AndroidDriver<AndroidElement> driver = null;

    /** 用例是否执行成功标识 */
    public boolean isPassed = false;

    /** 设备的deviceName */
    private final String DEVICE_NAME = "Redmi Note 4X";

    private final String URL = "http://127.0.0.1:4723/wd/hub";

    @BeforeTest
    public void beforeTest() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("automationName", "Appium");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", DEVICE_NAME);
        // capabilities.setCapability("appPackage","com.miui.home");
        // capabilities.setCapability("appActivity",".launcher.Launcher");
        capabilities.setCapability("appPackage","com.android.settings");
        capabilities.setCapability("appActivity",".Settings");
        capabilities.setCapability("newCommandTimeout", 5000);
        driver = new AndroidDriver<AndroidElement>(new URL(URL), capabilities);
    }

    @AfterTest
    public void AfterTest() {
        driver.quit();
    }
}

SettingsTest.java

import io.appium.java_client.android.AndroidKeyCode;
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class SettingsTest extends AppiumTest {

    @Test
    public void testDeviceInfo() throws InterruptedException {
        //driver.findElementByXPath("//android.widget.TextView[@text='我的设备']").click();  //错误
        //driver.findElementByXPath("//android.widget.TextView[contains(@text, 我的设备)]").click();
        //driver.findElement(By.xpath("//android.widget.TextView[contains(@text, 我的设备)]")).click();
        driver.findElement(By.xpath("//android.widget.ListView/android.widget.LinearLayout[contains(@index, 1)]")).click();
        Thread.sleep(1000);
        //String deviceName = driver.findElementByXPath("//android.widget.ListView/android.widget.LinearLayout[0]/android.widget.TextView").getText();  //错误
        String deviceName = driver.findElementByXPath("//android.widget.ListView/android.widget.LinearLayout[contains(@index, 0)]/android.widget.TextView").getText();
        System.out.print("Device Name: " + deviceName);
        driver.pressKeyCode(AndroidKeyCode.KEYCODE_HOME);
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/28/appium-java-client-get-phone-model/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Appium java-client获取手机型号
Appium java-client获取手机型号 测试平台:红米 Note 4X 操作系统:Android 7.0 AppiumTest.java import io.appium.java_client.android.AndroidDriver; imp……
<<上一篇
下一篇>>
文章目录
关闭
目 录