Teach you uiautomator_android the first demonstration of automated testing

Source: Internet
Author: User

Teach you uiautomator_android. The first example of automated testing

Android's automated testing program, has been a long time.

Google offers an automated solution after sdk4.0 Uiautomator:

Advantages: Can be used across the application; it's biological.

Disadvantage: Must sdk4.0 above version; To achieve good, it is best to have development cooperation; Java projects are compiled into jars that require push to the phone to run, which means that log brute force debugging must be printed.

Later heard a group of friends Teddy said Appium and Calabash-android, turned a appium article, found:

01.jpg

Appium is based on Android Instrumentationframework and Uiautomator, which means that this tool can be used across applications. Said far, well, in order to help everyone to understand the use of appium, I would like to talk about how to use Uiautomator.

The official homepage of Uiautomator is provided first:

Http://developer.android.com/tools/help/uiautomator/index.html

You should have ANDROID-SDK, upgrade to more than 4.0, go to directory android-sdk\tools, you will see two files:

Traceview.bat and Uiautomatorviewer.bat, these two files remind you of Monkeyrunner, yes, Traceview.bat corresponds to Hierarchyviewer.bat, which is used to view the UI of the program and is usually started with administrator privileges.

In addition to ANDROIDSDK, you need to get an ant to compile the program and configure the environment variables:

ant_home=c:\program\apache-ant-1.8.2

Okay, now create a Java project with eclipse, yes, you're right, Java project is not Android project, add Reference:

02.jpg

The content in Project.Properties is:

# Project Target.

Target=android-16

The android-16 here need to be consistent with the previous Android.jar and Uiautomator.jar positions.

And then what? Write code, build a class, get, send a reference to everyone:

Package com.uia.example.my;

Import org.apache.http.util.EncodingUtils;

Import Android.graphics.Bitmap;

Import android.graphics.BitmapFactory;

Import Android.graphics.Rect;

import android.os.Environment;

import Com.android.uiautomator.core.UiObject;

import com.android.uiautomator.core.UiObjectNotFoundException;

import com.android.uiautomator.core.UiScrollable;

Import Com.android.uiautomator.core. Uiselector;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;

import java.io.File;

import Java.io.FileOutputStream;

import java.io.IOException;

Public class TAppWorkAssistV1 extendsuiautomatortestcase {

Public Stringslog;

Public Filefout =null;

Public fileoutputstreamoutstream=null;

Public void write2file (String filename,string sData)

{

String slog= "";

Initializing log files

if (Environment. getexternalstoragestate (). Equals (Environment. media_mounted)) {

SLOG = environment. getexternalstoragedirectory (). GetAbsolutePath ();

Try {

Fout = new File (slog,filename);

OutStream = new fileoutputstream (Fout,true); The true here is append

Sdata=sdata + "\ n";

Outstream.write (Sdata.getbytes ());

Outstream.flush ();

Outstream.close ();

fout=null;

}

Catch (Exception e) {

E.printstacktrace ();

}

}Else{

System. out. println ("The phone does not have an SD card");

}

}

Public void Testdemo () throws uiobjectnotfoundexception {

1. Launch app

Getuidevice (). Presshome ();

UiObject Allappsbutton = new UiObject (new Uiselector (). Description ("Apps"));

Allappsbutton.clickandwaitfornewwindow ();

UiObject appstab = new UiObject (new uiselector (). Text ("Apps");

Appstab.click ();

Uiscrollable appviews = new uiscrollable (new uiselector (). Scrollable (true));

UiObject Settingsapp = Appviews.getchildbytext (new uiselector (). ClassName (Android.widget.TextView. Class. GetName ()), "Efilm");

Settingsapp.clickandwaitfornewwindow ();

2. Enter the main interface

System. out. println ("Into main view");

System. out. println (Getuidevice (). Waitforwindowupdate ("Com.eshore.efilm", 60000));

System. out. println ("intoed main View");

UiObject TV1 = new UiObject (new uiselector (). Text ("Cinema"));

Tv1.click ();

3. Click Cinema

UiObject oyy= New UiObject (new uiselector () description ("Cinema_row"));

System. out. println ("Wait Yingyuan come Out");

Oyy.waitforexists (60000);

System. out. println ("Yingyuan come Out");

Oyy.clickandwaitfornewwindow ();

System. out. println ("click Yingyuan");

4. Matches

UiObject occ= New UiObject (new uiselector () description ("LinearLayout10"));

System. out. println ("Wait changci come Out");

Oyy.waitforexists (60000);

System. out. println ("Changci come Out");

Occ.clickandwaitfornewwindow ();

System. out. println ("click Changci");

5. Seating

UiObject oseat= New UiObject (new Uiselector (). Description ("Cinema_shows_list_item"). Index (0). Childselector (new Uiselector (). Description ("LinearLayout10"));

System. out. println ("Wait seat come out");

Oseat.waitforexists (5000);

int h=getuidevice (). Getdisplayheight ();

int w=getuidevice (). Getdisplaywidth ();

System. out. println ("(H/2,W/2) =" +h/2+ "," +W/2 ");

Getuidevice (). Click (H/2,W/2);

System.out.println ("Seat Count:" +string.valueof (Oseat.getchildcount ()));

System.out.println ("Seat GetText:" + oseat.gettext ());

Seat-cut Map

Process process;

Try {

Process = Runtime. GetRuntime (). EXEC ("screencap/mnt/sdcard/efilmfailsnapshot01.png");

Try {

Process.waitfor ();

} catch (Interruptedexception e) {// TODO auto-generated Catch block

E.printstacktrace ();

}

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Takescreenshots ("Efilmseatsnapshot");

}

}

This example is casually written and may not be rigorous enough. Well, that's the situation. The next step is to compile the implementation, plug in the phone USB interface, and then open cmd, execute:

Find Sdkid, which is the-t parameter in Android create:

CD C + + Program\android-sdk\tools

Android List

After finding the value of the T parameter:

CD C + + Program\android-sdk\tools

Android Create Uitest-project-n tappworkassistv1-t 25-p C:\android Automation \tv2.0\testsetting

CD C:\android Automation \tv2.0\testsetting

Ant Build

CD C:\android Automation \tv2.0\testsetting\bin

ADB push tappworkassistv1.jar/data/local/tmp/

adb shell uiautomator runtest tappworkassistv1.jar-c com.uia.example.my. TAppWorkAssistV1

Look, there seems to be nothing special to explain.

-N TAppWorkAssistV1: class name

-P: directory where the project is located

ANT build compiles this class into a jar package: Tappworkassistv1.jar

Then push the jar package to the phone and invoke the class to execute it.

This is generally a step, but there is a very important detail, that is, if you need more worry, it is best to put the interface elements, regardless of dynamic or layout files, plus the content-description attribute, and ensure uniqueness, according to:

Uiselector:description (String desc)

Set the search criteria to match Thecontent-description property for a widget.

Then you can unify the method that only uses this one reference interface element, so you don't have to try to use other attributes to refer to it.

If you do not have the source code, it can only be based on the Traceview.bat tool to find different reference methods, if there is source code, you can refer to the following:

http://blog.csdn.net/testingba/article/details/19398445

Said back, Appium also have instrument part, can refer to the following:

http://blog.csdn.net/testingba/article/details/8565533

or refer to Ranorex's instrument code, you can understand what instrument is going on, and put a instrument jar package reference, and then set the hook.

http://blog.csdn.net/testingba/article/details/19538749

With these pieces of knowledge to do the bottom, appium how to achieve it will be more easily understood.

If there are mistakes, do not hesitate to enlighten.

Original URL:

Http://www.myexception.cn/android/1617889.html

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.