Skip to content
vowei edited this page Aug 28, 2012 · 3 revisions

This tutorial is based on android instrument test technology, iQuery for android also supports query controls from view server's output. Please read this 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

Clone this wiki locally