写一个使用了Androidx 的 UiAutomatorTestCase 示例
时间: 2024-02-13 17:59:57 浏览: 18
好的,以下是一个使用了Androidx的UiAutomatorTestCase示例:
```
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;
import androidx.test.uiautomator.UiAutomatorTestCase;
public class MyTestCase extends UiAutomatorTestCase {
public void testMyApp() throws UiObjectNotFoundException {
// 打开我的应用
getUiDevice().pressHome();
UiObject myApp = new UiObject(new UiSelector().text("我的应用"));
myApp.click();
// 执行一些操作
UiObject button = new UiObject(new UiSelector().text("点击按钮"));
button.click();
// 验证结果
UiObject result = new UiObject(new UiSelector().text("操作成功"));
assertTrue(result.exists());
}
}
```
在这个示例中,我们使用了Androidx的UiDevice、UiObject和UiSelector类来模拟用户的操作和验证结果。我们还继承了UiAutomatorTestCase类来创建一个测试用例。在testMyApp方法中,我们打开了一个名为“我的应用”的应用,执行了一些操作,并验证了结果。