import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
# import event
from appiumConfigure.config import appiumConfig
from appiumConfigure import find_element
from page import home_Page,search_Page,ai_Page
from Charles.mitm import mitm_query
from appiumConfigure import backtoapp
from Charles import AssertLen
import time
class Case(appiumConfig):

    def test_01_ai_scan(self):
        '跳过按钮埋点'

        #点击关闭弹窗
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)

        # 点击扫脸按钮
        scan = find_element.element(self.driver, home_Page.scan)
        scan.click()

        # 点击扫脸引导弹窗 -- 出现不稳定 采用容错机制
        find_element.ElementNotAssert(self.driver, ai_Page.aiScan)

        # 点击相册按钮
        PictureButton = find_element.element(self.driver, ai_Page.PictureButton)
        PictureButton.click()

        # 点击第一个照片
        picture = find_element.element(self.driver, ai_Page.picture)
        picture.click()

        # 点击跳过按钮
        jump = find_element.element(self.driver, ai_Page.jump)
        jump.click()
        # 退至后台
        backtoapp.backapp(self.driver, 2)


        #跳过按钮埋点
        # "params": {
                #     "button_name": "skip",
                #     "page_name": "face_scan_loading",
                #     "referrer_link": [
                #         "",
                #         "face_scan"
                #     ]
                # },
                # "type": "on_click_button"

        result = mitm_query(action="on_click_button", button_name="skip", page_name="face_scan_loading")
        AssertLen.maidiannumber(result)


    def test_02_ai_scan(self):
        '相册按钮埋点'

        #点击关闭弹窗
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击扫脸按钮
        scan = find_element.element(self.driver, home_Page.scan)
        scan.click()

        # 点击扫脸引导弹窗 -- 出现不稳定 采用容错机制
        find_element.ElementNotAssert(self.driver, ai_Page.aiScan)

        # 点击相册按钮
        PictureButton = find_element.element(self.driver, ai_Page.PictureButton)
        PictureButton.click()

        #退至后台
        backtoapp.backapp(self.driver, 2)

        # 点击相册埋点
        # "params": {
        #     "button_name": "album",
        #     "page_name": "face_scan",
        #     "tab_name": "AI测脸",
        #     "referrer_link": [
        #         ""
        #     ]
        # },
        # "type": "on_click_button"

        result = mitm_query(action="on_click_button", page_name="face_scan", button_name="album", tab_name="AI测脸")
        print(result)
        # q 如果是1正常 超出1 埋点就重复了。
        AssertLen.maidiannumber(result)
        # 因为是列表的形式存储的 所以 要用[0]的格式取
        res = result[0].get("params").get("page_name")
        print(res)



    def test_03_ai_scan(self):
        '上次报告按钮埋点'

        # 点击关闭弹窗
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)

        # 点击扫脸按钮
        scan = find_element.element(self.driver, home_Page.scan)
        scan.click()

        # 扫脸引导弹窗关闭按钮 -- 出现不稳定 采用容错机制
        find_element.ElementNotAssert(self.driver, ai_Page.aiScan)

        # 点击上次报告按钮
        Last_report = find_element.element(self.driver, ai_Page.Last_report)
        Last_report.click()

        # 退至后台
        backtoapp.backapp(self.driver, 2)

        # "params": {
        #     "button_name": "last_report",
        #     "page_name": "face_scan",
        #     "tab_name": "AI测脸",
        #     "referrer_link": [
        #         ""
        #     ]
        # },
        # "type": "on_click_button",

        result = mitm_query(action="on_click_button", page_name="face_scan", button_name="last_report", tab_name="AI测脸")
        AssertLen.maidiannumber(result)





    def test_04_ai_scan(self):
        '拍照按钮埋点'

        # 点击关闭弹窗
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)

        # 点击扫脸按钮
        scan = find_element.element(self.driver, home_Page.scan)
        scan.click()

        # 点击扫脸引导弹窗 -- 出现不稳定 采用容错机制
        find_element.ElementNotAssert(self.driver, ai_Page.aiScan)

        # 点击拍照按钮
        take_photo_record_button = find_element.element(self.driver, ai_Page.take_photo_record_button)
        take_photo_record_button.click()

        # 退至后台
        backtoapp.backapp(self.driver,3)
        # {'button_name': 'take_pic'
        # 'page_name': 'face_scan',
        # 'tab_name': 'AI测脸',
        #  'referrer_link': ['']},
        # 'type': 'on_click_button'
        result = mitm_query(action="on_click_button", tab_name="AI测脸", page_name="face_scan", button_name="take_pic")
        AssertLen.maidiannumber(result)


