Appium Automated Testing (Java Edition)

[Appium] automated testing

Appium software configuration (only set as shown in the picture)

Appium-Inspector tool configuration

Desired Capabilities property value configuration

property name type value
deviceName text 127.0.0.1:62001
platformName text Android
appPackage text com.smile.gifmaker
appActivity text com.yxcorp.gifshow.HomeActivity
  • Connect to NoxPlayer

adb connect 127.0.0.1:62001

  • Testing Equipment

adb devices

  • get package name

In the build-tools folder, enter the command in cmd

aapt  dump  badging  D :\ Android \ software \ kuaishou.apk ( software path)

  • Get application entry

Find the value of launchable-activity while getting the package name

Configuration result

After starting the session, if the following result appears, the configuration is successful.

Element Detection Tool

InspectorTools app
UIAutomatorViewer tool (native element positioning tool provided by SDK, no need to provide startup parameters)

The tool is in the Android/Android-SDK/tools/bin directory

name explanation

PackageName (package name) unique identifier for the application

ActivityName (class name) the name of a page

by command

adb shell dumpsys activity | find "mFocusedActivity"

View the package name and class name of the currently focused page

App type comparison

APP page layout

APP page controls

ADB components

adb common commands

  • View the help manual

adb help

  • Detect the Android device connected to the computer (note the connection of the emulator)

adb devices

  • Pull the information from the phone and put it on the local computer

adb pull < mobile path > < local path >

  • Push information from local to mobile phone

adb push < local path > < mobile phone path >

  • Log in to the device shell mode (man-machine interface of the command line)

adb shell

  • Install the app

adb install xxx.apk

  • uninstall app

adb  uninstall  com .ss .android .ugc .aweme (application package name)

  • View foreground application package name

adb shell dumpsys activity | find "mFocusedActivity"

  • Terminate adb service

adb kill-server

  • start adb service

adb start-server

  • start the app

  • Clear app’s data and cache

adb shell pm clear package name

  • Coordinate click

adb shell input tap x-axis coordinate y-axis coordinate

  • List all package names

adb shell pm list packages

  • print log

adb logcat (ctrl+c to terminate crawling)

First Experience

package pers.lele;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class FirstAppium {
    public static AndroidDriver<WebElement> androidDriver;

    public  static  void  main (String[] args)  throws MalformedURLException, InterruptedException {
         //1. Create a configuration object 
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
         //2. Add a configuration 
        //deviceName: the tested device 
        desiredCapabilities.setCapability( "deviceName" , "127.0.0.1:62001" );
         //platformName 
        desiredCapabilities.setCapability( "platformName" , "Android" );
         //appPackage: Tested App 
         //automationName:uiautomator2 solves the problem that the input box cannot input data //desiredCapabilities.setCapability("automationName"," 
        uiautomator2"); 
        //appActivity: Test App startup entry 

        //3. Create a driver 
        //The first parameter: Appium communication address 
        //The second parameter: Configuration object 
        androidDriver = new AndroidDriver<WebElement>( new URL( "http://127.0.0.1:4723/wd/hub " ),desiredCapabilities);

        searchTest();
    }

    public  static  void  searchTest ()  throws InterruptedException {
         //Wait for elements to be loaded 
        Thread.sleep( 1000 * 10 );
         //1. Find the search button and click 
         //2. Find the search box and enter the passerby king 

        // affirmations: 
        /*
            String expected = "";
            String result androidDriver.currentActivity();
            Assert.assertEquals(expected,result);
        */

        //Driver destruction 
        //androidDriver.quit();
    }
}

App element positioning

  • ID : The unique identifier of the control, but a page may have multiple identical ids due to the laxness of the app project developer

element waiting

  • forced to wait

Thread.sleep();

  • implicit wait

androidDriver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

  • Display Wait: Set the wait time for an element

WebDriverWait webDriverWait = new WebDriverWait(androidDriver,10);
WebElement webElement = webDriverWait.until(new ExpectedCondition<WebElement>() {
       @NullableDecl
       @Override
       public WebElement apply(@NullableDecl WebDriver webDriver) {
       }
   });
webElement.click();

Gesture Operation – Swipe

TouchAction touchAction = new TouchAction(androidDriver);
   PointOption startPoint = PointOption.point(476, 1326);
   PointOption endPoint = PointOption.point(414, 234);
   Duration duration = Duration.ofMillis(500);
   WaitOptions options = WaitOptions.waitOptions(duration);
   touchAction.press(startPoint).waitAction(options).moveTo(endPoint).release();
   touchAction.perform();

Leave a Comment

Your email address will not be published. Required fields are marked *