App automated testing - use of Appium

Introduction

Appiumis an open source test automation framework for native, hybrid and mobile web applications.

Install

AppiumThere are two installation methods, one is npmto install through the command line, the other is to install the visual tool.

  • npmInstall

    npmThe installation method is a little more troublesome. You need to install it before you npmcan operate it. If you want to know more, please refer to: Installing Appium

  • Visual tool installation

    Visualization tools can be downloaded from Github

This article uses the visual tool installation method.

Configure & Run

After the installation is complete, start it Appium.exeand click Edit Configuration

Insert image description here
In the pop-up configuration interface, fill in the environment variable path that has been configured in advance and save it to restart.

Insert image description here
After restarting, click Start Server

Insert image description here
Click Start Inspector Session in the upper right corner

Insert image description here
Insert image description here
Click the "+" sign in the lower middle to add parameters

Capability Description
platformName Device operating system
platformVersion Device operating system version
deviceName adb deviesDevice name ( the currently connected device can be obtained by executing adb )
appPackage Application package name
appActivity Activity name for Android activities launched from appPackage
noReset true or false, whether to clear app data when executing the program

For more capabilities, please refer to: Appium Desired Capabilities

Fill in some parameters as follows:

Insert image description here
After clicking to start the session, the following window will pop up:

Insert image description here
AppiumThrough recording operations, languages ​​that can be run by programs such as JS, Java, Python, and Ruby can be generated. Specific operations include: Start the operation → Click a button or input box on the app interface → After clicking, relevant information will be displayed in the selected element on the right → Select the operation of clicking or sending the key → Refresh the screenshot.

Insert image description here
Some selection elements cannot be selected, and coordinates need to be used to locate the clicked position. After recording the relevant operations, copy the code generated by the recording to the clipboard and paste it into the method annotated by Android Studiothe development tool .@Test

Insert image description here
runIn one click, the steps for automated UI testing are completed.

Insert image description here
To execute the code in it, three jar packages, , and should be introduced in advance. The download address is at the end of the article Android Studio. Part of the code is as follows:commons-lang3java-clientselenium-server-standaloneJava

	@Before
    public void initAppium() {
    
    
        File classpathRoot = new File(System.getProperty("user.dir"));
        // 获取apps文件,前面测试应用所存放的目录
        File appDir = new File(classpathRoot, "/test_app/");
        // 获取apk文件
        File app = new File(appDir, "AppiumAutoTest.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "测试的设备系统");
        capabilities.setCapability("platformVersion", "系统平台版本号");
        capabilities.setCapability("deviceName", "测试的设备名称");
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("appPackage", "被测应用包名");
        capabilities.setCapability("appActivity", "app启动的第一个运行界面");
        // 连接appium启动相应app
        try {
    
    
            driver = new AndroidDriver<>(new URL("http://192.168.31.98:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("App is launched!");
    }

    @Test
    public void startTest() throws InterruptedException {
    
    
        MobileElement el3 = (MobileElement) driver.findElementByAccessibilityId("Reflow");
        el3.click();
        sleep(1000);
        MobileElement el4 = (MobileElement) driver.findElementByAccessibilityId("Slideshow");
        el4.click();
        sleep(1000);
        MobileElement el5 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/androidx.drawerlayout.widget.DrawerLayout/android.view.ViewGroup/android.widget.LinearLayout[2]/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.view.ViewGroup");
        el5.click();
        sleep(1000);
        execRootCmdSilent("adb shell input tap 958 1588");
        sleep(1000);
        MobileElement el6 = (MobileElement) driver.findElementByAccessibilityId("更多选项");
        el6.click();
        sleep(1000);
        MobileElement el7 = (MobileElement) driver.findElementById("com.miyue.appiumautotest:id/title");
        el7.click();
        sleep(1000);
        MobileElement el8 = (MobileElement) driver.findElementByAccessibilityId("转到上一层级");
        el8.click();
    }
    
	private void sleep(int second) throws InterruptedException {
    
    
        Thread.sleep(second);
    }

    /**
     * 使用adb命令执行点击的坐标点
     * @param paramString
     */
    public void execRootCmdSilent(String paramString) {
    
    
        String content = "";
        BufferedReader reader = null;
        InputStream is = null;
        try {
    
    
            java.lang.Process process = Runtime.getRuntime().exec(paramString);
            is = process.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is));
            StringBuffer output = new StringBuffer();
            int read;
            char[] buffer = new char[4096];
            while ((read = reader.read(buffer)) > 0) {
    
    
                output.append(buffer, 0, read);
            }
            content = output.toString();
            System.out.println("App execution adb done!" + content);
        } catch (Exception e) {
    
    
            e.printStackTrace();
            System.out.println("App issue!" + e.getMessage());
        } finally {
    
    
            if (null != is) {
    
    
                try {
    
    
                    is.close();
                } catch (Exception e) {
    
    
                    e.printStackTrace();
                }
            }
            if (reader != null) {
    
    
                try {
    
    
                    reader.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 运行完成
     * @throws InterruptedException
     */
    @After
    public void end() throws InterruptedException {
    
    
        // 退出应用
        driver.quit();
    }

Problems & Solutions

  • no tasks available.

    Reason : Not configured Run/Debug Configurations.

  • Connection refused: connect.

    Reason : AppiumThe service is not started.

  • An unknown server-side error occurred while processing the command. Original error: Unable to find an active device or emulator with OS 11. The following are available: 6HJ4C19A29009173 (10).

    Cause : platformVersion The operating system entered is not the version of the operating system connected to.

  • An unknown server-side error occurred while processing the command. Original error: Could not find a connected Android device in 20088ms.

    Reason : Not connected to the test device. You need to use adb or a data cable to connect to the device. Execute the adb command adb devicesto view the currently connected device, and copy and paste the device name to deviceName.

  • Re-code the test steps and cleancache should be run in advance.

Advantages Disadvantages

advantage

  • Supports third-party applications.
  • Supports testing multiple devices at the same time.
  • Supports generating test code in multiple languages.
  • There is no need to compile third-party code into the app.
  • Free your hands and avoid unnecessary repeated operations.
  • Supports applications for IOS, Android, and Windows.
  • 支持Native apps、 Hybrid apps、Web apps。

shortcoming

  • Kotlin code that can be used to write Android programs is not currently supported.
  • Compared with manual testing, Appium automatic testing is not suitable for small-scale testing.

Summarize

Oh, I believe your views will be refreshed after you experience it Appium.

If the application under test does not require multiple repetitive UI tests, there seems to be no need to use Appiumautomated testing. Compared with manual click UI testing, the speed does not seem to be the slightest bit slower.

Recently I learned about something called AccessibilityService(Accessibility Service), which can be used to develop Application scripts. The script execution speed is extremely fast. It's a pity that it is not an automated testing framework, otherwise it would be very good for automated testing.

Android StudioCode download address for this article : Code download address for Appium automated testing

References

1. Appium official website
2. Appium download address
3. java-client.jar package download address
4. commons-lang3.jar package download address
5. Appium builds Android automated testing framework
6. selenium-server-standalone.jar package download address

Guess you like

Origin blog.csdn.net/baidu_41616022/article/details/126983066