测试人工智能自动语音识别系统之IOS

2020-06-30 18:10:21 浏览数 (1)

前面写过Android的测试情况,今天来讲讲IOS如何来测。 其实IOS跟Android区别不大。在Android里面,很多参数是可以用ADB来获取的,但是IOS不太好弄。 有没有办法呢?

libimobiledevice 是一个跨平台的软件库,支持 iPhone®, iPod Touch®, iPad® and Apple TV® 等设备的通讯协议。

安装

命令:

brew install --HEAD libimobiledevice

brew install --HEAD ideviceinstaller

PS:需要加上 –HEAD 选项,如果不加,安装是老版本,不支持iOS10的手机,所以安装时,需要加上 –HEAD 选项

常见问题: 设备名称:

代码语言:javascript复制
idevicename -u [udid]

Could not connect to lockdownd. Exiting.

尝试以下命令重新编译库

代码语言:javascript复制
brew uninstall -f libimobiledevice ideviceinstaller usbmuxd

如果失败执行:

代码语言:javascript复制
brew uninstall --ignore-dependencies libimobiledevice ideviceinstaller usbmuxd

brew install -v --HEAD --fetch --build-from-source usbmuxd libimobiledevice ideviceinstaller

常用命令

1.查看当前连接的设备

idevice_id -l #显示当前所连接的设备[udid],包括 usb、WiFi 连接

2.安装应用

ideviceinstaller -u [udid] -i [xxx.ipa] #xxx.ipa:安装文件路径

3.卸载应用 ideviceinstaller -u [udid] -U [bundleId] #bundleId:应用的包名

4.查看安装的三方包 ideviceinstaller -u [udid] -l # 指定设备,查看安装的第三方应用 ideviceinstaller -u [udid] -l -o list_user # 指定设备,查看安装的第三方应用 ideviceinstaller -u [udid] -l -o list_system # 指定设备,查看安装的系统应用 ideviceinstaller -u [udid] -l -o list_all # 指定设备,查看安装的系统应用和第三方应用

5.获取设备信息 ideviceinfo -u [udid] # 指定设备,获取设备信息 ideviceinfo -u [udid] -k DeviceName # 指定设备,获取设备名称:iPhone6s idevicename -u [udid] # 指定设备,获取设备名称:iPhone6s ideviceinfo -u [udid] -k ProductVersion # 指定设备,获取设备版本:10.3.1 ideviceinfo -u [udid] -k ProductType # 指定设备,获取设备类型:iPhone8,1 ideviceinfo -u [udid] -k ProductName # 指定设备,获取设备系统名称:iPhone OS

安装appium:

代码语言:javascript复制
npm i -g cnpm --registry=https://registry.npm.taobao.org

cnpm install -g appium

这样就可以跑起来了。

获取设备的信息:

代码语言:javascript复制
import subprocess


def cmd(cmd):
    return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


def get_device_udid():
    if cmd('idevice_id -l').stdout != "":
        result = cmd('idevice_id -l').stdout.readline()
        return result.decode("utf-8").strip()


def get_version():
    if cmd('ideviceinfo -k ProductVersion').stdout != "":
        result = cmd('ideviceinfo -k ProductVersion').stdout.readline()
        return result.decode("utf-8").strip()


def get_device_show_name():
    if cmd('idevicename').stdout != "":
        result = cmd('idevicename').stdout.readline()
        return result.decode("utf-8").strip()


def get_device_name():
    if cmd('ideviceinfo -k ProductType').stdout != "":
        result = cmd('ideviceinfo -k ProductType').stdout.readline()
        return result.decode("utf-8").strip()

这样就可以插入什么手机,就不需要手动去填一些参数了。 但使用过程有一个问题,就是不一定每次都能将ideviceinstaller 和 ideviceinfo 跑起来,需要重新安装。

完整代码:

代码语言:javascript复制
from appium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
import arrow
import datetime
# import pygame
import os
import numpy as np
import pandas as pd

from playsound import playsound

TIME_OUT = 30
TIMES = 30
element_max_wait_time = 10
DURATION = 0.5
SLEEPTIME = 13

current_path = os.path.dirname(os.path.abspath(__file__))

audio_file_path = os.path.join(current_path, "audio")
sentence_file = current_path   "/sentence.txt"
result_path = current_path   "/result"
log_path = current_path   "/result/log.txt"

import subprocess


def cmd(cmd):
    return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


def get_device_udid():
    if cmd('idevice_id -l').stdout != "":
        result = cmd('idevice_id -l').stdout.readline()
        return result.decode("utf-8").strip()


def get_version():
    if cmd('ideviceinfo -k ProductVersion').stdout != "":
        result = cmd('ideviceinfo -k ProductVersion').stdout.readline()
        return result.decode("utf-8").strip()


def get_device_show_name():
    if cmd('idevicename').stdout != "":
        result = cmd('idevicename').stdout.readline()
        return result.decode("utf-8").strip()


def get_device_name():
    if cmd('ideviceinfo -k ProductType').stdout != "":
        result = cmd('ideviceinfo -k ProductType').stdout.readline()
        return result.decode("utf-8").strip()


desired_caps = {}
desired_caps["automationName"] = "XCUITest"
desired_caps["platformName"] = "ios"
desired_caps["platformVersion"] = "13.1.1"
desired_caps["udid"] = "00008030-000164D41428802E"
desired_caps["deviceName"] = "iPhone 11 Pro"
desired_caps["bundleId"] = "com.ScoringSample"
desired_caps["xcodeOrgId"] = "Anderson"
desired_caps["xcodeSigningId"] = "iPhone Developer"
desired_caps["updatedWDABundleId"] = "com.SpeechScoringSample"

driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)

result = []


def play_audio(mp3):
    playsound(mp3)


def is_element_appear(driver, element):
    try:
        driver.find_element_by_accessibility_id(element)
        return True
    except:
        return False


def wait_element_appear(driver, element):
    try:
        wait = WebDriverWait(driver, element_max_wait_time, DURATION)
        wait.until(lambda driver: driver.find_element_by_accessibility_id(element))
        return True
    except:
        return False


def type_texts(text):
    input = driver.find_element_by_accessibility_id("txt_phrase")
    input.clear()
    input.send_keys(text)
    # driver.hide_keyboard()
    sleep(1)
    driver.find_element_by_accessibility_id("Load from file").click()

def start_record(file, each_time):
    record = "btn_recod"
    if wait_element_appear(driver, record):
        driver.find_element_by_accessibility_id(record).click()

    play_audio(file)
    sleep(SLEEPTIME)

    sent_record = "btn_recod"

    if wait_element_appear(driver, sent_record):
        driver.find_element_by_accessibility_id(sent_record).click()

    start_time = datetime.datetime.now()

    score = "lbl_message"

    #  Get the end time from judge the score whether appear or not
    while not is_element_appear(driver, score):
        print("please wait, the response still didn't back at {}".format(arrow.now()))
        timeout_diff = arrow.now() - start_time
        print(timeout_diff.seconds)
        if timeout_diff.seconds >= TIME_OUT:
            print("overtime")
            # end_time = start_time.shift(seconds= TIME_OUT)
            break

    end_time = datetime.datetime.now()

    used_time = (end_time - start_time).total_seconds()
    print("time is {}".format(used_time))
    each_time.append(used_time)
    sleep(1)

    # write log in case break out occur even it will lost some performance
    # content = get_brand()   get_version()   ": run {}".format(text)   "used time: {}".format(used_time) "rn"
    # write_log(content)


def write_log(content):
    # write a log in case crash or any breakout
    with open(log_path, 'a ') as f:
        f.writelines(content)


def get_sentence_audio():
    # get each sentence and mp3 map
    mp3_length = []
    with open(sentence_file, "r") as f:
        for index, line in enumerate(f.readlines()):
            line = line.strip("n")
            file = audio_file_path   "/"   str(index   1)   ".mp3"
            mutiply_times(line, file)


def mutiply_times(text, audio_file):
    # play mutiply times
    each_audio_play_time = []
    type_texts(text)
    for i in range(TIMES):
        start_record(audio_file, each_audio_play_time)

    result.append(each_audio_play_time)
    # in case it was crash.
    write_log("current result is {} {}".format(text, each_audio_play_time))


if __name__ == "__main__":
    get_sentence_audio()

    device_name = "iPhone11Pro"
    device_version = "13.1.1"

    # get result for each device
    result_arrary = np.array(result)
    result_pd = pd.DataFrame(result_arrary, columns=range(1, TIMES   1))
    # add the average result
    result_pd['AVG'] = result_pd.mean(axis=1)  # axis 0为列,1为行

    # add the sample in result
    sentences = open(sentence_file, 'r')
    text = sentences.readlines()
    sentences.close()
    sentence_pd = pd.DataFrame(text, columns=["sentence"])
    result_final = sentence_pd.join(result_pd)

    # add the device type in result

    device_arrary = np.array([device_name   "_"   device_version] * len(text)).T
    device_pd = pd.DataFrame(device_arrary)
    result_final = device_pd.join(result_final)

    # write the result to excel
    result_final.to_excel(result_path   "/"   device_name   "_"   device_version   ".xlsx", index=False)

跑下来的效果还可以,结果还比在Android上的结果要好一些。

0 人点赞