第二章android利用QPython环境调用APPium

第二章、android配置QPython环境调用APPium

在第一章APPium基础,扩张移动端,下面第一章位置

第一章、APPium、android自动化

QPython环境搭建

1、android studio 配置python工程

build.gradle配置远程库

 maven { url "https://chaquo.com/maven" } //添加行
  classpath 'com.chaquo.python:gradle:12.0.0'

第二章android利用QPython环境调用APPium_第1张图片
在app的gradle配置(说明:pip是可以下载相关python关联的第三方库)

plugins {
    id 'com.android.application'
    id 'com.chaquo.python'
}

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.by.python_test"
        minSdkVersion 22
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
        python {
            pip {
                install "requests"
                install "Appium-Python-Client==2.11.0"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

第二章android利用QPython环境调用APPium_第2张图片
对应工程调用代码com.chaquo.python的代码

package com.by.python_test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;

public class MainActivity extends AppCompatActivity {
    Button pythonButton;

    Python python;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED||
                ContextCompat.checkSelfPermission(this,
                        Manifest.permission.INTERNET)!= PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.INTERNET}, 100);
        }


        pythonButton = findViewById(R.id.searchButton);


        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }
        python = Python.getInstance();

        pythonButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PyObject pyObject=python.getModule("main");
                pyObject.callAttr("open_app");

            }
        });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

第二章android利用QPython环境调用APPium_第3张图片

python调用APPium

在对应python文件夹创建main.py文件
第二章android利用QPython环境调用APPium_第4张图片

对应的代码

from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.mobileby import MobileBy
import time
import subprocess


def main():
    print("Hello, Python!")

def open_app():
    options = AppiumOptions()
    options.load_capabilities({
        "platformName": "Android",
        "appium:platformVersion": "13.0.0",
        "appium:deviceName": "Mate 30",
        "appium:appPackage": "cn.migudm.aipatinside",
        "appium:appActivity": "cn.migudm.aipatinside.mvp.ui.MainActivity",
        "appium:ensureWebviewsHavePages": False,
        "appium:nativeWebScreenshot": True,
        "appium:newCommandTimeout": 3600,
        "appium:connectHardwareKeyboard": True
    })

    driver = webdriver.Remote("http://192.168.31.104:4723/wd/hub", options=options)

对应运行工程测试效果

appium效果

你可能感兴趣的:(android,appium,python)