iQuery Android tutorial
来源:互联网 发布:js实现滑动选择日期 编辑:程序博客网 时间:2024/05/29 16:30
This tutorial is based on android instrument test technology, iQuery for android also supports query controls from view server's output. Please readthis articie about getting view server's android activity hierarchy dump.
The source of sample target app can be found here:https://github.com/vowei/iQuery/tree/master/java/Test/multiplatformdemoproject
The source of this tutorial can be found:https://github.com/vowei/iQuery/tree/master/java/sample
Following below step to use iQuery in your instrument UI test:
Launch eclipse and create a new android project, set name to tutorial:
iQuery support android-2.2 and above, in this case, we choose android 2.2 platform:
Because we are creating an android test project, no need to add any activities:
Update manifest.xml file of newly created android project, specifying target app's package name by adding a new instrumentation block:
Right click tutorial project in eclipse, and select "Build Path" -> "Configure Build Path":
Click "Add External JARs" button on "Properties for tutorial" dialog:
Add dependencies to iQA.Runtime.jar, iQA.Runtime.Instrument.jar, and since iQuery is based on antlr, you need add antlr-runtime-3.4.jar. Finally, because this tutorial uses robotiumto access android UI elements, add it too. robotium-solo-3.1.jar, you build path setting should look like this:
Create a new test file and type following code:
`
package cc.iqa.studio.demo.test; import java.io.*; import java.util.*; import org.antlr.runtime.*; import junit.framework.Assert; import cc.iqa.iquery.*; import cc.iqa.iquery.android.*; import com.jayway.android.robotium.solo.*; import android.test.ActivityInstrumentationTestCase2; import android.view.*; @SuppressWarnings("rawtypes") public class DemoOnlyTest extends ActivityInstrumentationTestCase2 { private static String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "cc.iqa.studio.demo.MainActivity";private static String TARGET_PACKAGE_ID = "cc.iqa.studio.demo";private Solo _solo;@SuppressWarnings("unchecked")public DemoOnlyTest() throws Exception { super(TARGET_PACKAGE_ID, Class .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME));}public void setUp() throws Exception { this._solo = new Solo(this.getInstrumentation(), this.getActivity());}public void testSimplifiedAPI() throws Exception { List<SoloTreeNode> r1 = iQuery.query( new SoloTreeNode(_solo.getCurrentViews().get(0)), "LinearLayout >> TextView [mText = 'Down Under']"); Assert.assertEquals(2, r1.size()); }private List<ITreeNode> parseQueryAgainst(View root, String iquery) throws IOException, RecognitionException { InputStream query = new ByteArrayInputStream(iquery.getBytes("utf8")); ANTLRInputStream input = new ANTLRInputStream(query); iQueryLexer lexer = new iQueryLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); iQueryParser parser = new iQueryParser(tokens); List<ITreeNode> candidates = new ArrayList<ITreeNode>(); candidates.add(new SoloTreeNode(root)); List<ITreeNode> result = parser.query(candidates); return result;} }
`
- Finally, run the tests, you may install the sample target app before executing this tests.
The complete test file is: https://github.com/vowei/iQuery/blob/master/java/sample/src/cc/iqa/studio/demo/test/DemoOnlyTest.java
<complete test file>
package cc.iqa.studio.demo.test;import java.io.*;import java.util.*;import org.antlr.runtime.*;import junit.framework.Assert;import cc.iqa.iquery.*;import cc.iqa.iquery.android.*;import com.jayway.android.robotium.solo.*;import android.test.ActivityInstrumentationTestCase2;import android.view.*;@SuppressWarnings("rawtypes")public class DemoOnlyTest extends ActivityInstrumentationTestCase2 {private static String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "cc.iqa.studio.demo.MainActivity";private static String TARGET_PACKAGE_ID = "cc.iqa.studio.demo";private Solo _solo;@SuppressWarnings("unchecked")public DemoOnlyTest() throws Exception {super(TARGET_PACKAGE_ID, Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME));}public void setUp() throws Exception {this._solo = new Solo(this.getInstrumentation(), this.getActivity());}public void testParseElement() throws Exception {List<ITreeNode> actual = parseQueryAgainst(_solo.getCurrentViews().get(0), "LinearLayout >> TextView");Assert.assertEquals(12, actual.size());for (int i = 0; i < actual.size(); ++i) {Assert.assertTrue(actual.get(i).getName().endsWith("TextView"));}}public void testParseAttribute() throws Exception {List<ITreeNode> actual = parseQueryAgainst(_solo.getCurrentViews().get(0),"LinearLayout >> TextView [mText = 'Down Under']");Assert.assertEquals(2, actual.size());for (int i = 0; i < actual.size(); ++i) {Assert.assertTrue(actual.get(i).getName().endsWith("TextView"));Assert.assertEquals("Down Under", actual.get(i).getProperty("mText").getValue());}}public void testParseAttributeNegative() throws Exception {// remove supports to UNICODE attribute name for sake of consistent with iOS. List<ITreeNode> actual = parseQueryAgainst(_solo.getCurrentViews().get(0),"LinearLayout >> TextView [mText = 'Down Under']");Assert.assertEquals(2, actual.size());}public void testSimpleCreatingParserMethod() throws Exception {iQueryParser parser = iQuery.createParser("LinearLayout >> TextView [mText = 'Down Under']");List<ITreeNode> candidates = new ArrayList<ITreeNode>();candidates.add(new SoloTreeNode(_solo.getCurrentViews().get(0)));List<ITreeNode> result = parser.query(candidates);Assert.assertEquals(0, parser.getErrors().size());Assert.assertEquals(2, result.size());}public void testSimplifiedAPI() throws Exception {List<SoloTreeNode> r1 = iQuery.query(new SoloTreeNode(_solo.getCurrentViews().get(0)), "LinearLayout >> TextView [mText = 'Down Under']");Assert.assertEquals(2, r1.size());}private List<ITreeNode> parseQueryAgainst(View root, String iquery)throws IOException, RecognitionException {InputStream query = new ByteArrayInputStream(iquery.getBytes("utf8"));ANTLRInputStream input = new ANTLRInputStream(query);iQueryLexer lexer = new iQueryLexer(input);CommonTokenStream tokens = new CommonTokenStream(lexer);iQueryParser parser = new iQueryParser(tokens);List<ITreeNode> candidates = new ArrayList<ITreeNode>();candidates.add(new SoloTreeNode(root));List<ITreeNode> result = parser.query(candidates);return result;}}
- iQuery Android tutorial
- 开源类库iQuery Android版使用说明
- iQuery HelloWorld
- Android Tutorial Android Wifi-Direct Tutorial
- Android Notepad tutorial
- Android database tutorial
- Android Development Tutorial
- Android TabHost Tutorial
- JSON in Android - Tutorial
- Android JSON Parsing Tutorial
- Android Service Tutorial
- Android Development Tutorial
- Android Tutorial 1
- Android Library Projects - Tutorial
- Android SQLite Database Tutorial
- Android Tutorial 收藏
- Android Service - Tutorial
- Android JSON Parsing Tutorial
- 漫谈linux文件IO
- Mysql函数INSTR、LOCATE、POSITION
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
- W3 JMail 邮件组件
- 如何将Launcher项目导入到Eclipse中(其他项目的导入可以参考)
- iQuery Android tutorial
- js 新增和删除表格
- Howto: run several robinhood instances on multiple filesystems
- PHP如何获得电脑的MAC地址!
- linux日志管理应该知晓的5大命令详解
- keepalived配置文件详解
- 编写自己的sniffer(一)
- Perl threads join 返回值
- Java类学习