配置:android 5.0 模拟器 +AppiumForWindows-1.3.4.1
- 这是测试源代码:
```java
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import android.webkit.WebView;
import io.appium.java_client.AppiumDriver;
import java.io.File;
import java.net.URL;
import java.util.Set;
public class AndroidWebViewTest {
private AppiumDriver driver;
@Before
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "WebView.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.example.webview");
capabilities.setCapability("appActivity", ".MainActivity");
capabilities.setCapability("platformName", "Android");
// capabilities.setCapability("deviceName","HUAWEI G700-U00");
capabilities.setCapability("deviceName","myAVD");
capabilities.setCapability("platformVersion", "21");
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@Test
public void main(){
Set contextNames = driver.getContextHandles();
System.out.println(contextNames.size());
for (String contextName : contextNames) {
System.out.println(contextName);
if(contextName.contains("WEBVIEW")||contextName.contains("webview")){
driver.context(contextName);
}else{
System.out.println("no WEBVIEW");
}
}
// Thread.sleep(15000);
// driver.context("WEBVIEW");
// System.out.print("CurrentUrl"+driver.getCurrentUrl());
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
2. 这是打印的结果
```java
1
NATIVE_APP
no WEBVIEW
- 这是控制台打屏