??????uiautomator???????watcher???÷?????????????python?????uiautomator?б?????????????????????????Щ?????????????Щ????????????Щ???????????????????????????Щ?????????????????????????????譚????????????С?
????????д????????????????????С?????????app??????????????????????????????е?????????????????“???”?????????????????????????????????Щ???????????????д????????????????Щ???????????????????????Щ?????????????????????????壬????????????????watcher??????uiautomator?????????????????????
?????????????? UiWatcher????UiDevice??registerWatcher (String name?? UiWatcher watcher)??name???????????????????????key???????????????watcher??????????ú?remove????????
?????????UiWatcher????д???checkForCondition()???????????????????????д?Щ?ж???ЩUiSelector????????????????????????????·???????????
public void initwatch(final String name??final UiSelector checkSelecto??final UiSelector opSelector){
//??name????watcherNames??list?У????tearDown??????remove????
watcherNames.add(name);
final UiObject check = new UiObject(checkSelecto);
final UiObject op = new UiObject(opSelector);
mDevice.registerWatcher(name?? new UiWatcher() {
//??????дcheckForCondition????
public boolean checkForCondition() {
try {
if (check.exists()) {
op.click();
Thread.sleep(1000);
return true;
}
else{
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
}
????checkForConditon????????????boolean?????????true???????uidevice.hasWatcherTriggered(name) ????true??checkForConditon??????д?????uiautomator???????????????????????á?
?????????????????TestUtil.java
package com.yangyanxing.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.core.UiWatcher;
public class TestUtil {
public ArrayList<String> watcherNames = new ArrayList<String>();
public UiDevice mDevice = UiDevice.getInstance();
public static String doCmdshell(String commond){
String s = null;
try
{
Process p = Runtime.getRuntime().exec(commond);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String result = "";
while ((s = stdInput.readLine()) != null)
{
result = result + s + " ";
}
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
return result;
}
catch (Exception e)
{
return "Exception occurred";
}
}
//init watcher
public void initwatch(final String name??final UiSelector checkSelecto??final UiSelector opSelector){
//??name????watcherNames??list?У????tearDown??????remove????
watcherNames.add(name);
final UiObject check = new UiObject(checkSelecto);
final UiObject op = new UiObject(opSelector);
mDevice.registerWatcher(name?? new UiWatcher() {
//??????дcheckForCondition????
public boolean checkForCondition() {
try {
if (check.exists()) {
op.click();
Thread.sleep(1000);
return true;
}
else{
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
}
public static Boolean waitForUiselectorAppears(UiSelector selector??int timeout)
{
UiObject uiObject = new UiObject(selector);
return uiObject.waitForExists(timeout*1000);
}
}
UitestRunner.java
package com.yangyanxing.test;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import static com.yangyanxing.test.TestUtil.doCmdshell;
public class UitestRunner extends UiAutomatorTestCase {
//????????UiDevice
private UiDevice mDevice = UiDevice.getInstance();
private TestUtil tUtil = new TestUtil();
public UitestRunner(){
super();
}
//дsetUp()????
public void setUp() throws Exception{
super.setUp();
//??β??????????????????????????????????setUp??
doCmdshell("am start com.qihoo.mkiller/com.qihoo.mkiller.ui.index.AppEnterActivity");
System.out.println("???????????????");
//????Щwatcher
tUtil.initwatch("ignorSafe"?? new UiSelector().textContains("???????").className("android.widget.TextView")??
new UiSelector().text("???"));
tUtil.initwatch("agree"?? new UiSelector().text("??????")??
new UiSelector().text("??????"));
tUtil.initwatch("Noupdate"?? new UiSelector().textContains("????")??
new UiSelector().text("???"));
mDevice.runWatchers();//??watchers????????
}
//дtearDown??????????????force-stop
public void tearDown() throws Exception{
super.tearDown();
doCmdshell("am force-stop com.qihoo.mkiller");
System.out.println("?????????????");
for (String watcherName : tUtil.watcherNames) {
System.out.println(watcherName+"??remove???");
mDevice.removeWatcher(watcherName);
}
}
//?????????????????"??????"???
public void test_startScanButton() throws UiObjectNotFoundException{
UiSelector scanButton = new UiSelector().className("android.widget.Button").text("??????");
if(TestUtil.waitForUiselectorAppears(scanButton?? 20)){
UiObject scanoObject = new UiObject(scanButton);
if (scanoObject.click()) {
System.out.println("?????? ???????????");
}else{
System.out.println("?????? ???????????");
}
}else {
System.out.println("????????????");
}
UiSelector exitbutton = new UiSelector().className("android.widget.Button").text("???");
assertEquals(Boolean.TRUE?? TestUtil.waitForUiselectorAppears(exitbutton?? 120));
}
public void test_print(){
System.out.println("????2??????????");
}
}