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
import time
import json
class Case(appiumConfig):

    def test_01_search_button(self):
        '搜索按钮点击'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 搜索按钮点击
        ele = find_element.element(self.driver,home_Page.search_click_button)
        ele.click()

        backtoapp.backapp(self.driver, 2)

    #     "params": {
    #         "query": "超声刀",
    #         "referrer_link": [
    #
    #         ],
    #         "page_name": "home",
    #         "input_type": "默认词"
    #     },
    #
    #       "type": "do_search"

        result = mitm_query(action="do_search",page_name="home",input_type="默认词")
        AssertLen.maidiannumber(result)


    def test_02_search_button(self):
        '综合搜索下美购 埋点点击事件'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击美购卡片
        ele = find_element.element(self.driver, search_Page.service_card)
        ele.click()
        # "params": {
        #     "card_id": 5758189,
        #     "position": 0,
        #     "transaction_type": "-1",
        #     "business_id": "",
        #     "special_id": "",
        #     "referrer_tab_name": "精选",
        #     "is_cpc": 1,
        #     "query": "超声提升",
        #     "page_name": "search_result_more",
        #     "referrer_id": "",
        #     "cpc_referer": 88,
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "referrer_entry": "",
        #     "sub_tab_name": "",
        #     "card_type": "card",
        #     "referrer": "home",
        #     "card_content_type": "service",
        #     "card_name": "",
        #     "tab_name": "综合",
        #     "target_name": ""
        # },
        #  "type":"on_click_card"

        result = mitm_query(action="on_click_card", page_name="search_result_more", referrer="home", card_content_type="service")
        AssertLen.maidiannumber(result)




    def test_03_search_button(self):
        '日记tab切换点击'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击日记tab
        ele = find_element.element(self.driver,search_Page.diary_click)
        ele.click()
        backtoapp.backapp(self.driver,2)

        # 'params': {'business_id': '',
        #            'click_tab_name': '日记',
        #            'cpc_referer': '30',
        #            'page_name': 'search_result_more',
        #            'query': '超声刀',
        #            'referrer': 'home',
        #            'referrer_id': '',
        #            'referrer_link': ['home'],
        #            'referrer_tab_name': '精选'},
        #             'type': 'on_click_tab',

        result = mitm_query(action="on_click_tab",click_tab_name="日记",page_name="search_result_more",referrer="home",referrer_tab_name="精选")
        query = result[0].get("params").get("query")
        cpc_referer = result[0].get("params").get("cpc_referer")
        find_element.notnull((query,cpc_referer))

    def test_04_search_button(self):
        '日记tab下 日记卡片点击'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击日记tab
        ele = find_element.element(self.driver,search_Page.diary_click)
        ele.click()
        # 点击日记卡片
        ele = find_element.element(self.driver, search_Page.card_click)
        ele.click()

        #  "params":{
        #     "cpc_referer":0,
        #     "referrer_id":"",
        #     "query":"瘦脸针",
        #     "referrer_link":[
        #         "home"
        #     ],
        #     "transaction_type":"-1",
        #     "card_type":"card",
        #     "card_content_type":"diary",
        #     "card_id":"17311254",
        #     "is_cpc":0,
        #     "referrer_entry":"",
        #     "referrer":"search_result_more",
        #     "card_style":"cascade",
        #     "page_name":"search_result_diary",
        #     "card_name":"瘦脸针日记",
        #     "tab_name":"日记",
        #     "position":0,
        #     "business_id":"",
        #     "request_id":"868410041790363_e86af8824d11412b886fdd5d7d753e89_1612163868",
        #     "in_page_position":"recommend"
        # },
        #       "type":"on_click_card",

        backtoapp.backapp(self.driver,2)

        result = mitm_query(action="on_click_card",
                            card_type="card", card_content_type="diary",
                            referrer="search_result_more", page_name="search_result_diary", tab_name="日记")
        AssertLen.maidiannumber(result)


    def test_05_search_button(self):
        '点击百科卡片'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击百科tab
        ele = find_element.element(self.driver, search_Page.Encyclopedias_card)
        ele.click()

        backtoapp.backapp(self.driver,2)

        # 'params': {'business_id': '',
        #            'click_tab_name': '百科',
        #            'page_name': 'search_result_more',
        #            'query': '瘦脸针',
        #            'referrer': 'home',
        #            'referrer_id': '',
        #            'referrer_link': ['home'],
        #            'referrer_tab_name': '精选'},
        # 'type': 'on_click_tab',

        result = mitm_query(action="on_click_tab", click_tab_name="百科", page_name="search_result_more", referrer="home")
        device_id = result[0].get("device").get("device_id")
        #         AssertLen.devices_id(device_id)
        #         AssertLen.maidiannumber(result)
        AssertLen.maidiannumber(result)


    def test_06_search_button(self):
        '百科下百科列表点击卡片埋点'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击百科tab
        ele = find_element.element(self.driver, search_Page.Encyclopedias_tab)
        ele.click()
        # 点击百科下的卡片
        ele = find_element.element(self.driver, search_Page.aaa)
        ele.click()

        backtoapp.backapp(self.driver,2)
        # 'params': {'business_id': '',
        #            'card_content_type': 'wiki_item',
        #            'card_id': 22,
        #            'card_name': '',
        #            'card_type': 'card',
        #            'in_page_pos': '百科',
        #            'page_name': 'search_result_wiki',
        #            'position': 0,
        #            'query': '瘦脸针',
        #            'referrer': 'search_result_more',
        #            'referrer_id': '',
        #            'referrer_link': ['home'],
        #            'tab_name': '百科',
        #            'transaction_type': '-1'},
        # 'type': 'on_click_card',

        mitm_query(action="on_click_card", card_type="card", page_name="search_result_wiki", referrer="search_result_more", tab_name="百科" )

    def test_07_search_button(self):
        '点击问答tab 点击埋点'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击问答tab
        ele = find_element.element(self.driver, search_Page.questionAndquestion_tab)
        ele.click()
        backtoapp.backapp(self.driver, 2)

        # 'params': {'business_id': '',
        #            'click_tab_name': '问答',
        #            'page_name': 'search_result_more',
        #            'query': '下巴',
        #            'referrer': 'home',
        #            'referrer_id': '',
        #            'referrer_link': ['home'],
        #            'referrer_tab_name': '精选'},
        # 'type': 'on_click_tab',

        result = mitm_query(action="on_click_tab", click_tab_name="问答", page_name="search_result_more", referrer="home", referrer_tab_name="精选")
        AssertLen.maidiannumber(result)


    def test_08_search_button(self):
        '问答下问答列表点击卡片埋点'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击问答tab
        ele = find_element.element(self.driver, search_Page.questionAndquestion_tab)
        ele.click()
        # 点击问答卡片
        ele = find_element.element(self.driver, search_Page.questionAndquestion_card)
        ele.click()
        backtoapp.backapp(self.driver, 2)

        # 'params': {'business_id': '',
        #            'card_content_type': 'answer',
        #            'card_id': '873258',
        #            'card_name': '自体脂肪<ems>隆胸</ems>效果如何？',
        #            'card_type': 'card',
        #            'in_page_position': 'recommend',
        #            'page_name': 'search_result_question_answer',
        #            'position': 0,
        #            'query': '隆胸',
        #            'referrer': 'search_result_more',
        #            'referrer_entry': '',
        #            'referrer_id': '',
        #            'referrer_link': ['home'],
        #            'tab_name': '问答',
        #            'transaction_type': '-1'},
        # 'type': 'on_click_card'

        result = mitm_query(action="on_click_card",
                   card_content_type="answer", card_type="card", page_name="search_result_question_answer", tab_name="问答")
        AssertLen.maidiannumber(result)


    def test_09_search_button(self):
        '点击 我要提问按钮 点击埋点'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击问答tab
        ele = find_element.element(self.driver, search_Page.questionAndquestion_tab)
        ele.click()
        # 点击 我要提问 按钮 点击埋点
        ele = find_element.element(self.driver, search_Page.Iwanttoaskaquestion)
        ele.click()
        backtoapp.backapp(self.driver, 2)

        # 'params': {'business_id': '',
        #            'button_name': 'question',
        #            'page_name': 'search_result_question_answer',
        #            'query': '超声刀',
        #            'referrer': '',
        #            'referrer_id': '',
        #            'referrer_link': ['home'],
        #            'referrer_tab_name': '',
        #            'tab_name': '问答'},
        #           'type': 'on_click_button',

        result = mitm_query(action="on_click_button", button_name="question", page_name="search_result_question_answer", tab_name="问答")
        AssertLen.maidiannumber(result)


    def test_10_search_button(self):
        '问答详情页pv事件'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击问答tab
        ele = find_element.element(self.driver, search_Page.questionAndquestion_tab)
        ele.click()
        # 点击问答卡片
        ele = find_element.element(self.driver, search_Page.questionAndquestion_card)
        ele.click()
        backtoapp.backapp(self.driver, 2)

        # "params": {
        #     "message_id": "",
        #     "referrer_link": [
        #         "home",
        #         "search_result_question_answer"
        #     ],
        #     "in": "1612231783",
        #     "out_time_millis": "1612231786262",
        #     "referrer": "search_result_question_answer",
        #     "fake": 0,
        #     "page_name": "question_answer_detail",
        #     "referrer_id": "",
        #     "business_id": "467859",
        #     "out": "1612231786",
        #     "referrer_tab_name": "问答",
        #     "in_time_millis": "1612231783373",
        #     "extra_param": "",
        #     "is_first": 1,
        #     "is_push": 0
        # },
        #      "type": "page_view",

        result = mitm_query(action="page_view",referrer="search_result_question_answer",page_name="question_answer_detail", referrer_tab_name="问答")
        AssertLen.maidiannumber(result)

    def test_11_search_button(self):
        '综合tab下 进入美购详情页pv事件'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击美购卡片
        ele = find_element.element(self.driver, search_Page.service_card)
        ele.click()
        backtoapp.backapp(self.driver, 2)
        # "params": {
        #     "message_id": "",
        #     "referrer_link": [
        #         "home",
        #         "search_result_more"
        #     ],
        #     "in": "1612233750",
        #     "out_time_millis": "1612233755264",
        #     "referrer": "search_result_more",
        #     "fake": 0,
        #     "page_name": "welfare_detail",
        #     "referrer_id": "",
        #     "business_id": "5752521",
        #     "out": "1612233755",
        #     "referrer_tab_name": "综合",
        #     "in_time_millis": "1612233750919",
        #     "extra_param": "{
        #                    "is_flagship_store": false,
        #                                         "service_type": "common"
        # }",
        # "is_first": 1,
        # "is_push": 0
        #      "type":"page_view",

        result = mitm_query(action="page_view", referrer="search_result_more", page_name="welfare_detail", referrer_tab_name="综合")
        params = result[0].get("params")
        extra_param = params.get("extra_param")
        extra_param = json.loads(extra_param)
        service_type = extra_param.get("service_type")
        find_element.AssertExpect("common", service_type)
        AssertLen.maidiannumber(result)

    def test_12_search_button(self):
        '日记详情页pv事件'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击日记tab
        ele = find_element.element(self.driver, search_Page.diary_click)
        ele.click()
        # 点击日记卡片
        ele = find_element.element(self.driver, search_Page.card_click)
        ele.click()
        backtoapp.backapp(self.driver, 2)

        # "params": {
        #     "message_id": "",
        #     "referrer_link": [
        #         "home",
        #         "search_result_diary"
        #     ],
        #     "in": "1612340401",
        #     "out_time_millis": "1612340408259",
        #     "referrer": "search_result_diary",
        #     "fake": 0,
        #     "page_name": "diary_detail",
        #     "referrer_id": "",
        #     "business_id": "16036101",
        #     "out": "1612340408",
        #     "referrer_tab_name": "日记",
        #     "in_time_millis": "1612340401418",
        #     "extra_param": "",
        #     "is_first": 1,
        #     "is_push": 0
        # },      "type":"page_view",

        result = mitm_query(action="page_view", referrer="search_result_diary", page_name="diary_detail", referrer_tab_name="日记")
        in_time_millis = result[0].get("params").get("in_time_millis")
        in_time_millis = str(in_time_millis)
        find_element.notnull((in_time_millis,))
        AssertLen.maidiannumber(result)

    def test_13_search_button(self):
        '百科详情页pv事件'

        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        time.sleep(2)
        # 搜索按钮点击
        ele = find_element.element(self.driver, home_Page.search_click_button)
        ele.click()
        # 点击百科tab
        ele = find_element.element(self.driver, search_Page.Encyclopedias_tab)
        ele.click()
        # 点击百科下的卡片
        ele = find_element.element(self.driver, search_Page.aaa)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_tab_name": "百科",
        #     "referrer_id": "",
        #     "in": "1612347722",
        #     "is_first": "1",
        #     "referrer_link": [
        #         "home",
        #         "search_result_wiki"
        #     ],
        #     "out": "1612347723",
        #     "referrer": "search_result_wiki",
        #     "is_push": 0,
        #     "page_name": "wiki_detail",
        #     "out_time_millis": 1612347723679,
        #     "fake": 0,
        #     "business_id": "22"
        # },
        # "type": "page_view",

        result = mitm_query(action="page_view", referrer_tab_name="百科", referrer="search_result_wiki")
        page_name = result[0].get("params").get("page_name")
        # 判断是否为空
        find_element.notnull((page_name,))
        # 判断参数，是否在一个列表内
        find_element.AssertParameter(page_name,["wiki_detail", "wiki_collect"])
        #判断埋点是否重复
        AssertLen.maidiannumber(result)



