iQuery Android tutorial

本文介绍如何使用 iQuery 进行 Android UI 自动化测试。通过具体步骤指导设置 Eclipse 项目、配置依赖库及编写测试代码。利用 iQuery 和 Robotium 框架查询并操作 Android 应用中的 UI 元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值