2020-9-27Androidstudio学习笔记-UI测试

UI测试
1.输入名字显示hellow,xxx的测试
(1)设置build.gradle(Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.cxq.myapplication"
        minSdkVersion 18
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    //ADD THESE LINES:
    packagingOptions {
        exclude 'LICENSE.txt'
    }
    }

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    implementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'

}

(2)图形化界面的设计

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

        <TextView
            android:id="@+id/textView"
            android:text="@string/hello_world" android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:hint="Enter your name here"
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Say hello!"
            android:layout_below="@+id/editText"
            android:onClick="sayHello"
            tools:ignore="OnClick" />
    </RelativeLayout>

(3)主程序实现:

package com.cxq.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sayHello(View v){
        TextView textView = (TextView) findViewById(R.id.textView);
        EditText editText = (EditText) findViewById(R.id.editText);
        textView.setText("Hello, " + editText.getText().toString() + "!");

    }

}

(4)UI测试:

package com.cxq.myapplication;


import androidx.test.ext.junit.runners.AndroidJUnit4;
        import androidx.test.filters.LargeTest;
        import androidx.test.rule.ActivityTestRule;

        import org.junit.Rule;
        import org.junit.Test;
        import org.junit.runner.RunWith;

        import static androidx.test.espresso.Espresso.onView;
        import static androidx.test.espresso.action.ViewActions.click;
        import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
        import static androidx.test.espresso.action.ViewActions.typeText;
        import static androidx.test.espresso.assertion.ViewAssertions.matches;
        import static androidx.test.espresso.matcher.ViewMatchers.withId;
        import static androidx.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
@LargeTest

public class MainActivityInstrumentationTest {
    private static final String STRING_TO_BE_TYPED = "Peter";

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    @Test
    public void sayHello() {
        onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); //line 1

        onView(withText("Say hello!")).perform(click()); //line 2

        String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!";
        onView(withId(R.id.textView)).check(matches(withText(expectedText))); //line 3
    }
}

  1. 电话簿添加UI测试
package com.cxq.myapplication;

import android.app.Instrumentation;

import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;

import org.junit.Before;
import org.junit.Test;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

public class PhoneTester extends Instrumentation {
    @Before
    public void setUp() throws Exception {
        UiDevice device;
        device = UiDevice.getInstance(getInstrumentation());
        device.pressHome();

        //wait for Apps icon to shoe ip on the screen
        device.wait(Until.hasObject(By.desc("Apps list")),3000);
        UiObject2 appsButton = device.findObject(By.desc("Apps list"));
        appsButton.click();

        device.wait(Until.hasObject(By.desc("Phone")),3000);
        UiObject2 appButton = device.findObject(By.desc("Phone"));
        appButton.click();




    }

    @Test
    public void testAdd() throws Exception {
        UiDevice device;
        device = UiDevice.getInstance(getInstrumentation());

        device.wait(Until.hasObject(By.desc("Contacts tab.")),3000);
        UiObject2 appButton1 = device.findObject(By.desc("Contacts tab."));
        appButton1.click();

        device.wait(Until.hasObject(By.text("Create new contact")),3000);
        UiObject2 appButton2 = device.findObject(By.text("Create new contact"));
        appButton2.click();

        device.wait(Until.hasObject(By.text("First name")),3000);
        UiObject2 appButton3 = device.findObject(By.text("First name"));
        appButton3.setText("Peter1");

        device.wait(Until.hasObject(By.text("Last name")),3000);
        UiObject2 appButton4 = device.findObject(By.text("Last name"));
        appButton4.click();
        appButton4.setText("Ace1");
        device.wait(Until.hasObject(By.text("Email")),3000);

        device.pressBack();
        UiObject2 appButton5 = device.findObject(By.text("Phone"));
        appButton5.click();
        appButton5.setText("12345678");

        device.pressBack();
        UiObject2 appButton6 = device.findObject((By.text("Email")));
        appButton6.click();
        appButton6.setText("peter.ace1@123.com+");


        device.wait(Until.hasObject(By.text("SAVE")),3000);
        UiObject2 appButton7 = device.findObject((By.text("SAVE")));
        appButton7.click();
    }


}

注意事项:
1.依赖包的版本冲突
2.能用import改错,绝不用建立class
3.device.wait要给等待时间
4.device.pressBack 要先把输入法返回才能进行下一项
5.查log用System.out.println()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值