1. 在执行test前, 通过命令启动。
java -jar E:\maven\SeleGrid2\selendroid-standalone-0.16.0-with-dependencies.jar -app "you uat path\SS_UAT.apk" -serverStartTimeout 100000 -serverStartRetries 3
打开http://localhost:4444/wd/hub/status
{"value":{"os":{"name":"Windows 7","arch":"amd64","version":"6.1"},
"build":{"browserName":"selendroid","version":"0.16.0"},
"supportedDevices":[{"emulator":true,"screenSize":"(800, 1280)","avdName":"11","platformVersion":"19","apiTargetType":"android"}],
"supportedApps":[{"mainActivity":"com.singtel.mystorage.MainActivity","appId":"com.singtel.mystorage:2.0.22.697",
"basePackage":"com.singtel.mystorage"},
{"mainActivity":"io.selendroid.androiddriver.WebViewActivity","appId":"io.selendroid.androiddriver:0.16.0","basePackage":"io.selendroid.androiddriver"}]},"status":0}
实现代码:
@Test
public void test03() throws Exception{
SelendroidCapabilities capa = new SelendroidCapabilities("com.singtel.mystorage:2.0.22.697");
//capa.setLaunchActivity("com.singtel.mystorage.MainActivity");
capa.setPlatformVersion(DeviceTargetPlatform.ANDROID19);
capa.setEmulator(true);
SelendroidDriver sd = new SelendroidDriver(capa);
System.out.println(sd.getContextHandles()); //用于返回被测app是NATIVE_APP还是WEBVIEW,如果两者都有就是混合型App
//查看元素,输入,登录等动作
sd.quit();
}
2. 通过代码
import junit.framework.Assert;
import io.selendroid.client.SelendroidDriver;
import io.selendroid.common.SelendroidCapabilities;
import io.selendroid.common.device.DeviceTargetPlatform;
import io.selendroid.standalone.SelendroidConfiguration;
import io.selendroid.standalone.SelendroidLauncher;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class NativeApp2 {
private DesiredCapabilities seCap = null;
private SelendroidConfiguration config = null;
private SelendroidLauncher selendroidServer= null;
private WebDriver wd = null;
@BeforeTest
public void setup(){
if(selendroidServer != null){
selendroidServer.stopSelendroid();
}
config = new SelendroidConfiguration();
config.addSupportedApp("SS_UAT.apk"); //设置apk相对路径
selendroidServer = new SelendroidLauncher(config);
selendroidServer.launchSelendroid();
}
@Test
public void test03() throws Exception{
seCap = new SelendroidCapabilities("com.singtel.mystorage:2.0.22.697"); //设置appid值,需要和status的appid保持一致
wd = new SelendroidDriver(seCap);
WebElement inputField = wd.findElement(By.id("my_text_field"));
inputField.sendKeys("Selendroid");
Thread.sleep(3000);
Assert.assertEquals("Selendroid", inputField.getText());
}
@AfterTest
public void teardown(){
if(selendroidServer != null){
selendroidServer.stopSelendroid();
}
}
}
参考资料
https://github.com/selendroid/demoproject-selendroid/blob/master/src/main/java/io/selendroid/demo/SelendroidIntegrationTest.java#L45