#!/usr/bin/env python
# -*- coding:utf-8 -*- 
'''
测试测试测试测试测试测试
'''
import time
from runcenter.enums import EnumStatus, EnumPriority
from runcenter.testcase import TestCase, debug_run_all

from common import Interface
from common.common import Common
from uilib.game_page import Game_Page
from uilib.hall_page import Hall_Page
from uilib.level_page import Level_Page

class D25788_TestRedist111(TestCase):
    '''
    测试redis file add 1111
    '''
    owner = "LucyLiu"
    status = EnumStatus.Design
    priority = EnumPriority.High
    timeout = 20
    # print "开始"
    # pending = True

    def pre_test(self):
        self.common = Common()
        self.hall_page = Hall_Page()
        self.game_page = Game_Page()
        self.level_page = Level_Page()
        self.start_step("初始化driver")
        self.luadriver = self.common.setupdriver()
        self.common.closeactivity()
        self.common.user_money(money=3000)

    def run_test(self):
        self.hall_page.wait_element("同步标志")
        self.start_step("进去三人场")
        while self.hall_page.element_is_exist("游戏列表"):
            elments1 = self.hall_page.get_elements("游戏列表")
            print len(elments1)
            elments1[2].click()
        self.start_step("查看房间列表")
        while self.level_page.element_is_exist("房间列表"):
            self.level_page.wait_element("房间列表").click()
        self.game_page.wait_element("聊天")
        self.game_page.screenshot("第一次进房间.png")
        self.start_step("点击退出键退出房间")
        i = 0
        while self.game_page.element_is_exist("退出"):
            self.game_page.wait_element("退出").click()
            i += 1
            self.log_info("点击退出次数：%s" %i )
        self.level_page.wait_element("同步标志")
        self.hall_page.screenshot("点击退出键退出房间.png")
        self.level_page.wait_element("房间列表").click()
        self.game_page.wait_element("聊天")
        self.game_page.screenshot("第二次进房间.png")
        self.start_step("点击物理返回键退出房间")
        while self.game_page.element_is_exist("同步标志"):
            self.luadriver.keyevent(4)
            time.sleep(4)
            self.log_info("点击物理返回键" )
        self.level_page.wait_element("同步标志",30)
        self.hall_page.screenshot("物理返回键退出房间.png")
        self.level_page.wait_element("房间列表").click()
        self.game_page.wait_element("聊天")
        self.game_page.screenshot("第三次进房间.png")
        self.start_step("点击开始按钮")
        self.game_page.wait_element("开始",30).click()
        self.hall_page.screenshot("点击开始按钮.png")
        i = 0
        while self.game_page.element_is_exist("退出"):
            self.game_page.wait_element("退出").click()
            i += 1
            self.log_info("点击退出次数：%s" % i)
            if i > 5:
                while self.game_page.element_is_exist("出牌", 1) == False:
                    list = ["抢地主", "叫地主", "不加倍", "加倍"]
                    self.log_info("叫地主中")
                    for name in list:
                        try:
                            self.game_page.wait_element(name, 1).click()
                        except:
                            self.log_info("未出现抢地主按钮")
                    if self.game_page.element_is_exist("继续游戏", 1):
                        break
                self.start_step("托管")
                if self.game_page.element_is_exist("机器人") == False:
                    self.game_page.wait_element("托管").click()
                while self.game_page.element_is_exist("继续游戏", 1) == False:
                    time.sleep(1)
                    self.log_info("正在游戏中")
                self.start_step("退出玩牌房间")
                self.game_page.is_in_gameroom(self.luadriver)

    def post_test(self):
        try:
            self.game_page.is_in_gameroom(self.luadriver)
        finally:
            self.common.closedriver()

class D25789_TestRedist222(TestCase):
    '''
    测试Redis file add 222
    '''
    owner = "LucyLiu"
    status = EnumStatus.Design
    priority = EnumPriority.High
    timeout = 15
    # pending = True

    def pre_test(self):
        self.common = Common()
        self.hall_page = Hall_Page()
        self.game_page = Game_Page()
        self.level_page = Level_Page()
        self.start_step("初始化driver")
        self.luadriver = self.common.setupdriver()
        self.common.closeactivity()
        self.common.user_money(money=3000)

    def run_test(self):
        self.start_step("进去三人场")
        while self.hall_page.element_is_exist("游戏列表"):
            elments1 = self.hall_page.get_elements("游戏列表")
            print len(elments1)
            elments1[2].click()
        self.start_step("查看房间列表")
        while self.level_page.element_is_exist("房间列表"):
            self.level_page.wait_element("房间列表").click()
        self.start_step("开始玩牌")
        self.game_page.wait_element("开始").click()
        starttime = time.time()
        while self.game_page.element_is_exist("正在配桌中"):
            time.sleep(1)
            self.log_info("正在配桌中,等待")
            endtime = time.time()
            time.sleep(3)
            try:
                self.game_page.wait_element("换桌").click()
            except:
                self.log_info("换桌失败")
            if (endtime - starttime) / 60 > self.timeout - 5:
                self.game_page.is_in_gameroom(self.luadriver)
                self.log_info("等待超时")
                return
        self.start_step("叫地主")
        while self.game_page.element_is_exist("出牌", 1) == False:
            list = ["抢地主", "叫地主", "不加倍"]
            self.log_info("叫地主中")
            for name in list:
                try:
                    self.game_page.wait_element(name, 1).click()
                except:
                    self.log_info("未出现抢地主按钮")
            if self.game_page.element_is_exist("继续游戏", 1):
                break
        self.game_page.element_is_exist("农民地主标志",20)
        self.game_page.screenshot("农民地主标志.png")
        self.start_step("托管")
        if self.game_page.element_is_exist("机器人") == False:
            self.game_page.wait_element("托管").click()
        while self.game_page.element_is_exist("继续游戏", 1) == False:
            time.sleep(1)
            self.log_info("正在游戏中")
        self.start_step("退出玩牌房间")
        self.game_page.is_in_gameroom(self.luadriver)

    def post_test(self):
        try:
            self.game_page.is_in_gameroom(self.luadriver)
        finally:
            self.common.closedriver()


__qtaf_seq_tests__ = [D25744and25745_ThreeRoom_Trustee_Display]
if __name__ == '__main__':
    # C039_DFQP_Activity = C039_DFQP_Activity()
    # C039_DFQP_Activity.debug_run()
    debug_run_all()


