Appium java-client获取手机型号

Appium java-client获取手机型号

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

AppiumTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :