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
from Charles.mitm import mitm_query
from appiumConfigure import backtoapp
from Charles import AssertLen

class Case(appiumConfig):

    def test_01_search_sendkey_click(self):
        '首页搜索输入框点击'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击搜索输入框
        ele = find_element.element(self.driver,home_Page.search_sendkey_click)
        ele.click()

        backtoapp.backapp(self.driver, 2)

        # 埋点实例
        # 'params': {'explicit_word_num': 1, 'page_name': 'home', 'referrer_link': []},
        # 'type': 'on_click_navbar_search',

        result = mitm_query(action="on_click_navbar_search",page_name="home",explicit_word_num=1)
        AssertLen.maidiannumber(result)


    def test_02_search_sendkey_click(self):
        '热门搜索点击'

        # 关闭弹窗点击
        ele = find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击搜索输入框
        ele = find_element.element(self.driver, home_Page.search_sendkey_click)
        ele.click()
        # 获取按钮文案
        ele = find_element.element(self.driver, search_Page.hot_search)
        hot_search_text = ele.text

        # 点击热门搜索第一个选项
        ele = find_element.element(self.driver,search_Page.hot_search)
        ele.click()

        # "params": {
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "position": 0,
        #     "query_id": "1651",
        #     "query": "瘦脸针580",
        #     "card_name": "瘦脸针580",
        #     "card_id": "1651",
        #     "referrer": "home",
        #     "transaction_type": "promote",
        #     "referrer_id": "",
        #     "page_name": "search_home",
        #     "card_type": "search_word",
        #     "business_id": "",
        #     "card_content_type": "自定义专题页面",
        #     "in_page_pos": "热门"
        # },
        #     "type": "on_click_card",

        # 过滤埋点数据
        result = mitm_query(action="on_click_card", in_page_pos="热门", card_content_type="自定义专题页面", page_name="search_home")
        AssertLen.maidiannumber(result)
        # 获取个别 value
        query_id = result[0].get("params").get("query_id")
        card_name = result[0].get("params").get("card_name")
        position = result[0].get("params").get("position")
        # 判空
        find_element.notnull((query_id,card_name,position))
        # 判断是否和预期相符
        find_element.AssertExpect(hot_search_text, card_name)


    def test_03_search_sendkey_click(self):
        '热门榜第一位点击'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击搜索输入框
        ele = find_element.element(self.driver, home_Page.search_sendkey_click)
        ele.click()

        # 获取顶部搜索框文案
        ele = find_element.element(self.driver,search_Page.search_content)
        search_content = ele.text
        # 点击更美热门榜第一位
        ele = find_element.element(self.driver,search_Page.hot_list)
        ele.click()

        backtoapp .backapp(self.driver, 2)

        # 'params': {'business_id': '',
        #            'in_page_pos': '更美热门榜',
        #            'page_name': 'search_home',
        #            'position': '2',
        #            'query': '下巴',
        #            'referrer': 'home'
        #            'referrer_id': '',
        #            'referrer_link': ['home'],
        #            'word_position': '0'},
        # 'type': 'on_click_card'

        result = mitm_query(action="on_click_card",in_page_pos="更美热门榜", page_name="search_home", referrer="home")
        # 获取局部参数
        position = result[0].get("params").get("position")
        word_position = result[0].get("params").get("word_position")
        query = result[0].get("params").get("query")
        # 判空
        find_element.notnull((position, word_position))
        #判断预期
        find_element.AssertExpect(search_content,query)




