zoukankan      html  css  js  c++  java
  • Android的测试

    一个简单的Android界面

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sam.a.MainActivity"
    android:orientation="vertical">
    <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="25dp"
    android:id="@+id/textView"/>
    <EditText android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter your name here"
    android:id="@+id/editText"/>
    <Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="SAY HELLO"
    android:id="@+id/button"
    android:layout_gravity="center_horizontal" />
    </LinearLayout>

    Java代码
    package com.example.sam.a;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    public class MainActivity extends AppCompatActivity {
    @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    public void sayHello(View view){
    TextView textView=(TextView)findViewById(R.id.textView);
    EditText editText=(EditText)findViewById(R.id.editText);
    extView.setText("Hello,"+editText.getText().toString()+"!");
    }
    }


    测试代码

    public class MainActivityInstrumentation {  
       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());
            onView(withId(
       R.id.btn)).perform(click());      
       String expectedText="Hello,"+STRING_TO_BE_TYPED+"!";    
         onView(withId(
       R.id.textview)).check(matches(withText(expectedText)));   
       }
    }
    
    
    
    
  • 相关阅读:
    Fiddler: Creation of interception certificate failed.
    ip地址检查正则表达式 兼容ipv4,ipv6
    母版页与子页的启动过程
    erlang 读取confg文件异常 could not start kernel pid error in config file
    转义字符 显示形式 转换成 实际形式 \\n to \n
    How to use epoll? A complete example in C
    Lex & Flex 词法分析器实践(未完,持续更新)
    我理解的爱情———柳智宇 (转载)
    Learning by doing 系列文章概述
    锁与RCU数据共享机制
  • 原文地址:https://www.cnblogs.com/34nxy/p/6581158.html
Copyright © 2011-2022 走看看