android 界面 自动化测试

测试类继承图


android 界面 自动化测试_第1张图片
Paste_Image.png

使用InstrumentationTestCase

package com.yll.yhzb.nice;

import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.widget.Button;

import com.yll.zb.test.MainActivity;
import com.yll.zb.test.R;

/**
 * Created by Administrator on 2016/10/8.
 */

public class SampleInstrumentationTest extends InstrumentationTestCase {
    private MainActivity activity;
    private Button button2;
    private Button button;

    @Override
    protected void setUp() throws Exception {
        try {
            super.setUp();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Intent intent = new Intent();
        intent.setClassName("com.yll.zb.test", com.yll.zb.test.MainActivity.class.getName());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity = (MainActivity) getInstrumentation().startActivitySync(intent);
        button = (Button) activity.findViewById(R.id.button);
        button2 = (Button) activity.findViewById(R.id.button2);
    }

    /*
       * 垃圾清理与资源回收
       * @see android.test.InstrumentationTestCase#tearDown()
       */
    @Override
    protected void tearDown() {
        activity.finish();
        try {
            super.tearDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * 活动功能测试
     */
    public void testActivity() throws Exception {
        Log.v("testActivity", "test the Activity");
        SystemClock.sleep(1500);
        getInstrumentation().runOnMainSync(new PerformClick(button));
        SystemClock.sleep(3000);
        assertEquals("Hello Android", button.getText().toString());
    }

    private class PerformClick implements Runnable {
        Button button;

        public PerformClick(Button button) {
            this.button = button;
        }

        @Override
        public void run() {
            button.performClick();
        }
    }
}

使用ActivityInstrumentationTestCase2

package com.yll.yhzb.nice;

/**
 * Created by Administrator on 2016/10/9.
 */

import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;

import com.yll.zb.test.MainActivity;
import com.yll.zb.test.R;

public class ExampleTest extends ActivityInstrumentationTestCase2 {


    @SuppressWarnings("unchecked")
    public ExampleTest() throws ClassNotFoundException {
        super(MainActivity.class);
    }

    public void setUp() throws Exception {
        super.setUp();
        setActivityInitialTouchMode(false);
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    public void testRun() {
        final Button viewById = (Button) getActivity().findViewById(R.id.button);
        SystemClock.sleep(2000);
        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    viewById.performClick();
                }
            });
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        SystemClock.sleep(2000);
        assertEquals("ff",viewById.getText());
    }
}

使用robotium

https://github.com/RobotiumTech/robotium
支持多页面
as导入

android 界面 自动化测试_第2张图片
Paste_Image.png
package com.yll.yhzb.nice;

/**
 * Created by Administrator on 2016/10/9.
 */
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;

import com.robotium.solo.Solo;
import com.yll.zb.test.MainActivity;

@SuppressWarnings("rawtypes")
public class ExampleRobotiumTest extends ActivityInstrumentationTestCase2 {
    private Solo solo;

    @SuppressWarnings("unchecked")
    public ExampleRobotiumTest() throws ClassNotFoundException {
        super(MainActivity.class);
    }

    public void setUp() throws Exception {
        super.setUp();
        solo = new Solo(getInstrumentation(),getActivity());
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
        super.tearDown();
    }

    public void testRun() {
        Button bt=solo.getButton(0);
        solo.clickOnButton(bt.getText().toString());

    }
}

你可能感兴趣的:(android 界面 自动化测试)