Páginas

sábado, 25 de enero de 2014

Using UI Automator Viewer and uiautomator for UI Testing on Android

Hi everyone !

Today I want to talk about UI Testing on Android. The project I am developing, I am using two tools very usefull for this job: UI Automator Viewer and uiautomator (from Android SDK). I want to show you how to start with on Windows ( if you are using GNU/Linux like Ubuntu, maybe some steps are different but it is the same deeply).

STEP 1: CHECK YOUR UI COMPONENTS.
Firstable, we need to check our visual component with UI Automator Viewer to take a look how our component is referenced. You can find it in "tools" directory on your Android SDK.
    Figure 1: UI Automator Viewer in action
You have to write (in a paper apart, for example) the name of the UI components that you can use to test. Take details from XML fields like "resource-id" or "content-desc" (Figure 1). In the image, you don't see anything in that fields but you can write what you want on your XML layouts from your application. Later we can see how to reference it in the test.

STEP 2: CREATE THE PROJECT AND ADD DEPENDENCIES.

Now, you have to make a new Java Project on your Android IDE (I use ADT Bundle Eclipse but I think the idea is the same). You need to add 3 files on your project:
  • uiautomator.jar and android.jar: Go to proyect properties -> Java Build Path -> Add External jar. You can find them on "platform/< Android version you are compiling your project>" directory in your Android SDK.
  • JUnit (library): Go to proyect properties -> Java Build Path -> Add Library -> JUnit (3 - I don't know if it is compatible with 4 version yet)

STEP 3: WRITE THE TEST CODE.
So now, you have to write your test cases that you want to test. Here you have a general view about the source.
 
public class TestCasesName  extends UiAutomatorTestCase
{

	protected void setUp() throws Exception {
		super.setUp();
                // for precondition statements
	}
 
	protected void tearDown() throws Exception {
		super.tearDown();
                // for postconditions statements
	}
	
	public void testnumberone () throws UiObjectNotFoundException
	{
		 // write your test case 1 here
	}
	
	public void testnumbertwo () throws UiObjectNotFoundException
	{
		 // write your test case 2 here
	}
	
	// and all of the test that you want to write.
}

STEP 4: CHECK YOUR ANDROID IDENTIFIER
Identify your android version in android list. For this, you have to go to "tools" directory on Android SDK and write in the terminal:
android list
Figure 2: android list
STEP 5:  GENERATE THE BUILD CONFIGURATION FILES.

You can check that in my list, I have android-18 for number 1, and android-19 for number 2. You have to choose Android version you are compiling. Now, write the next in the same directory:
android create uitest-project -n <name_test_project> -t <identifier_android_version> -p <path_test_project_folder> 
For example:
android create uitest-project -n HelloTesting -t 1 -p c:\Users\danigonlinea\Documents\Android\Workspace\HelloTesting

STEP 6: COMPILE YOUR PROJECT WITH ANT

Next you have to do is create (or updated if it had been created automatically) a file name build.xml. To do this, go to your test project folder and write:
ant build

NOTE: if you have not installed Apache Ant in your system, you have to. Here a link to do it: Install Ant Windows 7.


STEP 7: PUT THE .JAR FILE ONTO THE DEVICE

Now, you have generated a .jar file on your "bin" directory on your test project. Check it out. The next step that we have to do is to move that file into your emulator/smartphone.
adb push <path_to_output_jar> /data/local/tmp/

NOTE: In my case, it was useless because actually I am using the fantastic "emulator" for Android testing, Genymotion. I had a problem: I was not be able to have permission to write in this folder. Genymotion runs on VirtualBox so what I did was to create a folder and share it, so whatever I put on this folder, it could be accessible on Genymotion.


VirtualBox: Shared Folders
STEP 8: RUN IT!

We have the .jar inside the emulator so, let's run it !!. For this propose, you have to go "platform-tools" directory in the Android SDK and write:

adb shell uiautomator runtest <path_to_jar_file> -c <package_plus_name_test_name_to_run>

For example:

adb shell uiautomator runtest /mnt/shared/SharedFilesGenymotion/ikeComercialTesting.jar -c android.testing.testnumberone

That's all!. In fact, the .jar will execute on your emulator and you can see all the steps that you describe on your test code automatically, like a video. Finally, you can check the result of the test on the terminal.

Happy Testing !



No hay comentarios:

Publicar un comentario