robotium原理之获取WebElement元素
版权声明:本文为博主原创文章,未经博主同意不得转载。
https://blog.csdn.net/hunterno4/article/details/35569665
RobotiumWeb.js就是此功能实现用的JS脚本。以solo中getWebElements()为例,
public ArrayList<WebElement> getWebElements(boolean onlySufficientlyVisible){
boolean javaScriptWasExecuted = executeJavaScriptFunction("allWebElements();");
return getWebElements(javaScriptWasExecuted, onlySufficientlyVisible);
}
private boolean executeJavaScriptFunction(final String function){
final WebView webView = viewFetcher.getFreshestView(viewFetcher.getCurrentViews(WebView.class, true));
if(webView == null){
return false;
}
//做一些JS注入运行前的准备工作。比如将WebView设为可同意运行JS等,并将RobotiumWeb.js中的脚本以String形式返回
final String javaScript = prepareForStartOfJavascriptExecution();
activityUtils.getCurrentActivity(false).runOnUiThread(new Runnable() {
public void run() {
if(webView != null){
webView.loadUrl("javascript:" + javaScript + function);
}
}
});
return true;
}
javascript:
function allWebElements() {
for (var key in document.all){
try{
promptElement(document.all[key]); //调用promptElement(element)函数
}catch(ignored){}
}
finished(); //运行完后,调用finished()函数
}
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); //弹出包括id、text、name等字段的提示框
}
}
function finished(){
prompt('robotium-finished'); //弹出包括robotium-finished字符串的提示框,用于标识脚本注入运行结束
}
onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) |
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult r) {
if(message != null && (message.contains(";,") || message.contains("robotium-finished"))){
//假设提示框中包括robotium-finished字符串,即表示那段JS注入脚本运行完毕了
if(message.equals("robotium-finished")){
webElementCreator.setFinished(true);
}
else{
webElementCreator.createWebElementAndAddInList(message, view);//有人提示框中的内容。那么就能够对提示框中的内容进行处理了
}
r.confirm();
return true;
}
else {
if(originalWebChromeClient != null) {
return originalWebChromeClient.onJsPrompt(view, url, message, defaultValue, r);
}
return true;
}
}
将JavaScriptEnabled设置为true。将将WebChromeClient设置为robotiumWebClient
public void enableJavascriptAndSetRobotiumWebClient(List<WebView> webViews, WebChromeClient originalWebChromeClient){
this.originalWebChromeClient = originalWebChromeClient;
for(final WebView webView : webViews){
if(webView != null){
inst.runOnMainSync(new Runnable() {
public void run() {
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(robotiumWebClient);
}
});
}
}
}
private WebElement createWebElementAndSetLocation(String information, WebView webView){
String[] data = information.split(";,"); //将消息按;,符号切割,当中;,符号是在前面运行JS时增加的
String[] elements = null;
int x = 0;
int y = 0;
int width = 0;
int height = 0;
Hashtable<String, String> attributes = new Hashtable<String, String>();
try{
x = Math.round(Float.valueOf(data[5]));
y = Math.round(Float.valueOf(data[6]));
width = Math.round(Float.valueOf(data[7]));
height = Math.round(Float.valueOf(data[8]));
elements = data[9].split("\\#\\$");
}catch(Exception ignored){}
if(elements != null) {
for (int index = 0; index < elements.length; index++){
String[] element = elements[index].split("::");
if (element.length > 1) {
attributes.put(element[0], element[1]);
} else {
attributes.put(element[0], element[0]);
}
}
}
WebElement webElement = null;
try{
webElement = new WebElement(data[0], data[1], data[2], data[3], data[4], attributes);//将id、text、name等字段存入
setLocation(webElement, webView, x, y, width, height);
}catch(Exception ignored) {}
return webElement;
}
/**
* Sets the location of a {@code WebElement}
*
* @param webElement the {@code TextView} object to set location
* @param webView the {@code WebView} the text is shown in
* @param x the x location to set
* @param y the y location to set
* @param width the width to set
* @param height the height to set
*/
private void setLocation(WebElement webElement, WebView webView, int x, int y, int width, int height ){
float scale = webView.getScale();
int[] locationOfWebViewXY = new int[2];
webView.getLocationOnScreen(locationOfWebViewXY);
int locationX = (int) (locationOfWebViewXY[0] + (x + (Math.floor(width / 2))) * scale);
int locationY = (int) (locationOfWebViewXY[1] + (y + (Math.floor(height / 2))) * scale);
webElement.setLocationX(locationX);
webElement.setLocationY(locationY);
}
至此。WebElement对象中包括了id、text、name等字段。还包括了x、y坐标,知道了坐标后就能够像其他Android中的原生View一样依据坐标发送点击事件。
robotium原理之获取WebElement元素的更多相关文章
- Selenium 获取隐藏元素的内容
第一种 先获取元素通过 属性获取 WebElement webElemt= webElement.findElement(By.xpath("//*[@class='xxxxxx]/a&qu ...
- selenium定位方式-获取标签元素:find_element_by_xxx
定位方式取舍# 唯一定位方式.多属性定位.层级+角标定位(离目标元素越近,相对定位越好) # 推荐用css selector(很少用递进层次的定位)# 什么时候用xpath呢? 当你定位元素时,必须要 ...
- vue获取dom元素注意问题
mounted(){ setTimeout(()=>{ this.contentToggle(); },1000) }, methods:{ contentToggle(){ console.l ...
- Robotium原理初步--Android自动化测试学习历程
章节:自动化基础篇——Robotium原理初步(第四讲) 主要讲解内容与笔记: 一.基于控件 1.spinner——下拉菜单 2.TabHost——左右滑动选择菜单,类似电话本 3.Gallery—— ...
- selenium 获取某元素的 某属性 的值
selenium 获取某元素的 某属性的值 1 先通过元素定位,获得此元素的 WebElement; WebElement yuansu = driver.findElement(By.clas ...
- javascript实现浏览器管理员工具鼠标获取Html元素 并生成 xpath
javascript实现浏览器管理员工具鼠标获取Html元素 并生成 xpath 看看标题就被吓尿了,够长吧.让我们看看到底是个什么玩意.. 直接上图: 就是这个东东了,做为一个写爬虫的,有必要了解下 ...
- selenium+python自动化104-如何获取隐藏元素text文本
前言 首先 selenium 是可以定位到隐藏元素的,但是 selenium 不能跟隐藏元素交互,也就是隐藏元素element不能使用element.click()方法. 隐藏元素element.te ...
- 如何通过源生js获取一个元素的具体样式值 /* getCss:获取指定元素的具体样式的属性值 curElement:[dom对象] attr:[string] */
昨天的博客些的真的是惨不忍睹啊!!!但是我的人生宗旨就是将不要脸的精神进行到底,所以,今天我又来了.哈哈哈哈哈! 方法一:元素.style.属性名:(这个有局限性--只能获取行内样式的值,对于样式表或 ...
- v-for遍历出的元素上添加click事件,获取对应元素上的属性id值
<span v-for="(n,nav) in floorList" data-id="{{nav.itemId}}" v-on:click=" ...
随机推荐
- [I2C]pca9555应用层测试代码
注意点: 如果在设置I2C_SLAVE的时候,提示device_busy,可以使用I2C_SLAVE_FORCE, 在驱动里面二者对应同一个case语句 应用层可以调用接口:i2c_smbus_wri ...
- HeadFisrt 设计模式03 装饰者
类应该对扩展开放, 对修改关闭. 所谓装饰者模式, 是指用其他的类来装饰某个类, 装饰者说白了就是使用 has-a 来代替 is-a 隐喻 咖啡店, 有很多种咖啡, 咖啡里还要增加一些 milk, 面 ...
- MS SQL Server2012中的EOMONTH函数
MS SQL Server2012中的EOMONTH函数 这个函数是获取一个指定日期所在月份最后一天的日期.可以得到某一个月月份的最后一天 如: declare @orderdate date=' ...
- jQuery 中get 和post 方法传值注意事项
用 jQuery 的都知道,jQuery 的 get 和 post 方法有三个参数:地址,数据 和回调函数,但我们知道地址也可以跟随数据的(形如:get_data.php?v1=1&v2=2) ...
- NodeJS与Javascript时代
如果你一直在关注互联网的相关技术,你应该会有这样一种感觉,web技术正在发生着变革,虽然我们不愿相信,但一个事实已经越来越清晰的摆在了眼前:LAMP组合的时代将要成为历史,在web诞生的二十年间,它影 ...
- 精心收集的Hadoop学习资料(持续更新)
转自:http://blog.csdn.net/wypblog/article/details/17528851 最近发现自己收集到的Hadoop学习资料有很多本,想想放在那里也浪费,所以觉得贴出来给 ...
- linux 下简单的ftp客户端程序
该ftp的客服端是在linux下面写,涉及的东西也比较简单,如前ftp的简单介绍,知道ftp主要的工作流程架构,套接字的创建,还有就是字符串和字符的处理.使用的函数都是比较简单平常易见的,写的时候感觉 ...
- 【BZOJ】1088: [SCOI2005]扫雷Mine(递推)
http://www.lydsy.com/JudgeOnline/problem.php?id=1088 脑残去想递推去了... 对于每一个第二列的格子,考虑多种情况,然后转移.....QAQ 空间可 ...
- 把本地仓库导入到Github
1, create a new rep 2, 进入到本地仓库: $ git remote set-url origin URL 3,上传 git push -v ref: https://www.do ...
- SQL UNIQUE Constraint
SQL UNIQUE Constraint The UNIQUE constraint uniquely identifies each record in a database table. The ...