Robotium源码webview的实现
来源:互联网 发布:淘宝手表代购是真的吗 编辑:程序博客网 时间:2024/06/06 02:41
webview实现的原理:在Robotium源码中有个RobotiumWeb.js的javascript文件,我们都知道在pc浏览器的地址栏里可以通过输入javascript文件来执行javascript脚本,而在这里我们可以通过调用webview.loadUrl("javascript")方法来执行javascript脚本。具体实现如下;
首先我们在WebUtil.java类中使用getJavaScriptAsString()方法读取javascript文件
private String getJavaScriptAsString() {
InputStream fis = getClass().getResourceAsStream("RobotiumWeb.js");
StringBuffer javaScript = new StringBuffer();
try {
BufferedReader input = new BufferedReader(new InputStreamReader(fis));
String line = null;
while (( line = input.readLine()) != null){
javaScript.append(line);
javaScript.append("\n");
}
input.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return javaScript.toString();
}
然后通过executeJavaScriptFunction()方法执行javascript脚本
private boolean executeJavaScriptFunction(final String function){
final WebView webView = viewFetcher.getFreshestView(viewFetcher.getCurrentViews(WebView.class));
if(webView == null){
return false;
}
final String javaScript = prepareForStartOfJavascriptExecution();
activityUtils.getCurrentActivity(false).runOnUiThread(new Runnable() {
public void run() {
if(webView != null){
webView.loadUrl("javascript:" + javaScript + function);
}
}
});
return true;
}
那么如何把javascript脚本的执行结果返回给Robotium的呢
我们先看一个javascript中的function:
function promptElement(element) {
var id = element.id;
var text = element.innerText;
if(text.trim().length == 0){
text = element.value;
}
var name = element.getAttribute('name');
var className = element.className;
var tagName = element.tagName;
var attributes = "";
var htmlAttributes = element.attributes;
for (var i = 0, htmlAttribute; htmlAttribute = htmlAttributes[i]; i++){
attributes += htmlAttribute.name + "::" + htmlAttribute.value;
if (i + 1 < htmlAttributes.length) {
attributes += "#$";
}
}
var rect = element.getBoundingClientRect();
if(rect.width > 0 && rect.height > 0 && rect.left >= 0 && rect.top >= 0){
prompt(id + ';,' + text + ';,' + name + ";," + className + ";," + tagName + ";," + rect.left + ';,' + rect.top + ';,' + rect.width + ';,' + rect.height + ';,' + attributes);
}
}
从上面的function可以看出最后通过prompt弹出框显示elment的属性信息。
我们知道webview的内核是webkit,在webkit中有两个类协助webview。webviewclient与webchromeclient。
WebViewClient主要帮助WebView处理各种通知、请求事件的
WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等
所以Robotium建立了一个WebChromClient的子类RobotiumWebClient,并重写了 onJsPrompt()方法来接收javascript传回的信息并通过webElementCreator类来存储webview中Element。
- Robotium源码webview的实现
- Robotium源码webview的实现
- Robotium中webview源码分析
- Robotium框架的使用和源码解析
- robotium 源码网
- robotium源码分析 ClickView
- robotium导入源码学习
- Robotium searchText源码分析
- robotium源码分析 ClickView
- robotium关键源码解释
- robotium有源码
- robotium无源码环境
- Robotium整体源码浅析
- robotium源码分析 ClickView
- Robotium整体源码浅析
- 雾山的Robotium学习笔记----源码的导入
- 雾山的Robotium学习笔记----源码的导入 .
- 雾山的Robotium学习笔记---修改Rototium源码
- eclipse maven plugin 插件 安装 和 配置
- jquerymobile demo实例
- Linux 内核配置机制(make menuconfig、Kconfig、makefile)讲解
- c++二级文件系统文档
- Shell 常用命令集锦
- Robotium源码webview的实现
- 十七周 委派任务
- 各省市地理坐标范围汇总
- <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0"/>
- Android下定义按钮样式
- 统计分词/无字典分词学习(2):n-gram词频统计
- 加强网站死页治理 提升搜索引擎亲和力
- 网络请求处理与数据处理返回对象
- Robotium 之Android 功能自动化测试关于关键盘弹出的处理