appium 学习笔记(三)

unittest 框架:

把原来的自动化脚本修改成:

from appium  import webdriver
from time import sleep
import adbbase
import unittest


class happyfoodTest(unittest.TestCase): #自动化框架,继承unittest.TestCase
    """Test com.happy.food"""

    def setUp(self): #准备测试环境,每个用例前执行
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['paltformVersion'] = adbbase.platformVersion()
        desired_caps['deviceName'] = adbbase.platformName()
        desired_caps['appPackage'] = adbbase.appPackage()
        desired_caps['appActivity'] = adbbase.appActivity()

        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)

        sleep(5)

    def test_login(self): #用例,以test开头
        """Test login"""
        self.driver.find_element_by_id("phoneNumber").send_keys("13410066133")
        self.driver.find_element_by_id("passWord").send_keys("1234567a")
        self.driver.back()
        self.driver.find_element_by_id("login").click()

    def tearDown(self): #清理环境,用例执行完后执行
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

执行后发现会报错:

ResourceWarning: unclosed file <_io.TextIOWrapper name=3 encoding='cp936'>

参考别人的脚本,修改自动获取app、设备信息的方式为子进程subprocess

import re
import os
import subprocess

appLocation = "E:\\test\kxlp5_v1.5.5.apk"

def platformName():
    # readDeviceName = list(os.popen('adb devices').readlines())
    # deviceName = re.findall(r'^\w*\b',readDeviceName[1])[0]
    # return deviceName

    cmd = "adb devices"
    result = ""
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         stdin=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    if output != "":
        result = output.split()[4].decode() 
    return result

def platformVersion():
    # deviceAndroidVersion = list(os.popen('adb shell getprop ro.build.version.release').readlines())
    # deviceVersion = re.findall(r'^\w*\b',deviceAndroidVersion[0])[0]
    # return deviceVersion

    cmd = "adb shell getprop ro.build.version.release"
    result = ""
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         stdin=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    if output != "":
        result = output.split()[0].decode()
    return result

def appPackage():
    # appPackageAdb = list(os.popen('chcp 65001 | aapt dump badging '+appLocation+'  | findstr package').readlines())
    # appPackage = re.findall(r"name='(.+?)' versionCode",appPackageAdb[0])[0]
    # return appPackage

    cmd = "chcp 65001 | aapt dump badging "+appLocation+"  | findstr package"
    result = ""
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         stdin=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    if output != "":
        result = re.findall(r"name='(.+?)' versionCode",output.decode())[0]
    return result

def appActivity():
    # appActivityAdb = list(os.popen('chcp 65001 | aapt dump badging '+appLocation+'  | findstr launchable-activity').readlines())
    # appActivity = re.findall(r"name='(.+?)'  label",appActivityAdb[0])[0]
    # return appActivity

    cmd = "chcp 65001 | aapt dump badging "+appLocation+"  | findstr launchable-activity"
    result = ""
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         stdin=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    if output != "":
        result = re.findall(r"name='(.+?)'  label",output.decode())[0]
    return result


subprocess为子进程的方法

split为切割方法,默认以所有空字符进行切片

decode将其他编码字符串解码成python默认的unicode

这里不解码的话可能会报错

re.findall 是正则表达的其中一种


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页