Instrumentation class--android Automated test learning process

Source: Internet
Author: User

Here, we need to take a look at the top, middle, and bottom three episodes of the instrumentation class video, and summarize the contents ...

Video address:

http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=881433&courseId=712011

http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=878104&courseId=712011

http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=877119&courseId=712011

First, the introduction (Interview questions)

How do I do event events in an app? (Touch, swipe and button, etc.)

For:

1. Device.touch (x, y)//monkeyrunner method

2, Solo.clickonview (String ID)//robotium method

3, Driver.findelement (by.name ("xxx")). Click ()//selenium method

4, do not use any open source or non-open-source testing framework (Monkeyrunner, Robotium, Appium, Selenium, androiddriver, uiautomator, etc. are not allowed)

At this point you need to use the basic test class provided by Google Instrumentation

Note: Robotium is based on the instrumentation two-time package, so understanding the Robotium principle is also instrumentation

Problem:

1. What is the operating principle of robotium?

A: Robotium is through encapsulation of the instrumentation injection events (Sendkeydownupsync, Sendpointersync), and instrumentation by calling Runonmainsync, Runonuithread and so on to complete the operation of the control, so that the user can directly invoke a solo.clickontext and other methods, can complete the operation, without having to write a bunch of find the control, and then click the trigger and other methods

2, why Robotium TestCase need a Setup method and a Teardown method

A: Analysis of the principle of instrumentation, instrumentationtestcase or InstrumentationTestCase2 need to have a setup and teardown method, one for the preparation, Get all the things you need, like the main activity, for example, get all the control IDs, and then use those IDs in the test method below to do the appropriate testing.

Before writing a program for a moment, forget to write Teardown method, the result of the program did not know when the end, so ran a test1 after the card there, and then although prompted test1 success, but test2 wait for a long time did not start, so this method is necessary Added this method, the 3 test method can run down.

3, Robotium Androidmanifest.xml in the configuration of the node <Instrumentation> what meaning? When the Jnuit-report.jar package is not imported, the Android:name= "Android.test.InstrumentationTestRunner" configured in instrumentation, If you use this Jnuit-report.jar package, you will need to modify the Instrumenation node configuration to Android:name= " Com.zutubi.android.junitreport.JUnitReportTestRunner "?

A: According to understanding, this configuration of the instrumentation node Anroid:name refers to the name of the Testruner used to start, only after the configuration of this, the test program can be enough to start up, This start-up process should go back to the one that was recorded in the previous robotium, running a test program that runs a system tool, activity Manager, and then activity The manager starts and controls the Testrunner through the instrumentation framework, but you need to provide the activity manager with a Testruner name, which means the android:name configured here

The configuration in this androidmanifest.xml tag of the instrumentation (the label is located outside the application label):

<instrumentation

Android:name= "Android.test.InstrumentationTestRunner"

Android:targetpackage= "Xxx.xxx.xxx"/>

ANDROID:NAME specifies Testrunner, which is the import of a namespace, a package name, Is android.test.Instrumentation, this label import, the system will know you want to apply the instrumentation test framework

Second, the Universal instrumentation class

First:what is instrumentation?

For:

Instrumentation is the base class for executing application instrumentation code, and instrumentation is on when the application is running (note: What do I need to know before the application runs?). ), instrumentation will initialize any application before it is run, and it can monitor the interaction between the system and the application.

Second:how to implement the instrumentation?

For:

Instrumentation is implemented by adding a <Instrumentation> tag to the Androidmanifext.xml, as follows:

Third:how to understand the instrumentation?

For:

Instrumentaion is more like a hook inside windows, and a special device is installed between the system and the application for monitoring.

The class used for automated testing is: Instrumetationtestcase class, (instrumentation is a test base class, Instrumentationtestcase is a test framework, So what is the relationship between the various test classes that the specific Android itself provides?

Robotium is the InsttumentationTestCase2 two times package, this instrumentationtestcase actually with InstrumenatationTestCase2 already a little difference, InstrumentationTestCase2 has already done a simplified processing of instrumentationtestcase, such as adding the public method getactivity (), to get the activity of the currently tested object directly, Instead of getting activity by Getinstrumentation (). Startactivitysync (Intent) method, as in Instrumentationtestcase.

Third, the realization of touch, keyevent and other events

1. Preparing the Android Environment (Java Environment, Eclipse, environment variable configuration)

2. Write an Android app (app to be tested)

3. Start writing Test framework code

Here this test app, since it is written by itself, then it is certainly more convenient to use the debug signature, but such words with their own written test framework of the signature is certainly consistent,

then have the source code, if need to run in a process, is not still have to re-sign ah?? If the test program and the program are placed in a project, under different packages, there is no need, otherwise the app and test project must be signed the same, in order to make asynchronous communication , just as long as the signature is different, still need to sign the same, so still need to re-signed ...

The specific code is to build a package name to create the app, then a program to be tested to build a package, a test program to build a package.

Instrumentation is not required for Android OS version, this is particularly good

DEMO1: Implement a Click action on a button

Detailed code content detailed:

1, Intent.setclassname (PackageName, MainActivity.class.getName) method

2, Startactivitysync (Intent Intent) method:

Intent is an intention, in Android development can be through Intent.setclass, then with This.startactivity (Intent Intent) can jump to the activity that you want to go

Startactivitysync This method, after launching the app, pass this intent to him, then it will return you to the current mainactivity

3, JUNIT4 's assertequals and Robotium's SearchText

The Assertequals method needs to input expected string and actual string, and then compares the two, returns whether equal, uses this to do the verification;

The Serchtext method is to find all the TextView, put it in a set, and then do the contrast (through regular expressions)

How does instrumentation correspond with Robotium??? Actually directly see INSTRUMENETATIONTESTCASE2, will be more clear, so robotium is need extends InstrumentationTestCase2. In fact, everything is the same, you need to know the package name, then know the class name, and then through the class name to Findviewbyid to get the control ID you need, and then call the method to manipulate the control (including Sendpointsync, such as injection events, Runonmainsync , Runonuithread method, etc.), and then, depending on the state of the current control, compare it to the state you expect, and you can see if you have reached the desired result. In addition, it is necessary to configure the <Instrumentation> node in Androidmanifest.xml to tell Activitymanager what Testrunner to use.

Demo2 and Demo3:

Instrumentation.sendkeydownupsync detailed

Sendpointersync method (capable of producing the response effect of the finger manipulation control)

Motionevent

One error handling: Java.lang.SecurityException:Injecting to another application requires inject_event permission

Solution: Directly in the Androidmanifest.xml configuration, if the occurrence of similar problems involving Perssion, is similar, directly in the Manifest.xml configuration can be resolved.

Iv. Instrumentation Class API Basics

1. Callactivityoncreate (activity activity, Bundle icicle)

Description: Perform Calling of an activity ' s onCreate (Bundle) method

An activity OnCreate (bundle Savedinstancestate) method, which is used to store the state of the activity, such as when activity is stop but not destory, this object is used , and then resumes back, it is necessary to use this bundle object to restore the previous state, comparing the storage of stack information like recursion

2, GetContext ()

Description: Get a reference to the context to get further view, windows, controls

3, Startactivitysync ()

Note: Start an activity synchronously, that is, this is a blocking method, must start the activity, return the results, and then the program can continue to go down

The so-called synchronization: is the issue of a request after doing nothing, has been waiting for the request to return to continue to do things;

The so-called asynchronous: is to make a request to continue to do other things, this request processing will notify you when it is done, this time you can handle this response.

4, Sendkeydownupsync (int key)

Downup Click events, through the operation of some system keys to complete. such as Keyevent.keycode_menu, keyevent.keycode_home, etc.

5. Sendpointersync (Motionevent event)

Send a specific touch event, Motionevent has the obtain method, can be used for specific down or up events, in a particular coordinate position, through this can see the test case, the program will be clicked on the selected shadow effect, The direct call to the Button.performclick () method does not have this effect.

Golden Sun Test

Sina Weibo: Golden Sun Woody

Website address

1, Baidu Search: Golden Sunshine test

2. Official website: www.goldensunshine.cc

Public number

Instrumentation class--android Automated test learning process

Related Article

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.