在前文开源类库iQuery Android版使用说明和类jQuery selector的控件查询iQuery开源类库介绍中,介绍了iQuery的基本用法。
iQuery是一个开源的自动化测试框架项目,有兴趣的朋友可以在这里下载:
https://github.com/vowei/iQuery/downloads
源码位置:
https://github.com/vowei/iQuery
iQuery的一个主要目标就是提供一个跨平台的控件查询机制,那就需要考虑如下几个平台差异性:
“>> ExpandableListView”
“>> UIAScrollView”
“:radio”
“:button [name = ‘确定’]”
“:button [mText = ‘确定’]”
“:button [:text = ‘确定’]”
为了尽可能的支持更多的框架,iQuery将上面的性质封装成两个接口,因此对新平台的支持,只要实现这两个接口就可以了
本文介绍扩展伪类、伪属性和添加对新平台支持的方法,后续文章会解释支持其他编程语言的做法。
在Java版本中,在iQA.Runtime.jar包里,可以通过iQueryParser. registerPseudoClass这个函数注册一个新的伪类,步骤如下:
iQueryParser. createParser(String iquery, boolean registerPseudo)
parser.registerPseudoClass("text", new IPseudoClass() { public boolean resolve(ITreeNode node) { return filterByNameEndsWith(node, "EditText"); } });
在JavaScript版本中,暂时不支持扩展伪类的做法,后续版本会添加这个功能。
在Java版中,通过iQueryParser.registerPseudoAttribute函数注册一个新的伪属性,步骤如下:
parser.registerPseudoAttribute("bottom", new IPseudoAttribute() { public String resolve(ITreeNode node) { return node.getProperty("mBottom").getValue(); } });
在iOS的JavaScript版本中的做法是:
#import "common.js"; #import "antlr3-all-min.js"; #import "iQueryLexer.js"; #import "iQueryParser.js"; #import "error.js";
var iq = new iQuery(selector);
iq.parser.registerPseudoAttrs("bottom", function(uiaobj) { if ( uiaobj != undefined && uiaobj.rect != undefined ) { var rect = uiaobj.rect(); return rect.origin.y + rect.size.height; } });
当前支持Java版本的扩展,扩展方式是:
cc.iqa.iquery.android.SoloTreeNode.java:
package cc.iqa.iquery.android; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import android.view.View; import android.view.ViewGroup; import cc.iqa.iquery.*; public class SoloTreeNode implements ITreeNode { private View _view = null; public SoloTreeNode(View view) { _view = view; } public SoloTreeNode(View view, SoloTreeNode parent) { this(view); _parent = parent; } public View getView() { return _view; } @Override public String getName() { return _view.getClass().toString(); } public boolean containsProperty(String key) { return getMethod(key) != null || getField(key) != null; } public IProperty getProperty(String key) { Method method = getMethod(key); Object value = null; if (method != null) { try { value = method.invoke(_view); } catch (IllegalArgumentException e) { return null; } catch (IllegalAccessException e) { return null; } catch (InvocationTargetException e) { return null; } } else { Field field = getField(key); if ( field != null ) { field.setAccessible(true); try { value = field.get(_view); } catch (IllegalArgumentException e) { return null; } catch (IllegalAccessException e) { return null; } } } return new SoloProperty(key, value.toString()); } private ITreeNode _parent; @Override public ITreeNode getParent() { return _parent; } private List<ITreeNode> _children; @Override public List<ITreeNode> getChildren() { if (_children == null) { _children = new ArrayList<ITreeNode>(); if (_view instanceof ViewGroup) { addChildren(_children, (ViewGroup) _view); } } return _children; } private Method getMethod(String key) { Class<?> cls = _view.getClass(); String getter = String.format("%1$s", key); try { return cls.getMethod(getter); } catch (SecurityException e) { return null; } catch (NoSuchMethodException e) { return null; } } private Field getField(String key) { Class<?> cls = _view.getClass(); Field ret = null; while ( ret == null && cls != null ) { try { ret = cls.getDeclaredField(key); } catch (SecurityException e) { } catch (NoSuchFieldException e) { } if ( ret != null ) break; try { ret = cls.getField(key); } catch (SecurityException e) { } catch (NoSuchFieldException e) { } cls = cls.getSuperclass(); } return ret; } private void addChildren(List<ITreeNode> children, ViewGroup viewGroup) { for (int i = 0; i < viewGroup.getChildCount(); i++) { final View child = viewGroup.getChildAt(i); children.add(new SoloTreeNode(child, this)); } } @Override public String getType() { return _view.getClass().toString(); } @Override public String getText() { return getProperty("mText").getValue(); } }
cc.iqa.iquery.android.SoloProperty.java:
package cc.iqa.iquery.android; import cc.iqa.iquery.*; public class SoloProperty implements IProperty { public SoloProperty(String name, String value) { this.name = name; this.value = value; } private String name; public String getName() { return name; } private String value; public String getValue() { return value; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final IProperty other = (IProperty) obj; if (this.name != other.getName() && (this.name == null || !this.name.equals(other.getName()))) { return false; } return !(this.value != other.getValue() && (this.value == null || !this.value .equals(other.getValue()))); } @Override public int hashCode() { int hash = 5; hash = 61 * hash + (this.name != null ? this.name.hashCode() : 0); hash = 61 * hash + (this.value != null ? this.value.hashCode() : 0); return hash; } }