background

Automation, performance testing, interface testing, development platform and other work, in the end where is the value of testing, in fact, the source of value constantly enrich and serve the public, today a simple introduction to the UI gadget Appium attack.

A simple introduction

Appium is an open source automated testing tool that supports native, Web, and hybrid apps on iOS and Android platforms. “Mobile native apps” are those written with the iOS SDK or Android SDK. “Mobile Web apps” are apps accessed using mobile browsers (Appium supports Safari on iOS and Chrome on Android). A “hybrid application” is one where native code encapsulates web views — native code interacts with Web content. Appium runs on Windows and Macs, but the Wind only runs on Android devices, and the MAC runs on both android and IOS devices.

Guihub: You can write tests with your favorite dev tools using any WebDriver-compatible language such as Java, Objective-C, JavaScript (Node), PHP, Python, Ruby, C#, Clojure, Or Perl with the Selenium WebDriver API and language-specific client libraries.

Architecture diagram

From the network

UI automatic revenue

Any UI automatic test can not completely replace manual test, the rate of return is not high depends on how the department uses any tool, the use of both sides depends on how to use, if there is repeated work needs manual regression every time, it is recommended to use automatic regression, everyone in the department uses automatic regression, It makes people more confident because every time you have a chance to use your own scripts to validate your own repetitive work.

Is script maintenance really expensive? Everyone says the cost is high, have you really maintained, written scripts? If not written, not defended, no voice. You only know if it’s high when you use it.

Contents summary

Today we use Windos to build Appium automation, using the Java language to do scripting language, content will briefly introduce installation, positioning, use and simple framework running, in order to save you time to tell you the main content of this article is what, so convenient whether to choose to look down. Hopefully a little inspiration for not doing automation.

  • Environment Install Android
  • Common operations
  • Element positioning (native)
  • Simple Java Demo use
  • Simple frame design
  • Report secondary encapsulation
  • Environmental installation

Install the desktop version. Open the following link and select exe to download: github.com/appium/appi…

After download and installation

Click launch:

DOS command Installation

Install the JDK download: www.oracle.com/technetwork… JAVA_HOME: JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_181

%JAVA_HOME%\bin; %JAVA_HOME%\jre\bin;

The CLASSPATH:

.; %JAVA_HOME%\lib; %JAVA_HOME%\lib\tools.jar Java -version validation:

Java version “1.8.0 comes with _181”

Java(TM) SE Runtime Environment (build 1.8.0_181-b13)

Java HotSpot(TM) Client VM (Build 25.181-B13, Mixed mode, Sharing) to install the SDK android-sdk.en.softonic.com/ android-sdk.en.softonic.com/download configuration environment variables: ANDROID_HOME

C:\Program Files (x86)\android-sdk-windows

Path:

; %ANDROID_HOME%\tools; %ANDROID_HOME%\platform-tools download node: nodejs.cn/download/ install appium NPM install -g appium

npm install -g cnpm –registry=registry.npm.taobao.org

cnpm install -g appium –no-cache

cnpm i appium-doctor

Appium -v installation verification environment command: appium-doctor

Appium version check and running display:

Note: If the above environment is not configured, please search baidu yourself

Common operations

Click on the click ()

The input

sendKeys(CharSequence… keysToSend);

remove

clear()

Long press

Public void cartSingleProductImage(AndroidDriver<AndroidElement> driver) String coordinate) {WaitUtil. WaitWebElement (driver, getByLocator getLocatorApp (coordinate), "according to the shopping cart goods pictures - the popup to collect and remove floating layer"); element = driver.findElement(getByLocator.getLocatorApp(coordinate)); int x = element.getLocation().getX(); int y = element.getLocation().getY(); TouchAction action = new TouchAction(driver); // longPress action.longpress (pointoption.point (x, y)).release().perform(); }Copy the code

sliding

   WebElement webElement = null;
    try {
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        webElement = driver.findElementByAndroidUIAutomator(
                "new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().text(\"See more details\"))");
    } catch (Exception e) {
        e.printStackTrace();
    }
Copy the code

Up, down, left, right

The simplest sliding command line:

Uuid indicates the mobile phone number

adb -s ” + uuid + ” shell input touchscreen swipe 400 800 400 400

/ * *

* @param driver * @param direction up, down, left, right */Copy the code

static Duration duration = Duration.ofSeconds(1);

Public static void swipe (AndroidDriver<AndroidElement> driver,String direction){switch (direction.tolowerCase ()){case  "up": SwipeUp(driver); break; case"down": SwipeDown(driver); break; case"left": SwipeLeft(driver); break; case"right": SwipeRight(driver); break; Default: system.out. println(" up, Down, left, right"); break; }}Copy the code

/ * *

* slide * * @param driver */Copy the code

public static void SwipeUp (AndroidDriver driver) {

try{ Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } Dimension size = driver.manage().window().getSize(); intheight = size.height; int width =size.width; LongPress (pointoption.point (width / 2,100)).moveto (pointoption.point (width / 2), height - 100)).release() .perform(); }Copy the code

/ * *

Public static void SwipeDown(AndroidDrive<AndroidElement> driver){try{thread.sleep (1000); } catch(InterruptedException e){ e.printStackTrace(); } Dimension size = driver.manage().window().getSize(); int height = size.height; int width = size.width; new TouchAction(driver) .longPress(PointOption.point(width /2, height -100)) .moveTo(PointOption.point(width /2, 100)).release().perform(); }Copy the code

/ * *

Public static void SwipeLeft(AndroidDriver<AndroidElement> driver) {try{thread.sleep (1000); } catch(InterruptedException e) { e.printStackTrace(); } Dimension size = driver.manage().window().getSize(); int height = size.height; int width =size.width; new TouchAction(driver) .longPress(PointOption.point(width - 100, height / 2)) .moveTo(PointOption.point(100, height /2)).release().perform(); }Copy the code

/ * *

Public static void SwipeRight(AndroidDriver<AndroidElement> driver){try{thread.sleep (1000); }catch(InterruptedException e){ e.printStackTrace(); } Dimension size = driver.manage().window().getSize(); int height = size.height; int width = size.width; new TouchAction(driver).longPress(PointOption.point(100, height /2)) .moveTo(PointOption.point(width -100, height / 2)).release() .perform(); }Copy the code

Retrieve attributes

getAttribute()

Get the text

getText()

Access to resources

getPageSource()

Element localization

There are two methods: one uses [UIAutomatorViewer. Bat] in SKD to locate elements

Open:

Uiautomatorviewer. bat:

Adb Devices: adb Connect 127.0.0.1:62001: adb Connect 127.0.0.1:62001

Simulator link shows:

Click on [UIAutomatorViewer.bat] in the SDK.

Link success display:

Above operation instructions:

Mouse click a control will prompt the control can operate the corresponding content:

Note: In fact, in the mobile terminal automation test, there are few positioning methods, such as ID /name/xpath/ coordinate positioning methods:

Positioning way

Id positioning:

    driver.findElement(By.id("xxxxxx")).click();
Copy the code

The name to locate

 driver.findElement(By.name("xxxxxx")).click();
Copy the code

Xpath positioning

Xpath location is the most commonly used one way, can learn the xpath syntax: www.w3school.com.cn/xpath/xpath… But there are also online daniu do a plug-in, do UI automation can be directly used: github.com/lazytesttea… You can use uiAutomatorViewer. jar in SDK. Click uiAutomatorViewer. bat and it will pop up again as follows:

  driver.findElement(By.xpath("xxxxxx")).click();
Copy the code

The second positioning method:

The current way is to locate h5 page operations

Activation:

Click:

Then enter in the pop-up dialog:

Enter in the box below:

You need to get appPackage and appActivity

Use the following command:

Aapt d badging pinduoduov4.76.0_downcc.com.apk | findstr searches "package launchable - the activity"Copy the code

Get results:

{

“platformName”: “Android”,

“DeviceName” : “127.0.0.1:62001”.

“appPackage”: “com.xunmeng.pinduoduo”,

“appActivity”: “com.xunmeng.pinduoduo.ui.activity.MainFrameActivity”

}

Click the start

Display startup

After startup, display:

Once started, all that remains is the same as any other operation

Note: If you have problems at startup, tweak it yourself. Conclusion:

Using Maven to establish the project, through TESNG to do the test class and parameter transfer, the above simple introduction of the environment deployment, positioning, startup class, report class and other methods. In the practical work of the far is not enough, but with the introduction to do or you can refer to reference and foundation engineering framework, if you want to directly start when running the test class name how to start the service side need to reference: you can refer to https://www.cnblogs.com/yc-c/p/9015621.html blogs; With commands, you can start the server using the DOS utility classes described above.Copy the code

Disadvantages: this is only android did not introduce how to test H5 positioning, IOS deployment.