python automation continuous integration: 4 Jenkins integrated APP source code packaging, deployment and automated testing

Posted by nadinengland on Tue, 08 Feb 2022 20:08:27 +0100

I Use the interface to configure task mode integration

1.APP source code packaging principle

Source code github address: https://github.com/princeqjzh/AndroidSampleApp

1.1 packaging dependent environment

  • Installation: SDK (this example takes windows system as an example. SDK is installed under Windows system, which has been explained in the appium chapter. For installing SDK on Linux system, please refer to the following methods)

(1) Download sdk: http://tools.android-studio.org/index.php/sdk/

(2) Create a new directory: / usr/local/sdk under the node machine, upload the downloaded sdk installation package to the directory and unzip it

tar -zxvf android-sdk_r24.4.1-linux.tgz 

(3) Configure environment variables

# Edit environment variable file
vi /etc/profile
# Add the following
export ANDROID_HOME=/usr/local/sdk/android-sdk-linux
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools
# Reload environment variables
source /etc/profile
# test
android -h
# Update sdk
android update sdk --no-ui
  • Install jdk

Refer to the previous chapters

1.2 source code packaging process

After configuring sdk and jdk, you can package the source code.

Step 1: upload the code source file to the node directory

Step 2: enter the source file directory

Step 3: execute the packaging command

gradlew clean assembleDebug

Step 4: go to the directory AndroidSampleApp\app\build\outputs\apk\debug to check whether the APK file is generated

2.APP Installation Principle

After the source code is packaged, you can install it

2.1 dependent environment:

sdk+jdk + real machine or simulator

Take the simulator as an example. sdk and jdk have been deployed ok, so I won't repeat the explanation here.

2.2 installation process

Installation:

# adb install + apk file path
adb install E:\project\AndroidSampleApp\app\build\outputs\apk\debug\app-debug.apk

Uninstall:

adb shell dumpsys activity top  # When the app is started, use this method to obtain the app package name
adb uninstall com.appsflyer.androidsampleapp # adb uninstall + app package name, uninstall app

3.app automation test

3.1 dependent environment

Dependent environment: sdk+jdk+appium + simulator

These environments are installed as explained in the previous appium chapter

3.2 test code

import time
from appium import webdriver

class TestAppSample:
    def setup_class(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['deviceName'] = '127.0.0.1:21503 device'
        desired_caps['appActivity'] = '.MainActivity'
        desired_caps['appPackage'] = 'com.appsflyer.androidsampleapp'
        self.driver =webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)

    def teardown_class(self):
        self.driver.quit()
	
    # Because our app has only one main page and doesn't write anything. Therefore, print is used to replace the operation in the app
    def test_demo(self):
        """test case"""
        time.sleep(5)
        print('test app success')
        

4. Use Jenkins integration

After understanding the working principle of each part, we can start to use Jenkins to integrate this series of operations.

4.1 build pull source code and package tasks






Here, our pull source code + packaging task is completed.

4.2 built in deployment and installation app




4.3 integration automation test






Here, our source code is packaged, released, and the automated testing process is completed. Of course, you can add test report, email and other functions. Because the case has been explained before, I won't repeat it here.

II pipeline integration practice

The way of using interface configuration mentioned above is complicated and difficult to maintain when building a project. Therefore, we generally use the pipeline file method to configure the Jenkins pipeline.

1. Mission objectives


2. Environmental dependence

SDK, JDK, appium, python, simulator or real machine

3. Construction task

(1). Prepare pipeline file

pipeline{
    agent {
    	# Set up operation node
        label 'salve1-windows'
    }

    stages{
        stage('Android program source code synchronization') {
            steps {
            	# Execute bat command
                bat"""
                	# Create a directory to place the source code of git pull
                    md AndroidSampleApp || echo "the dir AndroidSampleApp is exited"
                """
                # Pull the source code and place it in the local directory AndroidSampleApp
                dir("AndroidSampleApp"){
                    git credentialsId:'gitee_ouyi', branch:'master', url:'git@gitee.com:xxx/android-sample-app.git' #app source code warehouse address
                }
            }
        }

        stage('Android compilation and packaging') {
            steps {
                bat"""
                    cd AndroidSampleApp
                    gradlew clean assembleDebug
                """
            }
        }


        stage('Android deployment') {
            steps {
                bat"""
                    cd AndroidSampleApp
                    set pwd=%cd%
                    adb uninstall com.appsflyer.androidsampleapp || echo "the app not exit, not need uninstall"
                    adb install %pwd%/app/build/outputs/apk/debug/app-debug.apk
                """
            }
        }

        stage('Automatic test program source code synchronization') {
            steps {
                bat"""
                    md test_appsample || echo 'the dir test_appsample is exited'
                """
                dir("test_appsample"){
                        git credentialsId:'gitee_ouyi', branch:'master', url:'git@gitee.com:xxx/test_appsample.git' # Automation code warehouse address
                    }
            }
        }

        stage('Run automated testing') {
            steps {
                bat"""
                    cd test_appsample
                    pip install -r requirements.txt
                    python test_appsample.py
                """
            }
        }
    }

    post {
        success {
        	# Publish apk file
            archiveArtifacts artifacts: 'AndroidSampleApp/app/build/outputs/apk/debug/app-debug.apk'
        }

        always {
        	# Send mail
            emailext body: '$DEFAULT_CONTENT', recipientProviders: [[$class: 'RequesterRecipientProvider']], subject: '$DEFAULT_SUBJECT'
        }
    }
}


Topics: Python jenkins software testing