#3 楼 @sanlengjingvv 主要是我们的项目需要,需要对不同 app 都要进行自动化测试
#1 楼 @sanlengjingvv 这个需要知道那个 activity 的名字,要做成对各个 app 通用的话就不太好弄~
请问您这个问题解决了吗?我也遇到了同样的问题~
#2 楼 @sanlengjingvv thanks~
#1 楼 @lihuazhang 请问你的公开课哪里有资源啊?
#5 楼 @chenhengjie123 换个 API19 的模拟器,然后用 4 楼的代码就可以~貌似是我之前用的 API17 的版本不行
用的是 AppiumForWindows-1.3.4.1,莫非是我环境不对?之前用 driver.getContextHandles() 检测 webview 控件也是检测不到
#1 楼 @cpfeng0124 好像不是这个原因,我加了 sleep,手机上的界面也加载好了,但还是报同样的错~
#7 楼 @seveniruby webview 是标准实现的,我就一个 MainAcitivity,里面就只有一个 webview 控件,MainAcitivity 的内容如下:
package com.example.webview;
import android.support.v7.app.ActionBarActivity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://baidu.com");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
配置:android 5.0 模拟器 +AppiumForWindows-1.3.4.1
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
#4 楼 @xiang2743 我开启了 app 的 webview debug,修改后的 app 在真机上不能运行,但在模拟器上可以运行,但是依旧检测不到 webview
#1 楼 @xiang2743 是我要在这个 apk 的源码中添加这个语句么 WebView.setWebContentsDebuggingEnabled(true);