python+appium自动化测试-Appium并发测试之python启动appium服务

来自APP Android端自动化测试初学者的笔记,写的不对的地方大家多多指教哦

python启动appium服务,需要使用subprocess模块,该模块可以创建新的进程,并且连接到进程的输入、输出、错误等管道信息,并且可以获取进程的返回值。

测试场景

使用python启动2台appium服务,端口配置如下:

  • Appium服务器端口:4723,bp端口为4724
  • Appium服务器端口:4725,bp端口为4726

说明:bp端口(—bootstrap-port)是appium和设备之间通信的端口,如果不指定到时无法操作多台设备运行脚本。

一、启动单个appium服务

代码实现

import subprocess
from time import ctime

def appium_start(host, port):
    """启动appium server"""
    bootstrap_port = str(port + 1)
    # /b表示后台运行命令行串口
    cmd = 'start /b appium -a ' + host + ' -p ' + str(port) + ' -bp ' + str(bootstrap_port)

    print('%s at %s' % (cmd, ctime()))
    subprocess.Popen(cmd, shell=True, stdout=open('../appium_log/' + str(port) + '.log', 'a'),
                     stderr=subprocess.STDOUT)

# 测试函数,在实际运行过程中可以注释
if __name__ == '__main__':
    host = '127.0.0.1'
    port = 4723
    appium_start(host, port)

运行成功后显示

image.png

启动校验

(一)启动后生成日志文件

(二)启动后我们需要校验服务是否启动成功,校验方法如下:

  1. 首先查看有没有生成对应的log文件,查看log里面的内容。
  2. 使用如下命令来查看,cmd输入:
netstat -ano |findstr 端口号

netstat 命令解释

netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息。输入 netstat -ano 回车.可以查看本机开放的全部端口;输入命令 netstat -h可以查看全部参数含义。

image.png

关闭Appium服务

关闭进程有2种方式,具体如下:

1.通过netstat命令找到对应的Appium进程pid然后可以在系统任务管理器去关闭进程;win7系统任务管理器PID显示

打开电脑的任务管理器 —> 详细信息,找到对应pid后,关闭进程即可

  1. 使用如下命令来关闭:
taskkill -f -pid appium进程pid
注意:pid即通过上面netstat命令查看的状态信息(如上图)

关闭成功后如图所示:

image.png

对以上代码封装成类

# 启动单个appium服务
import subprocess
from time import ctime

class MultiAppium:

    def appium_start(self, host, port):
        """启动appium server"""
        bootstrap_port = str(port + 1)
        # /b表示后台运行命令行串口
        cmd = 'start /b appium -a ' + host + ' -p ' + str(port) + ' -bp ' + str(bootstrap_port)

        print('%s at %s' % (cmd, ctime()))
        subprocess.Popen(cmd, shell=True, stdout=open('./appium_log/' + str(port) + '.log', 'a'),
                         stderr=subprocess.STDOUT)

# 测试函数,在实际运行过程中可以注释
if __name__ == '__main__':
    host = '127.0.0.1'
    port = 4723
    appium_start1 = MultiAppium()
    appium_start1.appium_start(host, port)

二、启动多个appium服务

只需要在执行环境使用循环调用即可。

代码实现

import subprocess
from time import ctime

def appium_start(host, port):
    """启动appium server"""
    bootstrap_port = str(port + 1)
    # /b表示后台运行命令行串口
    cmd = 'start /b appium -a ' + host + ' -p ' + str(port) + ' -bp ' + str(bootstrap_port)

    print('%s at %s' % (cmd, ctime()))
    subprocess.Popen(cmd, shell=True, stdout=open('../appium_log/' + str(port) + '.log', 'a'),
                     stderr=subprocess.STDOUT)

# 测试函数,在实际运行过程中可以注释
if __name__ == '__main__':
    host = '127.0.0.1'
    for i in range(2):
        port = 4723 + 2 * i
        appium_start(host, port)

启动成功后显示:

image.png

用以上方法启动两个appium服务,由于显示启动时间较快,看不出区别,实际两个appium服务不是同时启动的

会生成两个日志文件:

image.png

对以上代码封装成类

# 启动多个appium服务
import subprocess
from time import ctime

class MultiAppium:

    def appium_start(self, host, port):
        """启动appium server"""
        bootstrap_port = str(port + 1)
        # /b表示后台运行命令行串口
        cmd = 'start /b appium -a ' + host + ' -p ' + str(port) + ' -bp ' + str(bootstrap_port)

        print('%s at %s' % (cmd, ctime()))
        subprocess.Popen(cmd, shell=True, stdout=open('./appium_log/' + str(port) + '.log', 'a'),
                         stderr=subprocess.STDOUT)

# 测试函数,在实际运行过程中可以注释
if __name__ == '__main__':
    # 启动多个appium服务
    for i in range(2):
        port = 4723 + 2 * i
        appium_start(host, port)

启动多个appium服务和单个appium服务区别在于运行时传入的port数量

三、多进程并发启动appium服务

python多进程并发启动appium服务需要导入multiprocessing多进程模块

代码实现

# 多进程并发启动多个appium服务
import multiprocessing
import subprocess
from time import ctime

def appium_start_sync(host, port):
    """启动appium server"""
    bootstrap_port = str(port + 1)
    # /b表示后台运行命令行串口
    cmd = 'start /b appium -a ' + host + ' -p ' + str(port) + ' -bp ' + str(bootstrap_port)

    print('%s at %s' % (cmd, ctime()))
    subprocess.Popen(cmd, shell=True, stdout=open('./appium_log/' + str(port) + '.log', 'a'),
                     stderr=subprocess.STDOUT)

# 构建进程组
appium_process = []

# 加载appium进程
for i in range(2):
    host = '127.0.0.1'
    port = 4723 + 2 * i
    appium_sync = multiprocessing.Process(target=appium_start_sync, args=(host, port))
    appium_process.append(appium_sync)

# 测试函数,在实际运行过程中可以注释
if __name__ == '__main__':
    # 并发启动appium服务
    for appium in appium_process:
        appium.start()
    for appium in appium_process:
        appium.join()

结果同上,对以上代码封装成类:

# 多进程并发启动多个appium服务
import multiprocessing
import subprocess
from time import ctime

class AppiumStartSync:
    """启动appium server"""
    def appium_start_sync(self, host, port):
        bootstrap_port = str(port + 1)
        # /b表示后台运行命令行串口
        cmd = 'start /b appium -a ' + host + ' -p ' + str(port) + ' -bp ' + str(bootstrap_port)
        print('%s at %s' % (cmd, ctime()))
        subprocess.Popen(cmd, shell=True, stdout=open('./appium_log/' + str(port) + '.log', 'a'),
                         stderr=subprocess.STDOUT)

    # 构建进程组
    appium_process = []

    # 加载appium进程
    for i in range(2):
        host = '127.0.0.1'
        port = 4723 + 2 * i
        appium_sync = multiprocessing.Process(target=appium_start_sync, args=(host, port))
        appium_process.append(appium_sync)

# 测试函数,在实际运行过程中可以注释
if __name__ == '__main__':
    appium_start = AppiumStartSync()
    # 并发启动appium服务
    for appium in appium_start.appium_process:
        appium.start()
    for appium in appium_start.appium_process:
        appium.join()

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容