Android Automated Test exploration

Source: Internet
Author: User

Android Automated Test exploration

Objective

Generally speaking, after we have developed the product, it is up to the Test team or ourselves to point it out, basically without a problem and start on the line. However, over time, the functionality of a product becomes more and more powerful. At this point, in order to guarantee the quality of the product, we need to go through all the processes in each test. However, this is difficult and complex for a large product. Because our own manual testing takes a lot of time to complete the repetitive work of the main business test. In order to ensure efficiency and to address costs, we need to consider automating these processes with automated testing.

Introduction to Automated testing

What is automated testing? Automated testing is done by the computer to automate our manual actions, such as automatic click, automatically fill in the text and so on. Android also offers a powerful automated testing tool, which is uiautomator. The official introduction document is: Https://developer.android.google.cn/training/testing/ui-testing/uiautomator-testing.html#setup.

Uiautomator Introduction

You may have questions about what this automated test can accomplish. Let me give you a simple example:

Write a picture description here

This login interface has not been done, but must have seen. We usually enter the user name, password, and then log in when testing. So what can automated testing do? It can enter the user name, password, and then log in, one go. You might think that this would require an automated test? NoNoNo, I'm just here to give you an example of what it can do for you. Even the wake-up screen, input graphics verification code, etc. can be completed.

Speaking of this, there must be some students have been eager to, suppress worry, slowly.

Use of Uiautomator

First we still need to follow the official documentation, which has already provided an address to open it.

Configuration

In Android's app module, add the following dependencies:

dependencies {
...
Androidtestcompile ' com.android.support.test.uiautomator:uiautomator-v18:2.1.1 '
Auxiliary tools

Because automated testing is done automatically, it needs to find the corresponding view before it can be manipulated. However, how to specify which view to look for, Android provides assistive tool Uiautomatorviewer. I believe many students are familiar with this tool.

Write a picture description here

It is located in the <android-sdk>/tools/directory and is started by using the command line:

$ uiautomatorviewer
1
1
After booting to find the device screenshot button, Uiautomatorviewer will start to intercept the interface on the phone via ADB and start analyzing the view tree as shown in the figure. At this point, choose a space, in the lower right corner will be listed in the view of some key information. In which the frame of the picture is the basic, but also the most commonly used information. This information is used to provide Uiautomator with critical search information when doing automated testing. It will be used later.

To create a test class

We need to specialize in a class to do automated testing. If you are using Androidstudio for development, Androidstudio will automatically create good one Android test classes at the beginning of the project, as follows:

Write a picture description here

Our usual development is in the main folder to develop, usually do not notice the other two folders Androidtest and test is what to do, today you will know what they are used to do.

First of all, the class we used to do the Android test was the one that was automatically created in the Androidtest folder, and we opened it:

Write a picture description here

There are two annotations at the top of the example class Exampleinstrumentedtest:

@RunWith (Androidjunit4.class)
@SdkSuppress (minsdkversion www.yongshiyule.cn/= 18)
1
2
1
2
These two are required to be configured, and Android's automated test framework is based on the Android testing support library, It can only run on devices above Android4.3, and it needs to work with Androidjunitrunner to complete the test.

If you are creating a class yourself, be sure to add annotations to the class: @RunWith (Androidjunit4.class), and you need to set Androidjunitrunner as the default Testrunner.

Setting Androidjunitrunner as the default Testrunner requires the following configuration in the app's Build.gradle file:

Android {
Defaultconfig {
Testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner"
Well, our test preparation is done, and then we start writing the test execution process.

Test the logic process

Here is also the use of the login page to do the example, the login page is generated automatically by the Androidstudio template.

Write a picture description here

We use automated testing to complete the test function, fill in the user name, password, and then log in.

We need to start Uiautomatorviewer first:

Write a picture description here

After the start of the interface:

Write a picture description here

We click the arrow in the graph to refer to the button, the following interface appears:

Write a picture description here

In the diagram on the left, select our first edittext input box to enter the text, and we can see that the property structure of the view hierarchy and some basic properties of the view appear on the right, where the EditText index is 0, Resource-id is Com.sahadev.autotestforlogin:id/password, and which class it is: Android.widget.EditText.

Having learned the basics, we begin to understand the execution of the test code:

1. First you need to get an instance of the context object Uidevice,uidevice that can access the device is obtained by means of a static method getinstance (), which needs to pass in a instrumentation object.
2. Find UIObject through Uidevice's Findobject (), the UIObject represents the edittext we have just designated.
3. Through UIObject to simulate the user's real operation, such as filling in text, or click and other actions. Since this is edittext, our job is to fill in the text, using the UIObject settext () method.
Test Logic implementation

With the above knowledge, we will implement this login process.

1. First obtain the Uidevice object:

Mdevice = Uidevice.getinstance (Instrumentationregistry.getinstrumentation ());
1
1
2. Find the UIObject representing EditText by the specified criteria:

UiObject email = mdevice.findobject (new Uiselector (www.xucaizxyl.com). ResourceId ("Com.sahadev.autotestforlogin:id/ Email "));
1
1
3. Write the email address to EditText:

Email.settext ("[email protected]");
1
1
Well, if you do, the system will automatically fill in this email address for us. The next thing we do is continue to fill in the password and click Sign in. Let's finish the rest of the code. The complete code is as follows:

Package com.sahadev.autotestforlogin;

Import Android.support.test.www.6788878.cn/instrumentationregistry;
Import android.support.test.filters.SdkSuppress;
Import ANDROID.SUPPORT.TEST.RUNNER.ANDROIDJUNIT4;
Import Android.support.test.uiautomator.UiDevice;
Import Android.support.test.uiautomator.UiObject;
Import Android.support.test. Www.yongshiyule178.com Uiautomator. Uiselector;

Import Org.junit.Before;
Import Org.junit.runner.RunWith;


/**
* Instrumentation test, which would execute on an Android device.
*
* @see <a href= "http://d.android.com/tools/testing" >testing documentation</a>
*/
@RunWith (Androidjunit4.class)
@SdkSuppress (minsdkversion = 18)
public class Exampleinstrumentedtest {

Private Uidevice Mdevice;

@Before
public void Startmainactivityfromhomescreen (www.tips139.com/) {

Mdevice = Uidevice.getinstance (Instrumentationregistry.getinstrumentation ());

UiObject email = mdevice.findobject (new Uiselector (). ResourceId ("Com.sahadev.autotestforlogin:id/email"));
Fill in the email address
Email.settext ("[email protected]");

UiObject Password = mdevice.findobject (new Uiselector (). ResourceId ("Com.sahadev.autotestforlogin:id/password"));
Fill in the password
Password.settext ("123456");

UiObject Loginbutton = mdevice.findobject (new Uiselector (). ResourceId ("Com.sahadev.autotestforlogin:id/ Www.xinbeiyule.cn/email_sign_in_button "));
Click Sign In
Loginbutton.click ();
The basic test code is complete and the next step is how to run it. Click the Run button in the Androidstudio:

Write a picture description here

The results are as follows:

Write a picture description here

What, do you have a feeling of wanting to try it? Gradually accumulate, it is a very large project. Today's article just take everyone into the door, this automated testing tool itself is very powerful, can do a lot of work. There are more questions that can be added to the group discussion. Uiautomator's official Presentation document address: https://developer.android.google.cn/training/testing/ui-testing/uiautomator-testing.html, please click here for more details.

I built a QQ group, welcomed the students interested in learning to join. We can explore, delve into, and master the technologies we use to keep ourselves from being too outdated.

Android Automated Test exploration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.