2014-07-10 13 views
5

Sto lavorando con UiAutomator, ma non sono riuscito ad estendere la mia classe con UIAutomatorTestCase, Inoltre, ho aggiunto i file jar, cioè i file jar UIAutomator e la libreria JUnit3. Sto riscontrando errori in questa classe E anche io voglio sapere come costruire i test per quell'applicazione più Come posso eseguire i test case dalla mia applicazione a livello di programmazione? se no, allora come posso eseguire questi casi di test.UIautomator Test Case Android

package com.example.test; 

import junit.framework.TestCase; 

public class UiAutomatorTestCase extends TestCase { 

    private static final String DISABLE_IME = "disable_ime"; 
    private static final String DUMMY_IME_PACKAGE = "com.android.testing.dummyime"; 
    private UiDevice mUiDevice; 
    private Bundle mParams; 
    private IAutomationSupport mAutomationSupport; 
    private boolean mShouldDisableIme = false; 

    @Override 
    protected void setUp() throws Exception { 
     super.setUp(); 
     mShouldDisableIme = "true".equals(mParams.getString(DISABLE_IME)); 
     if (mShouldDisableIme) { 
      setDummyIme(); 
     } 
    } 

    @Override 
    protected void tearDown() throws Exception { 
     if (mShouldDisableIme) { 
      restoreActiveIme(); 
     } 
     super.tearDown(); 
    } 

    /** 
    * Get current instance of {@link UiDevice}. Works similar to calling the static {@link UiDevice#getInstance()} from anywhere in the test classes. 
    */ 
    public UiDevice getUiDevice() { 
     return mUiDevice; 
    } 

    /** 
    * Get command line parameters. On the command line when passing <code>-e key value</code> pairs, the {@link Bundle} will have the key value pairs conveniently available to the 
    * tests. 
    */ 
    public Bundle getParams() { 
     return mParams; 
    } 

    /** 
    * Provides support for running tests to report interim status 
    * 
    * @return 
    */ 
    public IAutomationSupport getAutomationSupport() { 
     return mAutomationSupport; 
    } 

    /** 
    * package private 
    * 
    * @param uiDevice 
    */ 
    void setUiDevice(UiDevice uiDevice) { 
     mUiDevice = uiDevice; 
    } 

    /** 
    * package private 
    * 
    * @param params 
    */ 
    void setParams(Bundle params) { 
     mParams = params; 
    } 

    void setAutomationSupport(IAutomationSupport automationSupport) { 
     mAutomationSupport = automationSupport; 
    } 

    /** 
    * Calls {@link SystemClock#sleep(long)} to sleep 
    * 
    * @param ms 
    *   is in milliseconds. 
    */ 
    public void sleep(long ms) { 
     SystemClock.sleep(ms); 
    } 

    protected void setDummyIme() throws RemoteException { 
     IInputMethodManager im = IInputMethodManager.Stub.asInterface(ServiceManager.getService(Context.INPUT_METHOD_SERVICE)); 
     List<InputMethodInfo> infos = im.getInputMethodList(); 
     String id = null; 
     for (InputMethodInfo info : infos) { 
      if (DUMMY_IME_PACKAGE.equals(info.getComponent().getPackageName())) { 
       id = info.getId(); 
      } 
     } 
     if (id == null) { 
      throw new RuntimeException(String.format("Required testing fixture missing: IME package (%s)", DUMMY_IME_PACKAGE)); 
     } 
     im.setInputMethod(null, id); 
    } 

    protected void restoreActiveIme() throws RemoteException { 
     // TODO: figure out a way to restore active IME 
     // Currently retrieving active IME requires querying secure settings provider, which is hard 
     // to do without a Context; so the caveat here is that to make the post test device usable, 
     // the active IME needs to be manually switched. 
    } 
} 

La seguente classe è di testare la calcolatrice. ma io non knw come usarlo .. Ecco il link

package com.example.test; 

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; 

public class DemoCalTest extends UiAutomatorTestCase { 
    public void testingCalculator() throws UiObjectNotFoundException { 

     // First we testing the press of the HOME button. 

     getUiDevice().pressHome(); 

     // using the uiautomatorviewer tool we found that the button for the "applications" has 

     //the value “Apps” (screen9) 

     // so we use this property to create a UiSelector to find the button. 

     UiObject Applications = new UiObject(new UiSelector().description("Apps")); 

     // testing the click to bring up the All Apps screen. 

     Applications.clickAndWaitForNewWindow(); 

     // In the All Apps screen, the "Calculator" application is located in 

     // the Apps tab. So, we create a UiSelector to find a tab with the text 

     // label “Apps”. 

     UiObject apps = new UiObject(new UiSelector().text("Apps")); 

     // and then testing the click to this tab in order to enter the Apps tab. 

     apps.click(); 

     // All the applications are in a scrollable list 

     // so first we need to get a reference to that list 

     UiScrollable ListOfapplications = new UiScrollable(new UiSelector().scrollable(true)); 

     // and then trying to find the application  

     // with the name Calculator 

     UiObject Calculator = ListOfapplications.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "Calculator"); 

     Calculator.clickAndWaitForNewWindow(); 

     // now the Calculator app is open 

     // so we can test the press of button "7" using the ID "com.android.calculator2:id/digit7" 

     //we found by using uiautomatorviewer 

     UiObject seven = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit7")); 

     seven.click(); 

     // now we test the press of button "+" 

     UiObject plus = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/plus")); 

     plus.click(); 

     // and then the press of button "1" 

     UiObject one = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit1")); 

     one.click(); 

     // we test the press of button "=" 

     UiObject result = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/equal")); 

     result.click(); 

     //and finally we test the press of "Back" button 

     getUiDevice().pressBack(); 

    } 
} 

I googled molto, ma potrebbe trovare alcuna spiegazione al riguardo. Qualcuno può darmi una mano. Sono nuovo di esso Grazie

risposta

3

In questo modo si dovrebbe estendere la classe:

public class <yourClassName> extends UiAutomatorTestCase { 

Abbiamo anche bisogno di importare uiautomator.jar che di solito risiede in ~/sdk/platforms/android-xx/

si può cacciare iniziare da Link1 e Link2

+0

im in grado di estendere la classe con UiAutomatorTestCase – Namy

+0

dovrebbe ho bisogno di aggiungere qualsiasi lib, tranne i file jar UIAutomator e JUnit3 lib ?? – Namy

+0

Anvesh Yalamarthy: qualche lib mi manca ??? – Namy

0

Poiché Gradle è stato introdotto come nuovo strumento di creazione, non è necessario includere alcun contenitore. Basta aggiungere

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' 

come una dipendenza al file build.gradle.

Ad esempio:

dependencies { 
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') { 
     exclude module: 'support-annotations' 
    } 

    androidTestCompile 'com.android.support:support-annotations:24.2.1' 
    androidTestCompile 'com.android.support.test:rules:0.5' 
    androidTestCompile 'com.android.support.test:runner:0.5' 
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' 

}