1.优化前:
public class DTVTester extends Instrumentation {
@Before
public void setUp() throws Exception {
UiDevice device;
device = UiDevice.getInstance(getInstrumentation());
device.pressHome();
}
@Test
public void enter_homepage1() throws RemoteException {
UiDevice device;
device = UiDevice.getInstance(getInstrumentation());
device.wait(Until.hasObject(By.desc("fileDtv")),3000);
UiObject2 appButton = device.findObject(By.desc("fileDtv"));
appButton.click();
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
device.wait(Until.hasObject(By.text("完成")),3000);
UiObject2 appButton1 = device.findObject(By.text("完成"));
appButton1.click();
device.wait(Until.findObject(By.text("关闭")),3000);
UiObject2 appButton2 = device.findObject(By.text("关闭"));
appButton2.click();
device.wait(Until.findObject(By.text("确定")),3000);
UiObject2 appButton3 = device.findObject(By.text("确定"));
appButton3.click();
device.pressRecentApps();
device.wait(Until.findObject(By.text("全部清除")),3000);
UiObject2 appButton22 = device.findObject(By.text("全部清除"));
appButton22.click();
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
2.优化后
package com.cxq.myapplication.test;
import android.app.Instrumentation;
import android.os.RemoteException;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
public class DTVtester extends Instrumentation {
@Before
public void setUp() throws Exception {
UiDevice device;
device = UiDevice.getInstance (getInstrumentation ());
device.pressHome ();
}
@Test
public void enter_homepage1() throws RemoteException {
UiDevice device;
device = UiDevice.getInstance (getInstrumentation ());
device.wait (Until.hasObject (By.desc ("fileDtv")), 3000);
UiObject2 appButton = device.findObject (By.desc ("fileDtv"));
appButton.click ();
try {
Thread.currentThread ().sleep (10000);
} catch (InterruptedException e) {
e.printStackTrace ();
}
setText ("完成");
setText ("关闭");
setText ("确定");
exitapp ();
}
String Text0 = "完成";
String Text1 = "关闭";
String Text2 = "确定";
public String setText(String str){
UiDevice device;
device = UiDevice.getInstance (getInstrumentation ());
device.wait (Until.hasObject (By.text (str)),3000);
UiObject2 appButton1 = device.findObject (By.text (str));
String Text0 = str; //设置一个Text0为str
appButton1.click ();
return Text0;
}
public void exitapp() throws RemoteException {
UiDevice device;
device = UiDevice.getInstance (getInstrumentation ());
device.pressRecentApps();
device.wait(Until.findObject(By.text("全部清除")),3000);
UiObject2 appButton22 = device.findObject(By.text("全部清除"));
appButton22.click();
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}