Ejemplo n.º 1
0
class Common():
    def __init__(self):
        self.log = Logger().get_logger()
        self.resolution = None
        self.hall_page = Hall_Page()
        self.level_page = Level_Page()
        self.game_page = Game_Page()

    def setupdriver(self, agrs={}):
        '''初始化driver
        '''
        # 初始化Luadriver
        self.luaobj = LuaDriver()
        self.luaobj.creatLuaDriver(agrs)
        self.luadriver = self.luaobj.getLuaDriver()
        singleGlobalVar.set_map("luaDriver", self.luaobj.getLuaDriver())

    def closedriver(self):
        '''关闭driver
        '''
        singleGlobalVar.get("luaDriver").quit()

    def platformLog(self, msg):
        logStr = ""
        for v in msg:
            if type(v) != str:
                v = str(v)
            logStr = logStr + v
        self.log.info(logStr)

    def closeactivity_switchserver(self, luadriver):
        '''
        关闭活动页面,切换到指定服,然后再关闭弹出的活动页面
        '''
        # self.hall_page = Hall_Page()
        self.closeactivity()
        self.switchserver()
        time.sleep(8)
        self.closedriver()
        capabilities = {}
        capabilities['noReset'] = True
        self.setupdriver(capabilities)
        # self.hall_page.wait_element("同步标志", 12).click()
        self.closeactivity()

    def closeactivity(self):
        '''
       关闭活动页面
       '''
        # self.hall_page = Hall_Page()
        time.sleep(6)
        self.hall_page.wait_element("同步标志")
        # self.hall_page.wait_element("同步标志").click()
        try:
            self.hall_page.wait_element("返回", 1).click()
        except:
            print "未出现关闭按钮"
        j = 0
        while j < 2:
            try:
                elements = self.luadriver.find_elements_by_class_name(
                    "android.widget.Button")
                for h in range(len(elements)):
                    elements[h].click()
            except:
                print "未出现原生按钮"
            j += 1
        i = 0
        while i < 1:
            try:
                if self.hall_page.element_is_exist("签到", 1):
                    self.hall_page.wait_element("签到", 0).click()
                self.hall_page.wait_element("关闭", 1).click()
            except:
                print "未出现关闭按钮"
            try:
                self.hall_page.wait_element("关闭1", 1).click()
            except:
                print "未出现关闭按钮"
            try:
                self.hall_page.wait_element("关闭3", 1).click()
            except:
                print "未出现关闭按钮"
            i += 1

    def user_money(self, money=1000):
        #判断当前用户金币数目,如果破产则增加金币
        env = self.get_config_value('casecfg', 'env')
        if env == "0":
            #如果是外网环境,则通过图片方式来获取金币数
            image_element = self.hall_page.wait_element("金币显示")
            # print "image_element"+image_element
            text = self.image_text(image_element)
            print "text:" + text
            if text.isdigit():
                if int(text) < int(money):
                    self.setting_imei()
            elif text.isdigit() == False:
                self.setting_imei()

        elif env == "1":
            mid = self.get_config_value('casecfg', 'mid')
            # money1 = Interface.get_money(mid)
            # if int(money1) < int(money):
            self.set_money(mid, money)

    def switchserver(self):
        '''
        根据cfg文件切换正式服,测试服,预发布服
        @return:
        '''
        # self.hall_page = Hall_Page()
        self.hall_page.wait_element("切换环境按钮").click()
        while self.hall_page.element_is_exist("内网登录") == False:
            self.hall_page.wait_element("切换环境按钮").click()
            time.sleep(5)
        env = self.get_config_value('casecfg', 'env')
        if env == '0':
            try:
                self.hall_page.wait_element("外网正式登陆", 20).click()
            except:
                print "外网正式登陆失败"
        elif env == '1':
            try:
                self.hall_page.wait_element("内网登录", 20).click()
            except:
                print "内网登录失败"
        elif env == '2':
            try:
                self.hall_page.wait_element("外网测试登录", 20).click()
            except:
                print "外网测试登录失败"
        time.sleep(3)
        i = 0
        while (i < 3):
            i += 1
            try:
                self.hall_page.wait_element("关闭", 3).click()
            except:
                print "关闭对话框"

    def switchnetwork(self, luadriver, network):
        '''
        测试用例运行过程中切换网络
        '''
        if (network == '无网络'):
            print "设置为无网络状态"
            luadriver.set_network_connection(ConnectionType.NO_CONNECTION)
        if (network == 'WIFI模式'):
            print "设置为WIFI模式"
            luadriver.set_network_connection(ConnectionType.WIFI_ONLY)
        if (network == '数据网络'):
            print "设置为数据网络模式"
            luadriver.set_network_connection(ConnectionType.DATA_ONLY)
        if (network == '飞行模式'):
            print "设置为飞行模式"
            luadriver.set_network_connection(ConnectionType.AIRPLANE_MODE)
        if (network == '全部网络打开模式'):
            print "设置为全部网络打开模式"
            luadriver.set_network_connection(ConnectionType.ALL_NETWORK_ON)

    def network_connect(self):
        '''
        2个线程的方式启动网络
        '''
        # print self.luadriver.network_connection
        if self.luadriver.network_connection != 2:
            t1 = threading.Thread(target=self.switch_network)
            t2 = threading.Thread(target=self.closebtn)
            t1.start()
            t2.start()
            t1.join()
            t2.join()

    def switch_network(self):
        '''
        测试用例运行过程中切换网络
        '''
        cmd = "shell am start -n com.example.unlock/.Unlock"
        print "adb start:" + str(time.time())
        self.luadriver.adb(cmd)
        print "adb end:" + str(time.time())

    def closebtn(self):
        time.sleep(1)
        print "closebtn" + str(time.time())
        try:
            self.luadriver.find_element_by_android_uiautomator(
                'new UiSelector().textMatches("确定|允许|允许一次")').click()
            # print "close1" + str(time.time())
        except:
            print "1:" + str(time.time())
        try:
            self.luadriver.find_element_by_android_uiautomator(
                'new UiSelector().textMatches("确定|允许|允许一次")').click()
            # print "close2" + str(time.time())
        except:
            print "2:" + str(time.time())
        try:
            self.luadriver.find_element_by_android_uiautomator(
                'new UiSelector().textMatches("确定|允许|允许一次")').click()
            # print "close3" + str(time.time())
        except:
            print "3:" + str(time.time())

    def swipeelement(self, element1, element2):
        swipe_startx = element1.location['x']
        swipe_starty = element1.location['y']
        swipe_endx = element2.location['x']
        swipe_endy = element2.location['y']
        print swipe_startx, swipe_starty, swipe_endx, swipe_endy
        self.luadriver.swipe(swipe_startx, swipe_starty, swipe_endx,
                             swipe_endy, 1000)

    def unlock(self):
        #解锁
        self.luadriver.adb("shell am start -n com.example.unlock/.Unlock")
        time.sleep(4)
        self.luadriver.keyevent(3)  # home
        time.sleep(3)
        self.luadriver.adb("shell am start -n com.example.unlock/.Unlock")
        self.luadriver.keyevent(3)  # home
        time.sleep(3)
        print "读配置,拉起游戏"
        config = ConfigHelper(constant.cfg_path)
        self.luadriver.start_activity(config.getValue('appium', 'apppackage'),
                                      config.getValue('appium', 'appactivity'))

    def get_config_value(self, section, key):
        #从cfg.ini文件获取配置项的值
        config = ConfigHelper(constant.cfg_path)
        value = config.getValue(section, key)
        return value

    def set_config_value(self, section, key, value):
        #设置cfg.ini文件获取配置项的值
        config = ConfigHelper(constant.cfg_path)
        config.modifConfig(section, key, value)
        return True

    def random_str(self, len):
        '''生成随机字符'''
        str = ""
        for i in range(len):
            str += (random.choice(
                "safsdfsdfoewrweorewcvmdfadfdsafdskafaklvoreiutwuerpmvcmvasieqwoejandfsx1232183721873219731212345678890qweqweoieroeitoretoyriosadjaslkjf"
                "dsafkjljsxkznvcmxnvdfkjglajmndje"))
        return str

    def get_mid(self):
        '''获取用户mid'''

        self.hall_page = Hall_Page()
        self.hall_page.wait_element("头像").click()
        while self.hall_page.element_is_exist("用户ID", 1) == False:
            self.hall_page.wait_element("头像", 0).click()
        userid = self.hall_page.wait_element("用户ID").get_attribute('text')
        mid = filter(lambda ch: ch in '0123456789', userid)
        print "获取的用户mid为: %s" % mid
        self.hall_page.wait_element("返回").click()
        if (self.hall_page.element_is_exist("关闭")):  # 如果弹破产弹框,则关闭
            self.hall_page.wait_element("关闭").click()
        self.set_config_value("casecfg", "mid", str(mid))

    def set_money(self, mid, value):
        # 获取用户金币信息
        money = Interface.get_money(mid)
        addmoney = int(value) - int(money)
        # print addmoney
        if addmoney < 0:
            Interface.add_money(mid, 0 - addmoney, 1)
        else:
            Interface.add_money(mid, addmoney, 0)
        # print Interface.get_money(mid)

    def image_text(self, elment1, image_name='11.bmp', lan="eng"):
        self.game_page = Game_Page()
        path = self.game_page.get_screenshot_by_element(elment1, image_name)
        # print path
        from utils.util import image_get_text
        text = image_get_text(path, lang=lan)
        # print "text:"+text
        return text

    def setting_imei(self):
        self.hall_page = Hall_Page()
        while self.hall_page.element_is_exist("imei输入框", 1) == False:
            self.hall_page.wait_element("切换环境按钮", 0).click()
        imei_text = self.random_str(random.randint(4, 15))
        print imei_text
        self.hall_page.wait_element("imei输入框", 0).send_keys(imei_text)
        self.hall_page.wait_element("imei输入框", 0).click()
        while self.hall_page.element_is_exist("imei重登录", 3):
            self.hall_page.wait_element("imei重登录", 0).click()
        # while self.hall_page.element_is_exist("试玩账号",1)==False:
        try:
            self.hall_page.wait_element("切换环境按钮", 0).click()
            self.hall_page.wait_element("imei重登录", 0).click()
        except:
            print "imei重登录"
        # self.closeactivity()
        while self.hall_page.element_is_exist("马上重连", 3):
            self.hall_page.wait_element("马上重连", 0).click()
        while self.hall_page.element_is_exist("试玩账号", 3):
            self.hall_page.wait_element("试玩账号", 0).click()
            self.hall_page.wait_element("继续游客登陆").click()
            # while self.hall_page.element_is_exist("签到"):
            #     self.hall_page.wait_element("签到").click()
        self.closeactivity()
        # self.hall_page.wait_element("同步标志")

    def deletefile(self):
        driver = singleGlobalVar.get("luaDriver")
        self.platformLog("deleteFile  开始测试前删除配置及性能相关文件")
        config = ConfigHelper(constant.cfg_path)
        package = config.getValue('appium', 'apppackage')
        #删除性能测试结果文件
        delCsvCmd = "shell rm -r /mnt/sdcard/" + package + "_result.csv"
        self.platformLog(delCsvCmd)
        try:
            driver.adb(delCsvCmd)
        except:
            self.platformLog("删除性能测试文件失败")
        #minicap截图文件夹
        delMinicapCmd = "shell rm -rf /mnt/sdcard/minicap_screen"
        self.platformLog(delMinicapCmd)
        try:
            driver.adb(delMinicapCmd)
        except:
            self.platformLog("删除minicap截图文件夹失败")
        mkdirCmd = "shell mkdir /mnt/sdcard/minicap_screen"
        self.platformLog(mkdirCmd)
        try:
            driver.adb(mkdirCmd)
        except:
            self.platformLog("新建minicap截图文件夹失败")

    def getResumeTime(self):
        '''
        获取游戏启动完成时间,目前是头像元素出现的时间
        '''
        time.sleep(2)
        self.platformLog("-------------getResumeTime----------")
        try:
            self.hall_page.wait_element("启动完成标志", 180, 0.1)
            # utc = '%.2f' % (time.time())
            print "apk start completely"
            self.platformLog("apk start completely")
        except:
            self.platformLog("获取标志app成功成功元素失败")

    def startCollectApp(self):
        '''
        启动性能采集apk
        '''
        self.platformLog("startCollectApp start pmonitor apk")
        config = ConfigHelper(constant.cfg_path)
        #性能采集apk acticity
        apkName = config.getValue('appium', 'collectApkName')
        # 被测apk packageName
        testAPKName = config.getValue('appium', 'apppackage')
        # 最终生成结果文件在手机中目录
        resultFilePath = "/mnt/sdcard/" + testAPKName + "_result.csv"

        # SDK 17后由于权限问题,需要加--user 0
        startCmd = "shell am start --user 0 -n " + apkName + " --es packageName " + testAPKName + " --es filePath " + resultFilePath
        self.platformLog(startCmd)
        singleGlobalVar.get("luaDriver").adb(startCmd)

    def sendStopServiceBroad(self):
        self.platformLog("sendStopServiceBroad")
        # 广播类型
        broadcastType = "com.boyaa.stf.stopService"
        cmd_send = "shell am broadcast -a " + broadcastType
        singleGlobalVar.get("luaDriver").adb(cmd_send)

    def sendTagBroadcast(self, tagName, tagType):
        '''
        向性能采集apk发送场景广播
        :param tagName:
        :param tagType:
        :return:
        '''
        #广播类型
        broadcastType = "com.boyaa.stf.UIScript"
        cmd_send = "shell am broadcast -a " + broadcastType + " --es tagName " + tagName + " --es tagType " + tagType
        self.platformLog(cmd_send)
        singleGlobalVar.get("luaDriver").adb(cmd_send)

    def getResolution(self):
        '''
        获取分辨率
        :return: 直接返回的是minicap截图需要的分辨率参数 游戏会自动设置横屏,所以width>height
        '''
        if (self.resolution == None):
            screenWidth = singleGlobalVar.get(
                "luaDriver").get_window_size()['width']
            screenHeigth = singleGlobalVar.get(
                "luaDriver").get_window_size()['height']
            self.resolution = str(screenWidth) + "x" + str(
                screenHeigth) + "@" + str(screenWidth) + "x" + str(
                    screenHeigth)

    def taskScreenShot(self, fileName):
        '''
        由于appium截图效率问题,改为stf平台的minicap进行截图,其中-P如下:
        The format of the -P argument is: {RealWidth}x{RealHeight}@{VirtualWidth}x{VirtualHeight}/{Orientation}. The "virtual" size is the size of the desired projection.
        The orientation argument tells minicap what the current orientation of the device is (in degrees),
        可执行minicap命令:
        adb shell LD_LIBRARY_PATH=/data/local/tmp exec /data/local/tmp/minicap -P 720x1280@720x1280/90 -s >/sdcard/minicap_1504612944860.jpg
        :param page:  元素所在page页
        :param fileName: 截图保存的文件名
        :return:
        '''
        driver = singleGlobalVar.get("luaDriver")
        try:
            broadcastType = "com.boyaa.stf.screenShot"
            cmd_send = "shell am broadcast -a " + broadcastType + " --es sShotName " + fileName
            self.platformLog(cmd_send)
            driver.adb(cmd_send)
        except:
            self.platformLog("通知apk截图时间失败")

        try:
            cmd_pre = "shell LD_LIBRARY_PATH=/data/local/tmp exec /data/local/tmp/minicap -P "
            cmd_minicap = cmd_pre + self.getResolution(
            ) + "/0 -s >/mnt/sdcard/minicap_screen/" + fileName
            self.platformLog(cmd_pre)
            driver.adb(cmd_minicap)
        except:
            self.platformLog("调用minicap截图失败")

    @staticmethod
    def printStr(*info):
        '''
        打印log,自动将所有传入非字符串类型转换为字符串然后打印出来
        '''
        logStr = ""
        for v in info:
            if type(v) != str:
                v = str(v)
            logStr = logStr + v
        timeStamp = str(datetime.datetime.now())
        print timeStamp + " " + logStr
Ejemplo n.º 2
0
class Common():
    def __init__(self):
        self.log = Logger().get_logger()
        self.resolution = None
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        self.setting_page = Setting_Page()

    def setupdriver(self, agrs={}):
        '''
        luadriver 用于游戏操作,对应appium
        始化必然会执行到的逻辑
        全局共用driver  其余地方都是通过singleGlobalVar.get("luaDriver")获取
        '''
        self.platformLog("---------setupdriver---------")
        # 初始化Luadriver
        self.luaobj = LuaDriver()
        self.luaobj.creatLuaDriver(agrs)
        singleGlobalVar.set_map("luaDriver", self.luaobj.getLuaDriver())

    def closedriver(self):
        '''关闭driver
        '''
        singleGlobalVar.get("luaDriver").quit()

    def checkPopVisible(self, page, flagElName="同步标志"):
        '''
        根据page同步标志判断弹框是否存在,直至元素不存在或者操作次数操作3次
        :param page: 操作页
        :param flagElName: 同步标志元素name
        :param closeElName:关闭弹框元素name
        :return:
        '''
        cnt = 0
        while cnt < 3:
            print "cnt:" + str(cnt)
            try:
                page.get_element(flagElName)
                if singleGlobalVar.get("luaDriver") != None:
                    singleGlobalVar.get("luaDriver").keyevent(4)
            except:
                break
                print "该pop已关闭"
            cnt = cnt + 1

    def deletefile(self):
        driver = singleGlobalVar.get("luaDriver")
        self.platformLog("deleteFile  开始测试前删除配置及性能相关文件")
        config = ConfigHelper(constant.cfg_path)
        package = config.getValue('appium', 'apppackage')

        #正式服
        command = "shell rm -r /mnt/sdcard/." + package + "/dict/lastLoginInfo.dat"
        self.platformLog(command)
        try:
            driver.adb(command)
        except:
            self.platformLog("删除正式服上次登录配置文件失败")

        #预发布  1lastLoginInfo.dat
        command1 = "shell rm -r /mnt/sdcard/." + package + "/dict/1lastLoginInfo.dat"
        self.platformLog(command1)
        try:

            driver.adb(command1)
        except:
            self.platformLog("删除预发布1lastLoginInfo.dat失败")

        # 预发布  2lastLoginInfo.dat
        command2 = "shell rm -r /mnt/sdcard/." + package + "/dict/2lastLoginInfo.dat"
        self.platformLog(command2)
        try:
            driver.adb(command2)
        except:
            self.platformLog("删除预发布1lastLoginInfo.dat失败")

        #删除性能测试结果文件
        delCsvCmd = "shell rm -r /mnt/sdcard/" + package + "_result.csv"
        self.platformLog(delCsvCmd)
        try:
            driver.adb(delCsvCmd)
        except:
            self.platformLog("删除性能测试文件失败")

        #minicap截图文件夹
        delMinicapCmd = "shell rm -rf /mnt/sdcard/minicap_screen"
        self.platformLog(delMinicapCmd)
        try:
            driver.adb(delMinicapCmd)
        except:
            self.platformLog("删除minicap截图文件夹失败")

        mkdirCmd = "shell mkdir /mnt/sdcard/minicap_screen"
        self.platformLog(mkdirCmd)
        try:
            driver.adb(mkdirCmd)
        except:
            self.platformLog("新建minicap截图文件夹失败")

    def closeActivity(self):
        '''
        关闭进入大厅各种弹框
        :return:
        '''
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()

        #关闭首先出现的新手任务按钮
        try:
            self.hall_page.wait_element("新手任务", 5).click()
        except:
            print "未出现新手任务按钮"

        try:
            self.hall_page.wait_element("确认登陆", 5).click()
        except:
            print "未出现确认登陆按钮"

        try:
            self.hall_page.wait_element("立即升级绑定账号", 5).click()
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"

        #添加破产奖励领取,由于目前只能在正式服测试  导致所有接口不能用   先领取破产奖励,避免弹框对其他场景影响
        try:
            self.hall_page.wait_element("破产领奖按钮", 5).click()
            self.platformLog("领取破产奖励")
        except:
            print "没有破产按钮"

        self.closeActivityBtn(False)

    def closeActivityBtn(self, checkNewer=True):
        '''
        关闭活动弹框(同时切换服务器、重新登录进入大厅也是用该函数关闭活动弹框)
        @:param checkNewer 是否检测新手任务按钮
        :return:
        '''
        if checkNewer:
            try:
                self.hall_page.wait_element("新手任务", 5).click()
            except:
                print "未出现新手任务按钮"

        i = 0
        while (i < 5):
            i += 1
            try:
                self.sign_page.wait_element("关闭1", 2).click()
            except:
                print "sign_page关闭1按钮操作失败"

    def moveElement(self, startEl, endEl):
        '''
        滑动元素  将元素从位置1滑动到位置2  以元素中心位置进行滑动
        :param element1:  滑动开始元素名
        :param element2:  滑动结束元素名
        :return:
        '''
        swipe_startx = startEl.location['x'] + startEl.size['width'] / 2
        swipe_starty = startEl.location['y'] + startEl.size['height'] / 2

        swipe_endx = endEl.location['x'] + endEl.size['width'] / 2
        swipe_endy = endEl.location['y'] + endEl.size['height'] / 2
        singleGlobalVar.get("luaDriver").swipe(swipe_startx, swipe_starty,
                                               swipe_endx, swipe_endy, 1000)

    def swipeList(self, listEl, dir=EnumDirection.Up, devPos=20):
        '''
        滑动列表  以中心点进行滑动
        :param listEl: 需进行滑动的列表元素
        :param dir:滑动方向  EnumDirection.Up Down Left Right
        :param devPos:滑动偏移值
        :return:
        '''
        driver = singleGlobalVar.get("luaDriver")
        elLocation = listEl.location
        elSize = listEl.size
        startX = elLocation['x']
        centerX = elLocation['x'] + elSize['width'] / 2
        endX = elLocation['x'] + elSize['width'] - devPos
        startY = elLocation['y']
        centerY = elLocation['y'] + elSize['height'] / 2
        endY = elLocation['y'] + elSize['height'] - devPos
        if dir == EnumDirection.Up:
            driver.swipe(centerX, endY, centerX, startY)
        elif dir == EnumDirection.Down:
            driver.swipe(centerX, startY, centerX, endY)
        elif dir == EnumDirection.Left:
            driver.swipe(endX, centerY, startX, centerY)
        elif dir == EnumDirection.Right:
            driver.swipe(startX, centerY, endX, centerY)

        time.sleep(1)

    def closefloatBall(self):
        '''
        关闭浮动球,先判断浮动球是否显示中
        :return:
        '''
        try:
            self.hall_page.wait_element("同步标志")
            self.hall_page.get_element("设置").click()
            self.switchBtn("浮动球开关", "浮动球状态", True)
        except:
            self.platformLog("浮动球已关闭or关闭失败")
        finally:
            self.checkPopVisible(self.setting_page)

    def switchBtn(self, pName, cName, close):
        '''
        用于操作切换按钮(主要是设置框浮动球类按钮操作)
        若当前状态和要求一致,则操作切换元素两次  否则只点击一次
         @:param  pName:切换元素名
         @:param  cName:切换元素的按钮名  直接取【2】有问题  改为先取列表然后从list取index为2的元素
         @:param  close:是否需要关闭
        :return:
        '''
        # print "需要操作的开关名:"+pName + " 是否隐藏:"+ str(close)
        parentEl = None
        childEl = None
        isClose = False
        try:
            parentEl = self.setting_page.wait_element(pName)
        except:
            print "获取parentEl失败,退出切换按钮设置"
            return
        try:
            elList = self.setting_page.get_elements(cName)
            if len(elList) > 2:
                childEl = elList[2]
            else:
                return
        except:
            print "获取childEl失败,退出切换按钮设置"
            return

        parentX = parentEl.location['x']
        parentWidth = parentEl.size['width']
        childX = childEl.location['x']
        childWidth = childEl.size['width']

        if ((childX + childWidth / 2) < (parentX + parentWidth / 2)):
            isClose = True
        else:
            isClose = False
        if isClose == close:
            parentEl.click()
            time.sleep(0.5)
            parentEl.click()
            time.sleep(0.5)
        else:
            parentEl.click()
            time.sleep(0.5)

    #从26个大小写字母及数字从随机生成指定长度的字符串
    def random_str(self, len):
        '''生成随机字符'''
        str = ""
        resourceStr = string.ascii_letters + string.digits
        for i in range(len):
            str += (random.choice(resourceStr))
        return str

    def addmoney(self, mid):
        '''
        破产账号充值
        :return:
        '''
        user_info = PHPInterface.get_user_info(mid)  # 获取玩家信息
        coin = eval(user_info).get('result', {
            'coin': None
        }).get('coin')  # 获取当前银币值
        print coin
        AddMoney = 10000 - coin
        print AddMoney
        PHPInterface.add_money(mid, AddMoney)  # 将银币值设为60000

    def getResolution(self):
        '''
        获取分辨率
        :return: 直接返回的是minicap截图需要的分辨率参数 游戏会自动设置横屏,所以width>height
        '''
        if (self.resolution == None):
            screenWidth = singleGlobalVar.get(
                "luaDriver").get_window_size()['width']
            screenHeigth = singleGlobalVar.get(
                "luaDriver").get_window_size()['height']
            self.resolution = str(screenWidth) + "x" + str(
                screenHeigth) + "@" + str(screenWidth) + "x" + str(
                    screenHeigth)

        return self.resolution

    def taskScreenShot(self, fileName):
        '''
        由于appium截图效率问题,改为stf平台的minicap进行截图,其中-P如下:
        The format of the -P argument is: {RealWidth}x{RealHeight}@{VirtualWidth}x{VirtualHeight}/{Orientation}. The "virtual" size is the size of the desired projection.
        The orientation argument tells minicap what the current orientation of the device is (in degrees),
        可执行minicap命令:
        adb shell LD_LIBRARY_PATH=/data/local/tmp exec /data/local/tmp/minicap -P 720x1280@720x1280/90 -s >/sdcard/minicap_1504612944860.jpg
        :param page:  元素所在page页
        :param fileName: 截图保存的文件名
        :return:
        '''
        driver = singleGlobalVar.get("luaDriver")
        try:
            broadcastType = "com.boyaa.stf.screenShot"
            cmd_send = "shell am broadcast -a " + broadcastType + " --es sShotName " + fileName
            self.platformLog(cmd_send)
            driver.adb(cmd_send)
        except:
            self.platformLog("通知apk截图时间失败")

        try:
            cmd_pre = "shell LD_LIBRARY_PATH=/data/local/tmp exec /data/local/tmp/minicap -P "
            cmd_minicap = cmd_pre + self.getResolution(
            ) + "/0 -s >/mnt/sdcard/minicap_screen/" + fileName
            self.platformLog(cmd_pre)
            driver.adb(cmd_minicap)
        except:
            self.platformLog("调用minicap截图失败")

    @staticmethod
    def printStr(*info):
        '''
        打印log,自动将所有传入非字符串类型转换为字符串然后打印出来
        '''
        logStr = ""
        for v in info:
            if type(v) != str:
                v = str(v)
            logStr = logStr + v
        timeStamp = str(datetime.datetime.now())
        print timeStamp + " " + logStr

    def getResumeTime(self):
        '''
        获取游戏启动完成时间,目前是头像元素出现的时间
        :return:
        '''
        time.sleep(2)
        self.platformLog("-------------getResumeTime----------")
        try:
            self.hall_page.wait_element("启动完成标志", 180, 0.1)
            # utc = '%.2f' % (time.time())
            print "apk start completely"
            self.platformLog("apk start completely")
        except:
            self.platformLog("获取标志app成功成功元素失败")

    def startCollectApp(self):
        '''
        启动性能采集apk
        :return:
        '''
        self.platformLog("startCollectApp start pmonitor apk")
        config = ConfigHelper(constant.cfg_path)
        #性能采集apk acticity
        apkName = config.getValue('appium', 'collectApkName')
        # 被测apk packageName
        testAPKName = config.getValue('appium', 'apppackage')
        # 最终生成结果文件在手机中目录
        resultFilePath = "/mnt/sdcard/" + testAPKName + "_result.csv"

        # SDK 17后由于权限问题,需要加--user 0
        startCmd = "shell am start --user 0 -n " + apkName + " --es packageName " + testAPKName + " --es filePath " + resultFilePath
        self.platformLog(startCmd)
        singleGlobalVar.get("luaDriver").adb(startCmd)

    def platformLog(self, msg):
        logStr = ""
        for v in msg:
            if type(v) != str:
                v = str(v)
            logStr = logStr + v
        self.log.info(logStr)

    def sendTagBroadcast(self, tagName, tagType):
        '''
        向性能采集apk发送场景广播
        :param tagName:
        :param tagType:
        :return:
        '''
        #广播类型
        broadcastType = "com.boyaa.stf.UIScript"
        cmd_send = "shell am broadcast -a " + broadcastType + " --es tagName " + tagName + " --es tagType " + tagType
        self.platformLog(cmd_send)
        singleGlobalVar.get("luaDriver").adb(cmd_send)

    def sendStopServiceBroad(self):
        self.platformLog("sendStopServiceBroad")
        # 广播类型
        broadcastType = "com.boyaa.stf.stopService"
        cmd_send = "shell am broadcast -a " + broadcastType
        singleGlobalVar.get("luaDriver").adb(cmd_send)
Ejemplo n.º 3
0
class Common():
    def setupdriver(self, agrs={}):
        '''初始化driver
        luadriver 用于游戏操作,对应appium
        '''
        # 初始化Luadriver
        self.luaobj = LuaDriver()
        self.luaobj.creatLuaDriver(agrs)
        self.luadriver = self.luaobj.getLuaDriver()
        return self.luadriver

    def closedriver(self):
        '''关闭driver
        luadriver 用于游戏操作,对应appium_for_bebe
        nativedriver 用于原生应用操作,对应appium_hybrid_support
        '''
        self.luaobj.closeLuadriver()

    def closefloatBall(self):
        self.hall_page = Hall_Page()
        self.setting_page = Setting_Page()
        self.hall_page.wait_element("设置").click()
        time.sleep(2)
        try:
            self.setting_page.wait_element("关闭浮动球").click()
        except:
            print "浮动球已经关闭"
        time.sleep(3)
        self.setting_page.wait_element("页面返回").click()
        time.sleep(2)

    def changeServerView(self, name):
        self.hall_page = Hall_Page()
        self.hall_page.wait_element(name).click()
        time.sleep(5)

    def deletefile(self, driver):
        print "----删除自动登录文件----"
        config = ConfigHelper(constant.cfg_path)
        package = config.getValue('appium', 'apppackage')
        #正式服
        command = "shell rm -r /mnt/sdcard/." + package + "/dict/lastLoginInfo.dat"
        # command = "shell rm -r /mnt/sdcard/.com.boyaa.engineqpsc/dict/lastLoginInfo.dat"
        print command
        driver.adb(command)
        #预发布  1lastLoginInfo.dat
        command1 = "shell rm -r /mnt/sdcard/." + package + "/dict/1lastLoginInfo.dat"
        print command1
        driver.adb(command1)
        # 预发布  2lastLoginInfo.dat
        command2 = "shell rm -r /mnt/sdcard/." + package + "/dict/2lastLoginInfo.dat"
        driver.adb(command2)
        print command2

    def closeactivitytest(self, luadriver):
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(5)
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            "未出现登陆按钮"
        i = 0
        while (i < 3):
            i += 1
            try:
                self.sign_page.wait_element("关闭1").click()
                time.sleep(1)
            except:
                try:
                    self.sign_page.wait_element("关闭1").click()
                    time.sleep(1)
                except:
                    "关闭对话框"
        try:
            self.hall_page.wait_element("预发布").click()
        except:
            "未出现预发布按钮"
        time.sleep(15)
        i = 0
        while (i < 3):
            i += 1
            try:
                self.sign_page.wait_element("关闭1").click()
                time.sleep(1)
            except:
                try:
                    self.sign_page.wait_element("关闭1").click()
                    time.sleep(1)
                except:
                    "关闭对话框"

    def closeactivity(self, luadriver):
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            print "未出现按钮"
        time.sleep(15)
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            print "未出现登陆按钮"
        i = 0
        while (i < 3):
            i += 1
            try:
                self.sign_page.wait_element("关闭1").click()
                time.sleep(1)
            except:
                try:
                    self.sign_page.wait_element("关闭1").click()
                    time.sleep(1)
                except:
                    print "关闭对话框"
        try:
            self.hall_page.wait_element("正式服").click()
        except:
            print "未出现正式服按钮"
        time.sleep(15)
        i = 0
        while (i < 3):
            i += 1
            try:
                self.sign_page.wait_element("关闭1").click()
                time.sleep(1)
            except:
                try:
                    self.sign_page.wait_element("关闭1").click()
                    time.sleep(1)
                except:
                    print "关闭对话框"

    def closeactivity_switchserver(self, luadriver, switchserver):
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        time.sleep(10)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            print "未出现按钮"
        time.sleep(5)
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            print "未出现登陆按钮"
        i = 0
        while (i < 3):
            i += 1
            try:
                self.sign_page.wait_element("关闭1").click()
                time.sleep(1)
            except:
                try:
                    self.sign_page.wait_element("关闭1").click()
                    time.sleep(1)
                except:
                    print "关闭对话框"
        try:
            self.hall_page.wait_element("新手任务").click()
            time.sleep(2)
        except:
            print "未出现新手任务按钮"
        try:
            self.hall_page.wait_element(switchserver).click()
            time.sleep(1)
        except:
            print "未出现正式服按钮"
        time.sleep(13)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(2)
        i = 0
        while (i < 3):
            i += 1
            try:
                self.sign_page.wait_element("关闭1").click()
                time.sleep(1)
            except:
                try:
                    self.sign_page.wait_element("关闭1").click()
                    time.sleep(1)
                except:
                    print "关闭对话框"

    def switchnetwork(self, luadriver, network):
        '''
        测试用例运行过程中切换网络
        '''
        if (network == '无网络'):
            print "设置为无网络状态"
            luadriver.set_network_connection(ConnectionType.NO_CONNECTION)
            print luadriver.network_connection
        if (network == 'WIFI模式'):
            print "设置为WIFI模式"
            luadriver.set_network_connection(ConnectionType.WIFI_ONLY)
        if (network == '数据网络'):
            print "设置为数据网络模式"
            luadriver.set_network_connection(ConnectionType.DATA_ONLY)
        if (network == '飞行模式'):
            print "设置为飞行模式"
            luadriver.set_network_connection(ConnectionType.AIRPLANE_MODE)
        if (network == '全部网络打开模式'):
            print "设置为全部网络打开模式"
            luadriver.set_network_connection(ConnectionType.ALL_NETWORK_ON)

    def isloginuser(self, luadriver):
        '''
        判断是否是注册账号登陆,如果不是则切换到注册账号
        :param user:
        :param passwd:
        :return:
        '''
        self.login_page = Login_Page()
        self.setting_page = Setting_Page()
        self.hall_page = Hall_Page()
        # # 每个用例都需要关闭活动,把这个放在初始化里面实现
        # self.closeactivity(luadriver)
        self.hall_page.wait_element("头像").click()
        time.sleep(6)
        try:
            if (self.setting_page.wait_element("立即升级").get_attribute('text') !=
                    u"立即升级"):
                return True
            else:
                return False
        except:
            return False

    def loginuser(self, user, passwd):
        self.login_page = Login_Page()
        self.setting_page = Setting_Page()
        self.hall_page = Hall_Page()
        time.sleep(6)
        self.setting_page.wait_element("切换账号").click()
        time.sleep(2)
        try:
            self.setting_page.wait_element("继续登录").click()
        except:
            print "不需要继续切换"
        try:
            self.setting_page.wait_element("删除历史账号").click()
        except:
            print "无历史账号"
        time.sleep(1)
        self.setting_page.wait_element("手机号码").send_keys(user)
        time.sleep(1)
        self.setting_page.wait_element("密码").send_keys(passwd)
        time.sleep(1)
        self.setting_page.wait_element("确认登陆").click()
        time.sleep(10)
        self.hall_page.wait_element("同步标志")
        time.sleep(1)

    def swipeelement(self, element1, element2):
        # element1_size_width = element1.size['width']
        # element1_size_height = element1.size["height"]
        # element1_1_x = element1.location["x"]
        # element1_1_y = element1.location["y"]
        # swipe_startx = element1.location['x']+element1.size['width']/2
        # swipe_starty = element1.location['y']+element1.size['height']/2
        swipe_startx = element1.location['x']
        swipe_starty = element1.location['y']
        # swipe_endx = element2.location['x']+element1.size['width']/2
        # swipe_endy = element2.location['y']+element1.size['height']/2
        swipe_endx = element2.location['x']
        swipe_endy = element2.location['y']
        print swipe_startx, swipe_starty, swipe_endx, swipe_endy
        self.luadriver.swipe(swipe_startx, swipe_starty, swipe_endx,
                             swipe_endy, 1000)

    def restart(self):
        self.luaobj.closeLuadriver()
        self.luaobj.creatLuaDriver()
        self.luadriver = self.luaobj.getLuaDriver()
        self.closeactivity_switchserver(self.luadriver, "预发布")
        return self.luadriver

    def random_str(self, len):
        '''生成随机字符'''
        str = ""
        for i in range(len):
            str += (random.choice(
                "SDFJSDFJSDJFSDJF4234234SDFJSDFDSFJSADJFsfjdskjfkdsjfkdsjf428347832748327"
            ))
        return str

    def getdata(self, string):
        string1 = string.encode('gbk')
        print type(string1)
        data = filter(str.isdigit, string1)
        print data
        return data

    def closeactivityprepublish(self, luadriver):
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(5)
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            "未出现登陆按钮"
        i = 0
        while (i < 2):
            i += 1
            try:
                self.sign_page.wait_element("关闭1").click()
                time.sleep(1)
            except:
                try:
                    self.sign_page.wait_element("关闭1").click()
                    time.sleep(1)
                except:
                    "关闭对话框"
        try:
            self.hall_page.wait_element("新手任务").click()
            time.sleep(2)
        except:
            print "未出现新手任务按钮"
        try:
            self.hall_page.wait_element("预发布").click()
        except:
            "未出现预发布按钮"
        time.sleep(10)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(2)
        i = 0
        while (i < 2):
            i += 1
            try:
                self.sign_page.wait_element("关闭1").click()
                time.sleep(1)
            except:
                try:
                    self.sign_page.wait_element("关闭1").click()
                    time.sleep(1)
                except:
                    "关闭对话框"

    def game_is_exist(self, gamename):
        '''
        判断子游戏是否存在,存在返回True,不存在返回False
        :param gamename: 子游戏元素名,不查找“更多游戏”
        '''
        self.game_page = Game_Page()
        if (self.game_page.is_exist(gamename) == True):
            return True
        else:
            try:
                self.game_page.wait_element("右三角标").click()
                time.sleep(3)
                if (self.game_page.is_exist(gamename) == True):
                    return True
            except:
                print "无此按钮"
            if (self.game_page.is_exist("更多游戏") == True):
                try:
                    self.game_page.wait_element("左三角标").click()
                    time.sleep(3)
                    if (self.game_page.is_exist(gamename) == True):
                        return True
                except:
                    print "无此按钮"
            return False

    def game_is_download(self):
        '''
        判断子游戏是否已下载
        '''
        self.game_page = Game_Page()
        if (self.game_page.is_exist("资源下载-确定") == True):
            self.game_page.wait_element("资源下载-确定").click()
            time.sleep(20)
        else:
            print("游戏已下载")