[uiautomator篇] python uiautomatorDemo

#coding=utf-8
"""

@version: 1.0

@author: 

@prerequisite:
    based on Python 2.7

@usage:
    1) this is the handler for testcase

@Others:
    No

@description: 3DMark性能跑分

@module: performance

@caselevel: A

@timeout: 1800

"""

from uiautomator import Device
import time
from util import CylixUtilTestCase
import subprocess
import os
import zipfile
import shutil
import traceback
import re

class AI030002Mark3D(CylixUtilTestCase):
    
    PRE_INSTALL_APK = [
    # ('apk\\3DMark.apk','com.futuremark.dmandroid.application')
    # ('apk\\pad-debug.apk','com.softwinner.performance.frameratetest'),
    # ('apk\\pad-debug-androidTest.apk','com.softwinner.performance.frameratetest.test')
    ]



    def testMark3d(self):

        self.installPlugIn()
        self.UiautomatorMark3D()

    def installPlugIn(self):

        resourcedir = os.path.normpath(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'resource'))
        zipFilePath = os.path.normpath(os.path.join(resourcedir, r'mark3d\Android.zip'))
        unzipToPath = os.path.dirname(zipFilePath)
        tempfile = os.path.join(unzipToPath, r'Android')
        self.logger.info("step1: unzip  " + zipFilePath + " to " + unzipToPath)
        if os.path.exists(tempfile):
            self.logger.info(tempfile + ' exist ,delete it')
            self.deleteDirectory(tempfile)
        self.unzip_file(zipFilePath, unzipToPath)

        self.logger.info("step2: push data to device")
        localDataPath = resourcedir + r'\mark3d\Android\data'
        remoteDataPath = '/sdcard/Android' 
        self.device.adb.push(localDataPath, remoteDataPath)
        self.deleteDirectory(tempfile)

    def UiautomatorMark3D(self):
        assert self.Recent(), 'recent fail'
        self.enterApplist()
        self.openApplication('3DMark')
        self.runMark3d()
        self.parserXml()
        assert self.Recent(), 'recent fail'
       

    def enterApplist(self):
        d = Device(self.dut_adb_num)
        d.press.home()
        d.screen.on()

        if d(description = "Apps list", index = 3).wait.exists(timeout=2000):
            self.logger.info("Find App list successfully")
            d(description = "Apps list", index = 3).click()
        else:
            print "Can not find App list"
            assert False, 'Can not find App list'
        if d(resourceId = "com.android.launcher3:id/main_content").wait.exists(timeout=2000):
            self.logger.info("Enter APP list successfully")
            return True
        else:
            self.logger.info("Fail to enter APP list")
            assert False, 'Fail to enter APP list'

    def openApplication(self, packageName):
        d = Device(self.dut_adb_num)
        d.screen.on()
        if d(text = packageName).wait.exists(timeout = 2000):
            self.logger.info("Find %s successfully" % packageName)
            d(description = packageName).click()
        else:
            self.logger.info("Can not find %s" % packageName)
            assert False, "Can not find %s" % packageName

    def runMark3d(self):
        d = Device(self.dut_adb_num)
        d.screen.on()
        time.sleep(10)
        if d(textContains = "3DMARK").wait.exists(timeout = 20000):
            self.logger.info("Enter 3DMARK successfully")
        else:
            self.logger.info("Fail to enter 3DMARK")
            assert False, 'fail to enter 3DMARK'

        if d(description = "RUN").wait.exists(timeout = 20000):
            self.logger.info("Find RUN successfully")
            d(description = "RUN").click()
        else:
            self.logger.info("Find RUN Failure")
            assert False, 'find Run Failure'

        for i in range(1,15):
            time.sleep(30)
            self.logger.info("3DMark run time :%s s" % str(i * 30))
            if i%2 == 0:         
                if d(description = "SHARE").wait.exists(timeout = 2000):
                    self.logger.info("Find SHARE successfully")
                    self.logger.info("********************3DMark DONE********************")
                    return True

        xml_path = self.tcr_fd + os.sep + "3DMarkLiu.xml"
        self.logger.info('xml_path = %s' % xml_path)
        d.dump(xml_path)
        self.logger.info("Finish downloading %s file " % xml_path)
        f = file(xml_path)            
        lines = f.readlines()
        status = False
        for line in lines:
            if "SHARE" in line:
                self.logger.info("Find SHARE successfully")
                self.logger.info("********************3DMark DONE********************")
                status = True
                break;
        f.close()
        assert status, 'mark3d test fail'

    def parserXml(self):

        d = Device(self.dut_adb_num)
        d.screen.on()
        iceStormExtremeXML = os.path.normpath(os.path.join(self.tcr_fd, 'iceStormExtreme.xml'))
        self.logger.info('iceStormExtremeXML = %s' % iceStormExtremeXML )
        d.dump(iceStormExtremeXML)
        f = file(iceStormExtremeXML)
        lines = f.readlines()
        for line in lines:
            # content-desc="Ice Storm Extreme 1891"
            iceStormExtreme_score = re.findall(r'Ice Storm Extreme\s+(\d+)', line, re.M)[0]
            self.logger.info(iceStormExtreme_score)
            if not iceStormExtreme_score:
                assert False, 'recompile fail'
            assert int(iceStormExtreme_score) > 1500, 'iceStormExtreme_score below 1500'
        f.close()

    def unzip_file(self,zipfilename,unziptodir):

        # if not os.path.exists(unziptodir):
        #     os.mkdir(unziptodir,0777)
        assert os.path.exists(unziptodir), unziptodir + "not exist"
        
        zfobj = zipfile.ZipFile(zipfilename)
        for name in zfobj.namelist():
            name = name.replace('\\','/')
            if name.endswith('/'):
                # print "include folder: " + name
                os.mkdir(os.path.join(unziptodir,name))
            else:
                # print "include file: " + name
                ext_filename = os.path.join(unziptodir,name)
                ext_dir = os.path.dirname(ext_filename)
                if not os.path.exists(ext_dir):
                    os.mkdir(ext_dir,0777)
                outfile = open(ext_filename,'wb')
                outfile.write(zfobj.read(name))
                outfile.close()

    def deleteDirectory(self,current_path):
        if not os.path.exists(current_path):
            self.logger.info(current_path + " not exist")
            return
        current_filelist = os.listdir(current_path)
        for f in current_filelist:
            real_path = os.path.join(current_path,f)
            if os.path.isdir(real_path):
                real_folder_path = real_path
                try:
                    for root,dirs,files in os.walk(real_folder_path):
                        for name in files:
                            del_file = os.path.join(root,name)
                            os.remove(del_file)
                    shutil.rmtree(real_folder_path)
                except Exception,e:traceback.print_exc()
            if os.path.isfile(real_path):
                os.remove(real_path)
        os.rmdir(current_path)


    def Recent(self):
        d = Device(self.dut_adb_num)
        displayWidth = int(d.info.get("displayWidth"))
        displayHeight = int(d.info.get("displayHeight"))
        d.press.recent()
        if d(text="No recent items").wait.exists(timeout=2000):
            print "Cleared recent items"
            return True
        if d(descriptionContains="Dismiss").wait.exists(timeout=2000):
            for i in range(50):
                if d(text="CLEAR ALL").wait.exists(timeout=2000):
                    d(text="CLEAR ALL").click()
                    if d(description="Apps list",index=3).wait.exists(timeout=2000):
                        print "Cleared recent items"
                        return True
                    else:
                        print "Fail to clear recent items" 
                        return False
                else:
                    d.swipe(displayWidth/2,displayHeight/4,displayWidth/2,3*displayHeight/4,steps=30)
                    i+=1
            else:
                print "Fail to find CLEAR ALL "
                return False
        else:
            print "EXCEPTION CLEAR RECENT ITEMS"
            return True

 

posted @ 2017-07-17 16:53  liuzhipenglove  阅读(558)  评论(0编辑  收藏  举报