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:

  1. Launch eclipse and create a new android project, set name to tutorial:create new android test project

  2. iQuery support android-2.2 and above, in this case, we choose android 2.2 platform:choose android platform

  3. Because we are creating an android test project, no need to add any activities:set android info

  4. Update manifest.xml file of newly created android project, specifying target app's package name by adding a new instrumentation block:update manifest file

  5. Right click tutorial project in eclipse, and select "Build Path" -> "Configure Build Path":config build path menu

  6. Click "Add External JARs" button on "Properties for tutorial" dialog:add external jars

  7. 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:tutorial build path dialog

  8. 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;} }

`

  1. Finally, run the tests, you may install the sample target app before executing this tests.run test cases

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;
}
}