Android的UI Automato使用代码案例
时间: 2024-05-02 20:23:52 浏览: 208
好的,以下是一个Android UI Automator的代码示例:
```java
import androidx.test.uiautomator.*;
public class AndroidUIAutomatorTest {
public static void main(String[] args) throws UiObjectNotFoundException {
// 启动应用程序
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
device.pressHome();
// 打开应用程序
UiObject allAppsButton = device.findObject(new UiSelector().description("All apps"));
allAppsButton.clickAndWaitForNewWindow();
UiObject appsTab = device.findObject(new UiSelector().text("Apps"));
appsTab.click();
UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
appViews.setAsHorizontalList();
UiObject calculatorApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "Calculator");
calculatorApp.clickAndWaitForNewWindow();
// 在计算器中执行一些操作
UiObject sevenButton = device.findObject(new UiSelector().resourceId("com.android.calculator2:id/digit_7"));
UiObject plusButton = device.findObject(new UiSelector().resourceId("com.android.calculator2:id/op_add"));
UiObject threeButton = device.findObject(new UiSelector().resourceId("com.android.calculator2:id/digit_3"));
UiObject equalsButton = device.findObject(new UiSelector().resourceId("com.android.calculator2:id/eq"));
sevenButton.click();
plusButton.click();
threeButton.click();
equalsButton.click();
// 验证结果是否正确
UiObject resultText = device.findObject(new UiSelector().resourceId("com.android.calculator2:id/result"));
assertEquals("10", resultText.getText());
}
}
```
这个例子演示了如何使用Android UI Automator测试应用程序。在这个例子中,我们打开了计算器应用程序,并在它上面执行了一些操作,然后验证结果是否正确。
阅读全文