"Reprint" robotium-only apk file test

Source: Internet
Author: User

Summarize the contents of the PPT and explain the problem:

The basic steps out, with the Notepad program, directly install the APK, as is not know the source code is good!

Just use your own phone on the line!!

First, re-sign:

Steps:

1, directly run Re-sign.jar to sign

Precautions:

(1) Direct search on the website:

(2) for Android 4.4.2 (API19) as if the signature has a problem, from the image to be able to sign the successful, go to the program run as Android JUnit test will be prompted with similar error:

Test run failed:permission denial:starting instrumentation componentinfo{com.filemanager.test/ Com.zutubi.android.junitreport.JUnitReportTestRunner} from pid=6072, uid=6072 not allowed because package Com.filemanager.test does not has a signature matching the target com.estrongs.android.pop--for this issue, it can only be signed by the command line, the following 2 Will be talked about

For Anroid 4.2 (API17) and Android 4.3 (API18) can be successfully signed, the official example (NOTEPAD.APK) For example, its own example is the source, we re-signed APK, in accordance with the example of no source code to explain.

OK, next directly double-click Run Re-sign.jar, the following prompt appears: (the first possible problem)

The reason is: Run Re-sign.jar need to configure Android_home environment variables, should be the program write dead need to find the file directory from this environment variable, and then call its following Zipalign.exe program

Solution:

Configure the environment: direct My Computer right-system Properties-advanced property settings-environment variables-new environment variable name: Android_home,value Yes: "D:\Program files\adt-bundle-windows-x86-20130717 \sdk "(This is my SDK path, the personal installation path is different, you need to find out)

Then double-click on the Re-sign.jar, you will no longer appear the above hint, and then drag the apk directly into the Re-sign.jar run interface, there will be a prompt to save the generated re-signed file

Just click to save ...

Here the re-signature is actually also debug_keystore this signature, so the signature with a debug, this is the Guess ha ....

Then Re-sign.jar will pop the window to prompt for the package name and mainactivity of the re-signed APK file:

2. Command line signature

View re-signed PackageName and mainactivity with the AAPT badging command

Principle: Why do robotium need to re-sign the program?

(1) What is a signature? Why does the Android program need to sign

Signature is to mark themselves, to mark their own programs, let others see the signature when you know this is related to you (whether the program, or other signatures are so useful)

Android Program Signature is Android system requirements, Android system requires that each application must be digitally signed to install into the application system, if not digitally signed, there is no way to install in the application system! Android uses digital signatures to identify the author of an application and establish a trust relationship between applications, and Android is done by the application's author, without the need for authoritative authentication, just to make the application package self-certified.

(2) How do I sign a signature? (debug mode and release mode)

Debug mode: In debug mode, the program can run on the phone and emulator without special signature, ADT automatically uses the debug key to sign the application (debug key full name: Dedug.keystore, specific location: C:\Documents and Settings\xxx\.android\debug.keystore)

Their own use of different PC generated Debug.keystore, found that fundamentally different, so if the replacement of the machine, it will lead to different signatures, so the following will say, Robotium this program, if the testapk is on the other machine has been modified, you must re-sign the original test program, otherwise it will be Current signature mismatch problem

(see: The Notepad is signed on a machine as the program to be tested, write testcase on the B machine, run as Android Junit Test directly in Eclipse, will there be crash problem, or prompt signature mismatch? , but still need to verify!!! )

The release mode signature has two ways:

1) Eclipse+adt mode

This is the simplest and most common way to do this, in the following steps:

A:project---Cancel build automatically

B:project-Clean

Build C:project

D: Right-click on the project->android Tools->export signed application Package

The first run can use the Create new KeyStore, which can be signed later using the user existing KeyStore. Follow the instructions step by step, and it's easy to package your signature successfully.

Note: Please keep the generated signature files, do not lose, to ensure that the application release can be reused, in order to avoid the application of seamless continuous upgrade.

2) command line mode: See (Reprint article: "Reprint" about the signature--android application signature details http://www.cnblogs.com/keke-xiaoxiami/articles/4299619.html)

(3) Why is the test procedure to be re-signed?

Benefits of Signing:

(1) Facilitate the program upgrade.

When the new program and the old version of the program's digital certificate, the Android system will consider the two programs are different versions of the same program. If the new program and the old version of the digital certificate is not the same, then the Android system think they are different programs, and create a conflict, will require the newly-created program to change the package name.

(2) facilitates the modular design and development of the program. Android allows programs with the same digital signature to run in one process, and the Android program treats them as the same program. So developers can develop their own programs into modules, and users only need to download the appropriate modules when needed. (3) Data and code can be shared across multiple programs through permissions (permission). Android provides a digital certificate-based permission-granting mechanism that the application can share with other programs or data to programs that have the same digital credentials as themselves. If the ProtectionLevel of a permission (permission) is signature, this permission can only be granted to programs that have the same digital certificate as the package in which the permission resides. Why do Robotium need to re-sign the program under test? based on the benefits of the signature above, you know that you can share data and code after re-signing, and you can run the program with TESTAPK in the same process so that the testapk can interact directly with the program under test. The specific reasons are described below:

Robotium is to do the instrumentation two times package, instrumentation requires the program to be tested and testapk run in the same process, and need to do the data and resources to operate, then you must ensure that the signature is consistent, According to the advantages of the signature described above, the signature is a prerequisite and a necessary condition, otherwise it will not be able to use the TESTAPK treatment program and the UI test.

Android provides a custom class for running test cases, called Instrumentationtestrunner. This class control application is in the test environment, running the test program and the main program in the same process, and outputting the test results to the appropriate place. The key to Intrumentationtestrunner's ability to control the entire test environment at run time is the use of instrumentation. Note that if your test class does not use instrumentation, you can also use this testrunner.

When you run a test program, you first run a system tool called activity Manager. Activity Manager uses the instrumentation framework to start and control Testrunner, which in turn uses intrumentation to close any instances of the main program, Then start the test program and the main program (in the same process). This ensures direct interaction between the test program and the main program. After compiling the testcase with eclipse, you can use the System Tools activity Manager to run the test program after the test project is compiled. You provide the activity manager with the Testrunner name (typically Instrumentationtestrunner, specified in the program) The name includes the package name of the program being tested and the name of the Testrunner (this is the same in Robotium, and it needs to be provided to find the program you need to test). The Activity Manager loads and starts your test program, kills any instances of the main program, and then loads the main program in the same process of the test program, then passes the first test case of the test program. At this point, Testrunner will take over these test cases and run each of the test methods inside until all of the methods are run over. If you use eclipse, the results will be displayed in JUnit's panel. If you use the command line, the output will be on the stdout.

If you do not re-sign, we use the official original APK package and our own later generated testapk package installed, with the TESTAPK package to test the original APK package, you will receive the following error message:

Second, install the re-signed APK file to the Android phone

Install the APK that you want to test directly with ADB (drag it into cmd), then enter, the installation is successful

Third, robotium the testcase of the writing configuration (under Eclipse)

1. Create Android Test Project

(1) File-new-project-android-android Test Project (2) Enter the project name-next, select this Project-finish

2, create Anroid testcase (including the use of Uiatumatorviewer, this has to be at 17 and above only then ...) )

Use Uiautomatorviewer to get the layout of the program to be tested, you can add the path of the tool to the environment variable, or enter the directory, run with cmd, directly enter the Uiautomatorviewer can run the tool, which is already packaged tools, Can be used directly with ...

The process for creating the TestCase is as follows: (1) project name-src-package name-right-click the New-junit test Case (2) in the Name column, enter the names of the Test case, such as Testnotepad (3) Superclass column, Select Android.test.ActivityInstrumentationTestCase2, then select Setup,teardown,constructor method, click Finish to

3, Import Robotium-jar package (need attention and need to configure the project)

By creating the Lib folder, the direct copy file needs to be configured in the build path .... The steps are the same as the steps to add the Report-jar package below

(1) Project name-new-folder-lib (2) Paste the previously downloaded Robotium-solo-5.2.1.jar copy into the Lib directory (3) Project name-Right click-build path-configure Build Path-libraries-add External JARs

(4) Click on the right of order and Export, select the Robotium-solo-5.2.1.jar option, click OK

4. Modify the Androidmanifest.xml file

The modification of this configuration file mainly includes adding <Instrumentation> nodes

<instrumentation

Android:name= "Android.test.InstrumentationTestRunner"

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

Targetpackage is the same as the package name obtained in Re-sign.jar, which refers to the package name of the program under test.

5. Write test code

Four, run case:

You can run it all, or you can run it all, either in eclipse or directly from the command line under CMD:

(1) You can use the ADT tool to operate directly from Eclipse: Run all use cases-if you use Testsuite to process the use case in a certain order, you can add all the use cases you need in the class, and then on the class file, right-click Run as Android Junit Test, run a single use case--You can right-click on the class file of a test and run as Android Junit Test, and you'll be able to do just that class you want to run

(2) direct command line operation under CMD:

ADB shell am instrument-w com.example.test/android.test.instrumentationtestrunner

Parameter description:

-W is a must-write, otherwise you will not see the test results ...

com.example.test--is the testapk package name, Android.test.InstrumentationTestRunner is configured in Androidmanifest.xml

<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"/> Android:name the same, that is, through what to start this test

If the Report-jar package is imported into the program, the content configured in Androidmanifest.xml is modified as follows:

<instrumentation
Android:name= "Com.zutubi.android.junitreport.JUnitReportTestRunner"/>

You need to modify this cmd command as: adb shell am instrument-w com.example.test/com.zutubi.android.junitreport.junitreporttestrunner

V. Configure the Report-jar package to generate the desired XML test run results directly on the phone

Import this jar package, you can not rely on Eclipse, the program started up, you can close eclipse, waiting for the program to run, you can get the results of the program in a specific location, the specific import steps are shown below:

There are ready-made jar packages to help with this operation, the name is android-junit-report-1.5.8.jar

Download official website:https://github.com/jsankey/android-junit-report https://github.com/jsankey/ Android-junit-report/downloads

After the download is complete, you need to add the jar to the Lib folder of my test project and add it to the build-path

The process is as follows:

1, set up an Android test project, import Robotium package, set up the test case.

2, in the test project lib directory, copy the Android-junit-report-1.5.8.jar package, you can directly drag the jar package into the project Lib file, release the mouse, and then indicate whether you want to copy files, click OK

3. Right- Click the jar Package--build Path--ADD tobuild path (be sure to tick the jar under libraries and order and export to select OK )

4, in the test project androidmanifest.xml file, modify the contents of the <instrumentation> field,

The previous content of the modification is: Targetpackage indicates the package name of the program under test

<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"

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

The modified content is:

<instrumentation
Android:name= "Com.zutubi.android.junitreport.JUnitReportTestRunner"

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

5. Right-click Test Project-Run as--run Configuration

after opening the Run Configuration interface, select your own project, the right of the Test below the instrumentation runners, select as before Androidmanifest.xml in the configuration, then apply, and confirm that you can

6, running the program run as--Android Junit Test, run case, and then in the /data/data/com.xxx.xxx/files directory under the c10> generates a file junit-report.xml:

you can then use ADB pull to copy the files on the phone to the computer side:

ADB pull/data/data/xxx.xxx.xxx/files/junit-report.xml d:\

Parameter description: Xxx.xxx.xxx is Targetpackget, that is, the package name of the program to be tested, this XML test report generation location is in the files directory of this package, note: Not under the TESTAPK package name

D:\ If the parameter is not added, the file is pull to the current directory , if you want to locate a specific location, such as D:\

7, after can log information:

Here you can see which testsuite is running in which testcase, and then run the failure, you will be the details of the failure message

If the operation succeeds, there is no <failure message> node

Vi. problems that may be encountered throughout the process:

1, ClassNotFound problem (Jar package import problem, libraries not included in, mainactivity write wrong ... )

2, Crash problem (do not install the test package, can not find the package name when it should also crash)

Golden Sun Test

Sina Weibo: Golden Sun Woody

Website address

1, Baidu Search: Golden Sunshine test

2. Official website: www.goldensunshine.cc

Public number

"Reprint" robotium-only apk file test

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.