import java.io.File; import java.io.OutputStream; import java.lang.reflect.Field; import java.util.ArrayList; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import cn.lebo.appium.base.AndroidCapabilityType; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; public class NewTest { AppiumDriverLocalService service = null; AndroidDriver<WebElement> driver = null; @SuppressWarnings("unchecked") @BeforeClass public void beforClass() { Field streamField = null; Field streamsField = null; service = AppiumDriverLocalService .buildService(new AppiumServiceBuilder().usingPort(4723).withLogFile(new File("d:\44555554.txt"))); try { streamField = AppiumDriverLocalService.class.getDeclaredField("stream"); streamField.setAccessible(true); streamsField = Class.forName("io.appium.java_client.service.local.ListOutputStream") .getDeclaredField("streams"); streamsField.setAccessible(true); } catch (ClassNotFoundException | NoSuchFieldException e) { e.printStackTrace(); } try { ((ArrayList<OutputStream>) streamsField.get(streamField.get(service))).clear(); // remove System.out logging } catch (IllegalAccessException e) { e.printStackTrace(); } service.start(); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(AndroidCapabilityType.DEVICE_NAME, "android"); capabilities.setCapability(AndroidCapabilityType.APP_PACKAGE, "com.hpplay.sdk.source.test"); capabilities.setCapability(AndroidCapabilityType.APP_ACTIVITY, "com.hpplay.sdk.source.test.MainActivity"); driver = new AndroidDriver<>(service, capabilities); } @Test public void demo1() { System.out.println("执行test"); driver.openNotifications(); System.out.println("执行" + driver.toString()); // driver.manage().logs().get("logcat"); } @AfterClass public void AfterClass() { service.stop(); System.out.println("停止test"); } }