Example #1
0
 def get_game_list(self):
     '''
     获取当前界面子游戏列表名
     :return: 游戏列表名
     '''
     self.game_page = Game_Page()
     luaobj = LuaDriver()
     luadriver = luaobj.getLuaDriver()
     config = ConfigHelper(constant.cfg_path)
     game_id = config.getValue("casecfg", "game_id")
     game_id = game_id.rstrip("|")
     # print game_id
     gamelist = game_id.split("|")
     print gamelist
     game_list = []
     # print game_id
     if game_id == 'undefined':
         elements = self.game_page.get_elements("子游戏列表")
         # print len(elements)
         #去重,同时去除game0(约牌,比赛场)和game-1(更多游戏)
         elementsblack = ["game0", "game-1"]
         for i in range(len(elements)):
             name = elements[i].get_attribute("name")
             if name not in elementsblack:
                 elementsblack.append(name)
                 print name
                 game_list.append(elements[i])
         # print len(game_list)
     else:
         #gameid,添加"game"
         # print len(gamelist)
         for i in range(len(gamelist)):
             print "game" + gamelist[i]
             gameid = "game" + gamelist[i]
             try:
                 time.sleep(1)
                 elements1 = luadriver.find_lua_element_by_name(gameid)
                 print elements1.get_attribute("name")
                 game_list.append(elements1)
             except:
                 print "找不到元素 %s" % gamelist[i]
     print game_list
     return game_list
Example #2
0
class Test01(TestCase):
    '''
    recorder 
    '''
    owner = "recorder"
    status = EnumStatus.Design
    priority = EnumPriority.High
    timeout = 5
    def pre_test(self):
        self.luaobj = LuaDriver()
        self.luaobj.creatLuaDriver()
        self.luadriver = self.luaobj.getLuaDriver()

    def post_test(self):
        '''
        测试用例执行完成后,清理测试环境
        '''
        self.luaobj.closeLuadriver()
    def run_test(self):
Example #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
        '''
        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)
        try:
            self.setting_page.wait_element("页面返回").click()

        except:
            print "已经关闭"
        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
        try:
            driver.adb(command)
        except:
            print "命令执行失败"
        #预发布  1lastLoginInfo.dat
        command1 = "shell rm -r /mnt/sdcard/." + package + "/dict/1lastLoginInfo.dat"
        print command1
        try:

            driver.adb(command1)
        except:
            print "命令执行失败"
        # 预发布  2lastLoginInfo.dat
        command2 = "shell rm -r /mnt/sdcard/." + package + "/dict/2lastLoginInfo.dat"
        try:
            driver.adb(command2)
        except:
            print "命令执行失败"
        print command2

    def closeactivity_switchserver(self, luadriver, switchserver):
        '''
        关闭活动页面,切换到指定服,然后再关闭弹出的活动页面
        :param luadriver:
        :param switchserver:
        :return:
        '''
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        time.sleep(8)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            print "未出现按钮"
        try:
            luadriver.find_element_by_tag_name("允许").click()
        except:
            print "未出现按钮"
        try:
            luadriver.find_elements_by_class_name(
                "android.widget.Button")[1].click()
        except:
            print "未出现按钮"
        time.sleep(10)
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            print "未出现登陆按钮"
        try:
            self.hall_page.wait_element("立即升级绑定账号").click()
            time.sleep(1)
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"
        self.closeActivityBtn()
        try:
            self.hall_page.wait_element("新手任务").click()
            time.sleep(2)
        except:
            print "未出现新手任务按钮"
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            print "未出现按钮"
        while (self.hall_page.is_exist("switchserver")):
            try:
                self.hall_page.wait_element(switchserver).click()
                time.sleep(1)
            except:
                print "未出现正式服按钮"
            time.sleep(15)
            try:
                luadriver.find_element_by_name("允许").click()
            except:
                print "未出现允许按钮"
            try:
                self.hall_page.wait_element("立即升级绑定账号").click()
                time.sleep(1)
                self.sign_page.wait_element("关闭1").click()
            except:
                print "未出现立即升级绑定账号按钮"
            time.sleep(2)
            self.closeActivityBtn()

    def closeActivityBtn(self):
        '''
        关闭活动弹框
        :return:
        '''
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        i = 0
        while (i < 4):
            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_reservenotice(self, luadriver,
                                                 switchserver):  #切换服务器但保留公告页面
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        time.sleep(15)
        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:
            luadriver.find_element_by_name("允许").click()
        except:
            print "未出现按钮"
        try:
            self.hall_page.wait_element(switchserver).click()
            time.sleep(1)
        except:
            print "未出现%s按钮" % switchserver
        time.sleep(15)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(2)

    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 = LuaDriver()
        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("32423423324324234234234234343534532423432"))
        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()
            time.sleep(1)
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            "未出现登陆按钮"
        self.closeActivityBtn()
        try:
            self.hall_page.wait_element("新手任务").click()
            time.sleep(2)
        except:
            print "未出现新手任务按钮"
        try:
            self.hall_page.wait_element("预发布").click()
        except:
            "未出现预发布按钮"
        time.sleep(5)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(5)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(2)
        try:
            self.hall_page.wait_element("立即升级绑定账号").click()
            time.sleep(1)
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"
        self.closeActivityBtn()

    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 get_user(self):
        '''
        获取账号信息
        @return:
        '''
        global user_info
        user_info = user_util.gimme_afree_account()
        while user_info == None:
            time.sleep(5)
            user_info = user_util.gimme_afree_account()
        return user_info

    def release_user(self, user_mid):
        print "release"
        print user_mid
        try:
            user_util.release_account(user_mid)
        except:
            print "release user fail"

    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(40)
        else:
            print("游戏已下载")

    def get_cid(self):
        '''获取用户cid'''
        self.personinfo_page = Personinfo_Page()
        self.game_page = Game_Page()
        # self.start_step("获取用户mid")
        self.personinfo_page.wait_element("头像").click()
        time.sleep(6)
        cid = self.personinfo_page.wait_element("账号ID").get_attribute('text')
        print "获取的用户cid为: %s" % cid
        self.game_page.wait_element("关闭对话框").click()
        time.sleep(3)
        if (self.game_page.is_exist("关闭对话框")):  # 如果弹破产弹框,则关闭
            self.game_page.wait_element("关闭对话框").click()
            time.sleep(3)
        return cid

    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 get_safebox_money(self):
        '''从保险箱取出全部存款'''
        self.safebox_page = Safebox_Page()
        # self.start_step("从保险箱取出所有存款")
        self.hall_page.wait_element("保险箱").click()
        time.sleep(2)
        self.safebox_page.wait_element("取出").click()
        if (self.safebox_page.is_exist("确定---保险箱")):
            slider = self.safebox_page.wait_element("滚动条")
            addgoldbtn = self.safebox_page.wait_element("增加金条/银条数目")
            x = slider.location['x']
            y = slider.location['y']
            x1 = addgoldbtn.location['x']
            y1 = addgoldbtn.location['y']
            self.luadriver.swipe(x, y, x1, y1)
            self.safebox_page.wait_element("确定---保险箱").click()
            time.sleep(2)
            self.luadriver.keyevent(4)
        else:
            print("保险箱没有银币存款")
            time.sleep(2)
            self.luadriver.keyevent(4)

    def get_safebox_crystal(self):
        '''从保险箱取出全部金条'''
        self.safebox_page = Safebox_Page()
        self.hall_page.wait_element("保险箱").click()
        time.sleep(2)
        self.safebox_page.wait_element("金条保险箱").click()
        time.sleep(2)
        self.safebox_page.wait_element("取出").click()
        if (self.safebox_page.is_exist("确定---保险箱")):
            slider = self.safebox_page.wait_element("滚动条")
            addgoldbtn = self.safebox_page.wait_element("增加金条/银条数目")
            x = slider.location['x']
            y = slider.location['y']
            x1 = addgoldbtn.location['x']
            y1 = addgoldbtn.location['y']
            self.luadriver.swipe(x, y, x1, y1)
            self.safebox_page.wait_element("确定---保险箱").click()
            time.sleep(2)
            self.luadriver.keyevent(4)
        else:
            print("保险箱没有金条存款")
            self.luadriver.keyevent(4)
Example #4
0
class Common():
    def __init__(self):
        self.log = Logger().get_logger()

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

    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
        '''
        self.luaobj.closeLuadriver()

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

    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"
        print command
        try:
            driver.adb(command)
        except:
            print "命令执行失败"
        command1 = "shell rm -r /mnt/sdcard/."+package+"/dict/1lastLoginInfo.dat"
        print command1
        try:

            driver.adb(command1)
        except:
            print "命令执行失败"
        command2 = "shell rm -r /mnt/sdcard/."+package+"/dict/2lastLoginInfo.dat"
        try:
            driver.adb(command2)
        except:
            print "命令执行失败"
        print command2

    def closeactivity(self,luadriver):
        '''
       关闭活动页面
       '''
        self.hall_page = Hall_Page()
        time.sleep(20)
        # try:
        #     if(self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")')!=None):
        #         self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")').click()
        # except:
        #     print "未出现允许按钮"

        self.hall_page.wait_element("头像",90)
        # try:
        #     if(self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")')!=None):
        #         self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")').click()
        # except:
        #     print "未出现允许按钮"
        try:
            self.hall_page.wait_element("新手任务",8).click()
        except:
            print "未出现新手任务按钮"
        try:
            self.hall_page.wait_element("确认登陆",2).click()
        except:
            print "未出现登陆按钮"
        try:
            self.hall_page.wait_element("立即升级绑定账号",2).click()
            self.sign_page.wait_element("关闭1",2).click()
        except:
            print "未出现立即升级绑定账号按钮"

        #2017-9-27 JessicZeng  增加关闭版本升级框
        try:
            self.hall_page.wait_element("资源下载-取消",2).click()
        except:
            print "未出现版本升级框"


        self.closeActivityBtn()
        # try:
        #     luadriver.find_element_by_name("允许").click()
        # except:
        #     print "未出现按钮"


    def closeactivity_switchserver(self,luadriver):
        '''
        关闭活动页面,切换到指定服,然后再关闭弹出的活动页面
        :param luadriver:
        :param switchserver:
        :return:
        '''
        self.closeactivity(luadriver)
        self.switchserver_init()
        self.closeactivity(luadriver)

    def switchserver(self):
        '''
        根据cfg文件切换正式服,测试服,预发布服
        @return:
        '''
        self.hall_page = Hall_Page()
        env = self.get_config_value('casecfg', 'env')
        if env=='0':
            try:
                self.hall_page.wait_element("正式服").click()
            except:
                print "切换到正式服失败"
        elif env =='1':
            try:
                self.hall_page.wait_element("测试服").click()
            except:
                print "切换到测试服失败"
        elif env =='2':
            try:
                self.hall_page.wait_element("预发布").click()
            except:
                print "切换到预发布失败"
        else:
            try:
                self.hall_page.wait_element("预发布").click()
            except:
                print "切换到预发布服失败"
    time.sleep(5)

    def switchserver_init(self):
        '''
        根据cfg文件切换正式服,测试服,预发布服
        @return:
        '''
        self.hall_page = Hall_Page()
        env = self.get_config_value('casecfg', 'env')
        if self.hall_page.element_is_exist("测试按钮"):
            self.hall_page.wait_element("测试按钮").click()
            if self.hall_page.element_is_exist("切换环境"):
                self.hall_page.wait_element("切换环境").click()
                if env == '0':
                    try:
                        # while self.hall_page.element_is_exist("正式1", 2):
                            self.hall_page.get_element("正式1").click()
                    except:
                        print "切换到正式服失败"
                elif env == '1':
                    try:
                        # while self.hall_page.element_is_exist("测试1",2):
                            self.hall_page.get_element("测试1").click()
                    except:
                        print "切换到测试服失败"
                elif env == '2':
                    try:
                        # while self.hall_page.element_is_exist("预发布1", 2):
                            self.hall_page.get_element("预发布1").click()
                    except:
                        print "切换到预发布失败"
                else:
                    try:
                        # while self.hall_page.element_is_exist("预发布1", 2):
                            self.hall_page.get_element("预发布1").click()

                    except:
                        print "切换到预发布服失败"
            else:
                self.hall_page.wait_element("关闭测试页面").click()
                if env=='0':
                    try:
                        self.hall_page.wait_element("正式服").click()
                    except:
                        print "切换到正式服失败"
                elif env =='1':
                    try:
                        self.hall_page.wait_element("测试服").click()
                    except:
                        print "切换到测试服失败"
                elif env =='2':
                    try:
                        self.hall_page.wait_element("预发布").click()
                    except:
                        print "切换到预发布失败"
                else:
                    try:
                        self.hall_page.wait_element("预发布").click()
                    except:
                        print "切换到预发布服失败"
            time.sleep(15)

    def closeActivityBtn(self):
        '''
        关闭活动弹框
        :return:
        '''
        self.sign_page = Sign_Page()
        self.hall_page = Hall_Page()
        try:
            self.hall_page.wait_element("新手任务",6).click()
        except:
            print "未出现新手任务按钮"
        i = 0
        while (i < 5):
            i += 1
            try:
                self.sign_page.wait_element("关闭1",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']+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 get_user(self):
        '''
        获取账号信息
        '''
        global user_info
        user_info = user_util.gimme_afree_account()
        while user_info ==None:
            time.sleep(5)
            user_info = user_util.gimme_afree_account()
        return user_info

    def release_user(self,user):
        '''
        释放用户
        '''
        print "release"
        print user
        try:
            user_util.release_account(user)
        except:
            print "release user fail"

    def get_cid(self):
        '''获取用户cid'''
        self.personinfo_page = Personinfo_Page()
        self.personinfo_page.wait_element("头像").click()
        cid = self.personinfo_page.wait_element("账号ID").get_attribute('text')
        print "获取的用户cid为: %s" % cid
        self.personinfo_page.wait_element("关闭").click()
        if (self.personinfo_page.element_is_exist("关闭")):  # 如果弹破产弹框,则关闭
            self.personinfo_page.wait_element("关闭").click()
        return cid

    def get_mid(self):
        cid = self.get_cid()
        time.sleep(3)
        config = ConfigHelper(constant.cfg_path)
        region = config.getValue('casecfg', 'region')
        mid = PHPInterface.get_mid(cid, region)
        print "用户mid为:%s" % mid
        return mid

    def set_coin(self,mid,value):
        # 获取用户银币信息
        result_userinfo = PHPInterface.get_user_info(mid)
        myuser_info = json.loads(result_userinfo)
        coin = myuser_info.get('result', {'coin': None}).get('coin')
        AddMoney = int(value) - int(coin)
        result = PHPInterface.add_money(mid, AddMoney)
        print "用户银币数为:%s" % AddMoney,"加金币执行结果:",result

    def set_crystal(self,mid,value):
        # 设置金条数据
        result_userinfo = PHPInterface.get_user_info(mid)
        myuser_info = json.loads(result_userinfo)
        crystal = myuser_info.get('result', {'crystal': None}).get('crystal')  # 获取当前金条值
        print "用户金条数为:%s" % crystal
        AddMoney = int(value) - int(crystal)
        PHPInterface.add_crystal(mid, AddMoney)

    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 get_idle_userinfo_and_mid(self):
        '''
        获取一个空闲账号的mid
        :return:
        '''
        user_info = self.get_user()
        print user_info
        UserCID = user_info.get('cid')
        config = ConfigHelper(cfg_path)
        region = config.getValue('casecfg', 'region')
        UserMID = PHPInterface.get_mid(UserCID, region)
        mid_and_userinfo = {}
        mid_and_userinfo['userinfo'] = user_info
        mid_and_userinfo['mid'] = UserMID
        return mid_and_userinfo

    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 recover_user(self,mid):
        #初始化用户
        self.set_coin(mid,'10000')
        self.set_crystal(mid,"0")
        PHPInterface.set_vip(mid,"-1")
        self.set_safeBoxMoney(mid,0,0)
        self.set_safeBoxMoney(mid,0,1)

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

    def set_safeBoxMoney(self,mid, money, money_type):
        #money_type=0银币,1为金条
        safeBoxInfo = PHPInterface.get_safebox(mid)
        if money_type == 0:
            safeBoxMoney=int(safeBoxInfo.get("safebox"))
        else:
            safeBoxMoney = int(safeBoxInfo.get("crystalsafebox"))
                
        if safeBoxMoney - money > 0:
            PHPInterface.withdraw_safebox(mid, safeBoxMoney - money, money_type)
            if money_type ==0:
                PHPInterface.add_money(mid, -(safeBoxMoney - money))
            else:
                PHPInterface.add_crystal(mid, -(safeBoxMoney - money))            
        else:
            if money_type ==0:
                PHPInterface.add_money(mid, money - safeBoxMoney)
            else:
                PHPInterface.add_crystal(mid, money - safeBoxMoney)
            
            PHPInterface.deposit_safebox(mid, money - safeBoxMoney, money_type)

    def room_set_cheat(self, game_list, switch=1):
        for i in range(len(game_list)):
            gameId = filter(lambda ch: ch in '0123456789', game_list[i].get_attribute("name"))
            if switch == 1:
                print "设置子游戏:%s 标准场为作弊场" % gameId
            else:
                print "设置子游戏:%s 标准场为非作弊场" % gameId
            for i in range(4):
                PHPInterface.set_cheat(gameid=gameId, playmode=i, switch=switch)

    def set_robot(self,gameid,level=12,robotflag=0):
        '''
        设置机器人开关
        '''
        gamecfg = PHPInterface.get_levelconfig(gameid, 0, 0, level)
        robotflag1 = gamecfg.get('values', {'ADDROBOTFLAG': None}).get('ADDROBOTFLAG')
        if (robotflag1 != robotflag):
            result = PHPInterface.set_robot_flag(gameid, 0, 0, level, robotflag=robotflag)
            print result
        return True

    def checkPopVisible(self,driver,page,flagElName="同步标志"):
        '''
        根据page同步标志判断弹框是否存在,直至元素不存在或者操作次数操作3次
        :param page: 操作页
        :param flagElName: 同步标志元素name
        :return:
        '''
        cnt = 0
        while cnt < 3:
            try:
                page.get_element(flagElName)
                if driver != None :
                    driver.keyevent(4)
                    print "关闭弹框",page
            except:
                break
            cnt = cnt + 1
Example #5
0
class Common():
    def __init__(self):
        self.log = Logger().get_logger()
        self.resolution = None
        self.orientation = None

    def setupdriver(self, agrs={}):
        '''初始化driver
        全局共用driver  其余地方都是通过new LuaDriver().getgetLuaDriver()获取
        luadriver 用于游戏操作,对应appium
        '''
        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):
        '''
        关闭活动页面,切换到指定服,然后再关闭弹出的活动页面
        :param switchserver:
        :return:
        '''
        driver = singleGlobalVar.get("luaDriver")
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()

        #性能测试中启动完成后才开始关闭活动弹框,sleep可省略
        # time.sleep(8)
        try:
            driver.find_element_by_name("允许").click()
        except:
            print "未出现允许按钮"
        try:
            driver.find_element_by_tag_name("允许").click()
        except:
            print "未出现允许按钮"
        try:
            driver.find_elements_by_class_name(
                "android.widget.Button")[1].click()
        except:
            print "未出现android.widget.Button按钮"
        time.sleep(10)
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            print "未出现确认登陆按钮"
        try:
            self.hall_page.wait_element("立即升级绑定账号").click()
            time.sleep(1)
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"
        #添加破产奖励领取,由于目前只能在正式服测试  导致所有接口不能用   先领取破产奖励,避免弹框对其他场景影响
        try:
            self.hall_page.wait_element("破产领奖按钮").click()
            self.platformLog("领取破产奖励")
        except:
            print "没有破产按钮"

        self.closeActivityBtn()
        try:
            self.hall_page.wait_element("新手任务").click()
            time.sleep(2)
        except:
            print "未出现新手任务按钮"
        try:
            driver.find_element_by_name("允许").click()
        except:
            print "未出现允许按钮"

    def closeActivityBtn(self):
        '''
        关闭活动弹框
        :return:
        '''
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        i = 0
        while (i < 4):
            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 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)

    #从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:
        '''
        try:
            driver = singleGlobalVar.get("luaDriver")
            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(5)
        self.platformLog("-------------getResumeTime----------")
        hallPage = Hall_Page()
        try:
            hallPage.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")
        config = ConfigHelper(constant.cfg_path)
        # 广播类型
        broadcastType = "com.boyaa.stf.stopService"
        cmd_send = "shell am broadcast -a " + broadcastType
        singleGlobalVar.get("luaDriver").adb(cmd_send)
Example #6
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
        '''
        self.luaobj.closeLuadriver()

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

    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
        try:
            driver.adb(command)
        except:
            print "命令执行失败"
        #预发布  1lastLoginInfo.dat
        command1 = "shell rm -r /mnt/sdcard/."+package+"/dict/1lastLoginInfo.dat"
        print command1
        try:

            driver.adb(command1)
        except:
            print "命令执行失败"
        # 预发布  2lastLoginInfo.dat
        command2 = "shell rm -r /mnt/sdcard/."+package+"/dict/2lastLoginInfo.dat"
        try:
            driver.adb(command2)
        except:
            print "命令执行失败"
        print command2

    def closeactivity(self,luadriver):
        '''
       关闭活动页面
       '''
        self.hall_page = Hall_Page()
        try:
            if(self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")')!=None):
                self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")').click()
        except:
            print "未出现允许按钮"
        self.hall_page.wait_element("头像",90)
        self.hall_page.wait_element("头像", 90)
        try:
            if(self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")')!=None):
                self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")').click()
        except:
            print "未出现允许按钮"
        try:
            self.hall_page.wait_element("新手任务").click()
        except:
            print "未出现新手任务按钮"
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            print "未出现登陆按钮"
        try:
            self.hall_page.wait_element("立即升级绑定账号").click()
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"
        self.closeActivityBtn()
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            print "未出现按钮"

    def closeactivity_switchserver(self,luadriver):
        '''
        关闭活动页面,切换到指定服,然后再关闭弹出的活动页面
        :param luadriver:
        :param switchserver:
        :return:
        '''
        self.closeactivity(luadriver)
        self.switchserver()
        self.closeactivity(luadriver)

    def switchserver(self):
        '''
        根据cfg文件切换正式服,测试服,预发布服
        @return:
        '''
        self.hall_page = Hall_Page()
        config=ConfigHelper(constant.cfg_path)
        env = config.getValue('casecfg', 'env')
        if env=='0':
            try:
                self.hall_page.wait_element("正式服").click()
            except:
                print "切换到正式服失败"
        elif env =='1':
            try:
                self.hall_page.wait_element("测试服").click()
            except:
                print "切换到测试服失败"
        elif env =='2':
            try:
                self.hall_page.wait_element("预发布").click()
            except:
                print "切换到预发布失败"
        else:
            try:
                self.hall_page.wait_element("预发布").click()
            except:
                print "切换到预发布失败"

    def closeActivityBtn(self):
        '''
        关闭活动弹框
        :return:
        '''
        self.sign_page = Sign_Page()
        i = 0
        while (i < 4):
            i += 1
            try:
                self.sign_page.wait_element("关闭1",8).click()
            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 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']+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 get_user(self):
        '''
        获取账号信息
        @return:
        '''
        global user_info
        user_info = user_util.gimme_afree_account()
        while user_info ==None:
            time.sleep(5)
            user_info = user_util.gimme_afree_account()
        return user_info

    def release_user(self,user_mid):
        print "release"
        print user_mid
        try:
            user_util.release_account(user_mid)
        except:
            print "release user fail"

    def get_cid(self):
        '''获取用户cid'''
        self.personinfo_page = Personinfo_Page()
        self.personinfo_page.wait_element("头像").click()
        cid = self.personinfo_page.wait_element("账号ID").get_attribute('text')
        print "获取的用户cid为: %s" % cid
        self.personinfo_page.wait_element("关闭").click()
        if (self.personinfo_page.element_is_exist("关闭")):  # 如果弹破产弹框,则关闭
            self.personinfo_page.wait_element("关闭").click()
        return cid

    def get_mid(self):
        cid = self.get_cid()
        time.sleep(3)
        config = ConfigHelper(constant.cfg_path)
        region = config.getValue('casecfg', 'region')
        mid = PHPInterface.get_mid(cid, region)
        print "用户mid为:%s" % mid
        return mid

    def set_coin(self,mid,value):
        # 获取用户银币信息
        result_userinfo = PHPInterface.get_user_info(mid)
        myuser_info = json.loads(result_userinfo)
        coin = myuser_info.get('result', {'coin': None}).get('coin')
        print "用户银币数为:%s" % coin
        print value
        AddMoney = int(value) - int(coin)
        PHPInterface.add_money(mid, AddMoney)

    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 get_idle_userinfo_and_mid(self):
        '''
        获取一个空闲账号的mid
        :return:
        '''
        user_info = self.get_user()
        print user_info
        UserCID = user_info.get('cid')
        config = ConfigHelper(cfg_path)
        region = config.getValue('casecfg', 'region')
        UserMID = PHPInterface.get_mid(UserCID, region)
        mid_and_userinfo = {}
        mid_and_userinfo['userinfo'] = user_info
        mid_and_userinfo['mid'] = UserMID
        return mid_and_userinfo

    def unlock(self):
        self.luadriver.adb("shell pm clear com.example.unlock")
        self.luadriver.adb("shell am start -n com.example.unlock/.Unlock")
        time.sleep(3)
        print "home键"
        self.luadriver.keyevent(3)  # home
        # self.luadriver.adb("shell am start -n com.example.unlock/.Unlock")
        # time.sleep(3)
        # self.luadriver.keyevent(3)  # home
        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):
        config = ConfigHelper(constant.cfg_path)
        value =config.getValue(section=section, key=key)
        return value

    def set_config_value(self,section,key,value):
        config = ConfigHelper(constant.cfg_path)
        config.modifConfig(section=section, key=key,value=value)
        return True

    def set_crystal(self,mid,value):
        # 设置金条数据
        result_userinfo = PHPInterface.get_user_info(mid)
        myuser_info = json.loads(result_userinfo)
        crystal = myuser_info.get('result', {'crystal': None}).get('crystal')  # 获取当前金条值
        print "用户金条数为:%s" % crystal
        AddMoney = int(value) - int(crystal)
        PHPInterface.add_crystal(mid, AddMoney)

    def recover_user(self,mid):
        #初始化用户
        self.set_coin(mid,'10000')
        self.set_crystal(mid,"0")
        PHPInterface.set_vip(mid,"-1")
Example #7
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 "浮动球已经关闭"
        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 "----删除自动登录文件----"
        #正式服
        command = "shell rm -r /mnt/sdcard/.com.boyaa.engineqpsc/dict/lastLoginInfo.dat"
        driver.adb(command)
        #测试服  1lastLoginInfo.dat
        command1 = "shell rm -r /mnt/sdcard/.com.boyaa.engineqpsc/dict/1lastLoginInfo.dat"
        driver.adb(command1)
        print command1

    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 < 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()
        except:
            "未出现测试服按钮"
        time.sleep(15)
        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 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 < 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:
                    print "关闭对话框"
        try:
            self.hall_page.wait_element("正式服").click()
        except:
            print "未出现正式服按钮"
        time.sleep(15)
        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:
                        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)
        print self.setting_page.wait_element("安全绑定").get_attribute('text')
        if(self.setting_page.wait_element("安全绑定").get_attribute('text') != u"安全绑定"):
            return True
        else:
            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_endx = element2.location['x']+element1.size['width']/2
        swipe_endy = element2.location['y']+element1.size['height']/2
        print element1_size_width,element1_size_height,element1_1_x,element1_1_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(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()
        except:
            "未出现预发布按钮"
        time.sleep(10)
        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:
                    "关闭对话框"
Example #8
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
        '''
        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)
        try:
            self.setting_page.wait_element("页面返回").click()

        except:
            print "已经关闭"
        time.sleep(2)

    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
        try:
            driver.adb(command)
        except:
            print "命令执行失败"
        #预发布  1lastLoginInfo.dat
        command1 = "shell rm -r /mnt/sdcard/."+package+"/dict/1lastLoginInfo.dat"
        print command1
        try:

            driver.adb(command1)
        except:
            print "命令执行失败"
        # 预发布  2lastLoginInfo.dat
        command2 = "shell rm -r /mnt/sdcard/."+package+"/dict/2lastLoginInfo.dat"
        try:
            driver.adb(command2)
        except:
            print "命令执行失败"
        print command2

    def closeactivity(self,luadriver):
        '''
       关闭活动页面
       '''
        self.hall_page = Hall_Page()
        time.sleep(1)
        # try:
        #     if(self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")')!=None):
        #         self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")').click()
        # except:
        #     print "未出现允许按钮"

        self.hall_page.wait_element("头像",90)
        # try:
        #     if(self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")')!=None):
        #         self.luadriver.find_element_by_android_uiautomator('new UiSelector().textMatches("允许")').click()
        # except:
        #     print "未出现允许按钮"
        try:
            self.hall_page.wait_element("新手任务",6).click()
        except:
            print "未出现新手任务按钮"
        try:
            self.hall_page.wait_element("确认登陆",6).click()
        except:
            print "未出现登陆按钮"
        try:
            self.hall_page.wait_element("立即升级绑定账号",6).click()
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"
        self.closeActivityBtn()
        # try:
        #     luadriver.find_element_by_name("允许").click()
        # except:
        #     print "未出现按钮"


    def closeactivity_switchserver(self,luadriver):
        '''
        关闭活动页面,切换到指定服,然后再关闭弹出的活动页面
        :param luadriver:
        :param switchserver:
        :return:
        '''
        self.closeactivity(luadriver)
        self.switchserver()
        self.closeactivity(luadriver)

    def switchserver(self):
        '''
        根据cfg文件切换正式服,测试服,预发布服
        @return:
        '''
        self.hall_page = Hall_Page()
        env = self.get_config_value('casecfg', 'env')
        if self.hall_page.element_is_exist("测试按钮"):
            self.hall_page.wait_element("测试按钮").click()
            if self.hall_page.element_is_exist("切换环境"):
                self.hall_page.wait_element("切换环境").click()
                if env == '0':
                    try:
                        while self.hall_page.element_is_exist("正式1", 2):
                            self.hall_page.get_element("正式1").click()
                    except:
                        print "切换到正式服失败"
                elif env == '1':
                    try:
                        while self.hall_page.element_is_exist("测试1",2):
                            self.hall_page.get_element("测试1").click()
                    except:
                        print "切换到测试服失败"
                elif env == '2':
                    try:
                        while self.hall_page.element_is_exist("预发布1", 2):
                            self.hall_page.get_element("预发布1").click()
                    except:
                        print "切换到预发布失败"
                else:
                    try:
                        while self.hall_page.element_is_exist("预发布1", 2):
                            self.hall_page.get_element("预发布1").click()
                    except:
                        print "切换到预发布服失败"
            else:
                self.hall_page.wait_element("关闭测试页面").click()
                if env=='0':
                    try:
                        self.hall_page.wait_element("正式服").click()
                    except:
                        print "切换到正式服失败"
                elif env =='1':
                    try:
                        self.hall_page.wait_element("测试服").click()
                    except:
                        print "切换到测试服失败"
                elif env =='2':
                    try:
                        self.hall_page.wait_element("预发布").click()
                    except:
                        print "切换到预发布失败"
                else:
                    try:
                        self.hall_page.wait_element("预发布").click()
                    except:
                        print "切换到预发布服失败"
            time.sleep(5)

    def closeActivityBtn(self):
        '''
        关闭活动弹框
        :return:
        '''
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        i = 0
        while (i < 4):
            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_reservenotice(self,luadriver,switchserver):#切换服务器但保留公告页面
        self.hall_page = Hall_Page()
        self.sign_page = Sign_Page()
        time.sleep(15)
        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:
            luadriver.find_element_by_name("允许").click()
        except:
            print "未出现按钮"
        try:
            self.hall_page.wait_element(switchserver).click()
            time.sleep(1)
        except:
            print "未出现%s按钮"%switchserver
        time.sleep(15)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(2)

    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 = LuaDriver()
        self.luaobj.closeLuadriver()
        self.luaobj.creatLuaDriver()
        self.luadriver = self.luaobj.getLuaDriver()
        self.closeActivityBtn()
        return self.luadriver

    def random_str(self,len):
        '''生成随机字符'''
        str = ""
        for i in range(len):
            str += (random.choice("safsdfsdfoewrweorewcvmdfadfdsafdskafaklvoreiutwuerpmvcmvasieqwoejandfsxmndje"))
        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()
            time.sleep(1)
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        try:
            self.hall_page.wait_element("确认登陆").click()
        except:
            "未出现登陆按钮"
        self.closeActivityBtn()
        try:
            self.hall_page.wait_element("新手任务").click()
            time.sleep(2)
        except:
            print "未出现新手任务按钮"
        try:
            self.hall_page.wait_element("预发布").click()
        except:
            "未出现预发布按钮"
        time.sleep(5)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(5)
        try:
            luadriver.find_element_by_name("允许").click()
        except:
            "未出现按钮"
        time.sleep(2)
        try:
            self.hall_page.wait_element("立即升级绑定账号").click()
            time.sleep(1)
            self.sign_page.wait_element("关闭1").click()
        except:
            print "未出现立即升级绑定账号按钮"
        self.closeActivityBtn()

    def get_user(self):
        '''
        获取账号信息
        @return:
        '''
        global user_info
        user_info = user_util.gimme_afree_account()
        while user_info ==None:
            time.sleep(5)
            user_info = user_util.gimme_afree_account()
        return user_info

    def release_user(self,user_mid):
        print "release"
        print user_mid
        try:
            user_util.release_account(user_mid)
        except:
            print "release user fail"

    def get_cid(self):
        '''获取用户cid'''
        self.personinfo_page = Personinfo_Page()
        # self.start_step("获取用户mid")
        self.personinfo_page.wait_element("头像").click()
        time.sleep(6)
        cid = self.personinfo_page.wait_element("账号ID").get_attribute('text')
        print "获取的用户cid为: %s" % cid
        self.game_page.wait_element("关闭对话框").click()
        time.sleep(3)
        if (self.game_page.element_is_exist("关闭对话框")):  # 如果弹破产弹框,则关闭
            self.game_page.wait_element("关闭对话框").click()
            time.sleep(3)
        return cid

    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 get_safebox_money(self):
        '''从保险箱取出全部存款'''
        self.safebox_page = Safebox_Page()
        # self.start_step("从保险箱取出所有存款")
        self.hall_page.wait_element("保险箱").click()
        time.sleep(2)
        self.safebox_page.wait_element("取出").click()
        if (self.safebox_page.element_is_exist("确定---保险箱")):
            slider = self.safebox_page.wait_element("滚动条")
            addgoldbtn = self.safebox_page.wait_element("增加金条/银条数目")
            x = slider.location['x']
            y = slider.location['y']
            x1 = addgoldbtn.location['x']
            y1 = addgoldbtn.location['y']
            self.luadriver.swipe(x, y, x1, y1)
            self.safebox_page.wait_element("确定---保险箱").click()
            time.sleep(2)
            self.luadriver.keyevent(4)
        else:
            print ("保险箱没有银币存款")
            time.sleep(2)
            self.luadriver.keyevent(4)

    def get_safebox_crystal(self):
        '''从保险箱取出全部金条'''
        self.safebox_page = Safebox_Page()
        self.hall_page.wait_element("保险箱").click()
        time.sleep(2)
        self.safebox_page.wait_element("金条保险箱").click()
        time.sleep(2)
        self.safebox_page.wait_element("取出").click()
        if (self.safebox_page.element_is_exist("确定---保险箱")):
            slider = self.safebox_page.wait_element("滚动条")
            addgoldbtn = self.safebox_page.wait_element("增加金条/银条数目")
            x = slider.location['x']
            y = slider.location['y']
            x1 = addgoldbtn.location['x']
            y1 = addgoldbtn.location['y']
            self.luadriver.swipe(x, y, x1, y1)
            self.safebox_page.wait_element("确定---保险箱").click()
            time.sleep(2)
            self.luadriver.keyevent(4)
        else:
            print ("保险箱没有金条存款")
            self.luadriver.keyevent(4)

    def get_game_list(self):
        '''
        获取当前界面子游戏列表名
        :return: 游戏列表名
        '''
        self.game_page = Game_Page()
        elements = self.game_page.get_elements("子游戏列表")
        print len(elements)
        #去重,同时去除game0(约牌,比赛场)和game-1(更多游戏)
        elementsblack = ["game0","game-1"]
        game_list = []
        for i in range(len(elements)):
            name =elements[i].get_attribute("name")
            print name
            if name not in elementsblack:
                elementsblack.append(name)
                game_list.append(elements[i])
        print len(game_list)
        return game_list

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

    def get_cid(self):
        '''获取用户cid'''
        self.personinfo_page = Personinfo_Page()
        self.personinfo_page.wait_element("头像").click()
        cid = self.personinfo_page.wait_element("账号ID").get_attribute('text')
        print "获取的用户cid为: %s" % cid
        self.personinfo_page.wait_element("关闭").click()
        if (self.personinfo_page.element_is_exist("关闭")):  # 如果弹破产弹框,则关闭
            self.personinfo_page.wait_element("关闭").click()
        return cid

    def get_mid(self):
        cid = self.get_cid()
        time.sleep(3)
        config = ConfigHelper(constant.cfg_path)
        region = config.getValue('casecfg', 'region')
        mid = PHPInterface.get_mid(cid, region)
        print "用户mid为:%s" % mid
        return mid

    def set_coin(self,mid,value):
        # 获取用户银币信息
        result_userinfo = PHPInterface.get_user_info(mid)
        myuser_info = json.loads(result_userinfo)
        coin = myuser_info.get('result', {'coin': None}).get('coin')
        AddMoney = int(value) - int(coin)
        result = PHPInterface.add_money(mid, AddMoney)
        # print "用户银币数为:%s" % AddMoney

    def set_crystal(self,mid,value):
        # 设置金条数据
        result_userinfo = PHPInterface.get_user_info(mid)
        myuser_info = json.loads(result_userinfo)
        crystal = myuser_info.get('result', {'crystal': None}).get('crystal')  # 获取当前金条值
        print "用户金条数为:%s" % crystal
        AddMoney = int(value) - int(crystal)
        PHPInterface.add_crystal(mid, AddMoney)

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

    def recover_user(self,mid):
        #初始化用户
        self.set_coin(mid,'10000')
        self.set_crystal(mid,"0")
        PHPInterface.set_vip(mid,"-1")
        self.set_safeBoxMoney(mid,0,0)
        self.set_safeBoxMoney(mid,0,1)

    def set_safeBoxMoney(self, mid, money, money_type):
        # money_type=0银币,1为金条
        safeBoxInfo = PHPInterface.get_safebox(mid)
        if money_type == 0:
            safeBoxMoney = int(safeBoxInfo.get("safebox"))
        else:
            safeBoxMoney = int(safeBoxInfo.get("crystalsafebox"))

        if safeBoxMoney - money > 0:
            PHPInterface.withdraw_safebox(mid, safeBoxMoney - money, money_type)
            if money_type == 0:
                PHPInterface.add_money(mid, -(safeBoxMoney - money))
            else:
                PHPInterface.add_crystal(mid, -(safeBoxMoney - money))
        else:
            if money_type == 0:
                PHPInterface.add_money(mid, money - safeBoxMoney)
            else:
                PHPInterface.add_crystal(mid, money - safeBoxMoney)

            PHPInterface.deposit_safebox(mid, money - safeBoxMoney, money_type)
Example #9
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
Example #10
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("游戏已下载")