Create a new Apps folder on the Java project to hold the APK package being tested and drag and drop the apk into the folder:
Second, create a new class, enter the code as follows:
/* * Install application contactmanager.apk * Open Contactmanager, enter contact name, mailbox, click Save */package appiumtest; import io.appium.java_client.android.AndroidDriver;import org.junit.After; import org.junit.before;import org.junit.test;import org.openqa.selenium.by;import org.openqa.selenium.webelement;import org.openqa.selenium.remote.capabilitytype;import Org.openqa.selenium.remote.desiredcapabilities; import java.io.file;import java.net.url;import java.util.List; public class demo1 { private androiddriver driver; @Before public void SetUp () throws exception { // set up appium file classpathroot = new file ( System.getproperty ("User.dir")); &nbSp; file appdir = new file (classpathroot, "apps"); file app = new file (appDir, "ContactManager.apk") ; desiredcapabilities capabilities = new Desiredcapabilities (); capabilities.setcapability ( capabilitytype.browser_name, ""); Capabilities.setcapability ("PlatformName", "Android"); Capabilities.setcapability ("DeviceName", "Android emulator"); capabilities.setcapability ("Platformversion", "4.2.2"); capabilities.setcapability ("App", app.getabsolutepath ()); capabilities.setcapability ("apppackage", "com.Example.android.contactmanager "); capabilities.setcapability (" Appactivity ", ". Contactmanager "); driver = new androiddriver (new url ("Http://127.0.0.1:4723/wd/hub"), capabilities); } @After public void teardown () throws exception { driver.quit (); } @Test public void addcontact () { webelement el = driver.findelement (By.name ("Add contact")); el.click (); list< Webelement> textfieldslist = driver.findelementsbyclassname ("Android.widget.EditText"); &Nbsp; textfieldslist.get (0). SendKeys ("Some name"); textfieldslist.get (2). SendKeys ("[email protected]"); //driver.swipe (100, 500, 100, 100, 2); driver.findelementbyname ("Save"). Click (); }}
Third, open the Android emulator, run the test case, the system automatically installs the tested contactmanager.apk, and opens, executes the test case, closes the app.
Note: If you do not want the program to be installed automatically, you can manually install it using the command (make sure your Android emulator is turned on), win+r, enter "adb install the path and name of the APK to be tested."
As I prepare the apk named "app-release0902.apk", in the computer "C:\Users\xiaoqing.he\Desktop\coding" folder, you should enter the command "adb install C:\Users\ Xiaoqing.he\desktop\coding\app-release0902.apk ", appear status, prompt success that means the installation is successful!
Appium Test Cases--install APK with code