关闭

Robotium原理之获取WebElement元素

发表于:2015-8-14 09:51

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:开源中国    来源:51Testing软件测试网博客

  第三步:将提示框中的消息存入WebElement Java bean中
  获取到了prompt提示框中的消息后,接下来就是对这些已经过处理含特殊格式的消息进行解析处理了,依次得到WebElement的id、text、name等字段。
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一样根据坐标发送点击事件。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号