代码改变世界

开源手机自动化测试框架iQuery入门教程(一)

2012-09-07 13:12  知平软件  阅读(2790)  评论(1编辑  收藏  举报

上次介绍iQuery之后,已经有些朋友在开始在手机自动化测试程序里试用iQuery了,由于之前的介绍文档比较含糊,先搁置扩展iQuery使其支持多种编程语言的系列文章,补充一下iQuery的入门教程,之前写的介绍性文章可以参考:
1.    开源类库iQuery Android版使用说明
2.    类jQuery selector的控件查询iQuery开源类库介绍

iQuery是一个开源的自动化测试框架项目,有兴趣的朋友可以在这里下载:https://github.com/vowei/iQuery/downloads

源码位置:https://github.com/vowei/iQuery

iQuery for iOS简易教程

 

当前iQuery for iOS还只支持instrument里的自动化测试,在本教程里,我们使用下面的程序演示使用iQuery for iOS编写自动化测试用例的方法:

可用在iphone模拟器里的程序:https://github.com/vowei/iQuery/tree/master/iOS/targetApp
源代码:https://github.com/vowei/iQuery/tree/master/iOS/targetApp/Gestures

该教程里演示的自动化测试用例需要下载iQuery for iOS类库:

https://github.com/downloads/vowei/iQuery/iQuery%20for%20iOS.zip

依照下面的步骤创建并运行iOS程序的自动化测试用例:
1.    在Mac机上启动 instruments:

ios-launch-instruments

2.    在“Choose a Template for The Trace Document”对话框里选择“Automation”。

ios-select-automation

3.    点击instruments上的“Choose Target”下拉框 。

ios-choose-target
4.    将我们的演示用程序Gestures.app设置为目标程序。

ios-choose-target-gestures
5.    Gestures.app要求iOS 5的模拟器,需要显式设置。

ios-set-instruments-option
6.    然后点击“Create …”按钮创建一个新的自动化测试用例脚本。

ios-instruments-create-script
7.    并在新脚本对话框中输入如下代码。
ios-instruments-iquery-statements
8.    最后,点击底下的“run”按钮来执行自动化测试用例脚本,然后在结果对话框里查看结果。

ios-run-instruments-test

iQuery for Android简易教程


这个教程是基于Android Instrumentation测试技术写的,但iQuery for Android同时还支持从View Server的输出中查询控件,关于如何从View Server中获取Android Activity层次信息的方法,后面会写文章讲到:

演示用的Android程序在:https://github.com/vowei/iQuery/tree/master/java/Test/multiplatformdemoproject
其源码在:https://github.com/vowei/iQuery/tree/master/java/sample

下面是使用iQuery编写Android Instrument UI测试的方法:
1.    打开eclipse并且创建一个新的Android工程(Create Android Project),命名为tutorial.

android-new-project-name
2.    iQuery支持android 2.2以上的版本,在这个教程里,我们选择Android 2.2平台。

android-new-project-target
3.    因为创建的是测试工程,因此不需要添加任何的Activity。

android-new-project-info
4.    工程创建完毕之后,更新新建工程的manifest.xml文件,添加一个新的instrumentation块,来指定要测试的应用的包名。

android-test-manifest-file
5.    在eclipse里右键单击tutorial工程,依次点击“Build Path”-> “Configure Build Path”。

android-config-build-path-menu
6.    在“Properties for tutorial”对话框里,点击“Add External JARs”按钮。

android-config-build-path-add-external-jars
7.    添加对iquery-core.jar和iquery-instrumentation.jar的引用,由于iQuery是基于antlr的,还需要添加对antlr-runtime-3.4.jar的引用。这篇教程里,我们使用robotium来抓取和修改UI控件的信息,还需要添加对robotium-solo-3.1.jar的引用。最后,Build path应该如下所示:

android-config-build-path-jars
8.    创建一个新的测试用例文件,并输入下面的代码。

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


9.    最后,将演示用的Android程序安装到模拟器或者手机上,并运行测试用例。

android-run-tutorial-tests

完整的测试用例文件可以从这里下载:
https://github.com/vowei/iQuery/blob/master/java/sample/src/cc/iqa/studio/demo/test/DemoOnlyTest.java