Appium webView 测试问题,无法检测到 webView 控件

zqting124 · April 09, 2015 · Last by zqting124 replied at April 10, 2015 · 2001 hits

测试代码如下:

Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
      System.out.println(contextName);
}

apk 的 layout 文件内容如下,就只有一个 WebView 控件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.webviewtest.MainActivity" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:layout_alignParentTop="true"/>

</RelativeLayout>
  1. 为什么无论是在 android4.4 以下或以上系统,上述结果输出都是:NATIVE_APP?
  2. driver.getCurrentUrl() 方法能否获得 webView 里加载页面 url 链接?每次调用该方法时都会报错
共收到 8 条回复 时间 点赞

如果你用的是真机,可能是这个原因: For a real device you need to have "setWebContentsDebuggingEnabled" in the web view. Without it the web view is not exposed to automation tools

2Floor has deleted

#1 楼 @xiang2743 是我要在这个 apk 的源码中添加这个语句么 WebView.setWebContentsDebuggingEnabled(true);

#3 楼 @zqting124 你用模拟器试下,看能不能识别到

#4 楼 @xiang2743 我开启了 app 的 webview debug,修改后的 app 在真机上不能运行,但在模拟器上可以运行,但是依旧检测不到 webview

配置:android 5.0 模拟器 +AppiumForWindows-1.3.4.1

  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
  1. 这是控制台打屏

webview 是标准实现吗. 还是自定义的方案? 所有版本都不能获得 webview 挺奇怪. 感觉更像是跟你的 app 相关

8Floor has deleted

#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);
    }
}

#7 楼 @seveniruby 恒温,请问下,我在模拟器上可以识别 WebView,真机上无法识别到控件,是不是一定需要把 setWebContentsDebuggingEnabled 这个选项打开呢?

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up