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
from appium.webdriver.common.touch_action import TouchAction
import time
import json
from pprint import pprint
class Case(appiumConfig):

    def test_01_home(self):
        '城市选择器点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击导航栏地域选择框（选择城市按钮）
        ele = find_element.element(self.driver,home_Page.city)
        ele.click()

        backtoapp.backapp(self.driver,2)

        result = mitm_query(action="home_click_navbar_area",page_name="home")
        AssertLen.maidiannumber(result)

    def test_02_home(self):
        '首页发帖按钮+号点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 首页点击➕号按钮
        ele = find_element.element(self.driver,home_Page.add_button)
        ele.click()
        backtoapp.backapp(self.driver,2)

        # "params": {
        #     "page_name": "home",
        #     "referrer_link": [
        #
        #     ],
        #     "type": "new"
        # },
        # "type": "on_click_create_topic",

        result = mitm_query(action="on_click_create_topic",page_name="home",type="new")
        AssertLen.maidiannumber(result)


    def test_03_home(self):
        '金刚区按钮点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 金刚区按钮点击
        ele = find_element.element(self.driver,home_Page.home_jingang_icon)
        ele.click()
        backtoapp.backapp(self.driver,2)

        # "params": {
        #     "current_position": 0,
        #     "referrer_link": [],
        #     "card_type": "icon",
        #     "transaction_type": "operation",
        #     "card_content_type": "16",
        #     "total_position": 2,
        #     "url": "gengmei://operation_together?cp_id=9",
        #     "card_id": "26",
        #     "module_id": 26,
        #     "result_status": "",
        #     "page_name": "home",
        #     "card_name": "玻尿酸",
        #     "position": 0,
        #     "in_page_pos": "outer_face"
        # },
        # "type": "on_click_card",

        result  = mitm_query(action="on_click_card",in_page_pos="outer_face",page_name="home",card_type="icon")
        AssertLen.maidiannumber(result)
        url = result[0].get("params").get("url")
        find_element.notnull((url,))



    def test_04_home(self):
        '首页banner点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击banner
        ele = find_element.element(self.driver,home_Page.banner_click)
        ele.click()
        backtoapp.backapp(self.driver, 2)

        # "params": {
        #     "referrer_id": "2695",
        #     "referrer_link": [],
        #     "transaction_type": "operation",
        #     "card_type": "banner",
        #     "card_id": 1983,
        #     "referrer": "",
        #     "module_id": 9280,
        #     "page_name": "home",
        #     "card_name": "女神新人馆",
        #     "card_content_type ": "自定义专题页面",
        #     "position": 2,
        #     "business_id": "",
        #     "in_page_pos": "top"
        # },
        # "type": "on_click_card",

        result = mitm_query(action="on_click_card",card_type="banner",referrer="",page_name="home")
        AssertLen.maidiannumber(result)

    # def test_05_home(self):
    #     '首页四分区豆腐块点击埋点'
    #     # 关闭弹窗点击
    #     find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
    #     # 首页四分区豆腐块点击
    #     ele = find_element.element(self.driver,home_Page.four_fotu)
    #     ele.click()
    #     backtoapp.backapp(self.driver,2)
    #
    #     # "params": {
    #     #     "module_id": 1148,
    #     #     "target_name": "",
    #     #     "module_type": 44,
    #     #     "grid": "a",
    #     #     "page_name": "home",
    #     #     "referrer_link": [],
    #     #     "position": 0,
    #     #     "transaction_type": "operation",
    #     #     "card_type": "gadget",
    #     #     "card_content_type": "45",
    #     #     "business_id": "",
    #     #     "card_id": "1980"
    #     # },
    #     # "type": "on_click_card",
    #
    #     result = mitm_query(action="on_click_card", card_type="gadget", page_name="home", grid="a")
    #     AssertLen.maidiannumber(result)

    def test_06_home(self):
        '首页顶部横条'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击顶部滑动条功能
        ele = find_element.element(self.driver,home_Page.title_hengtiao)
        ele.click()
        backtoapp.backapp(self.driver,2)

        # "params": {
        #     "module_id": "21",
        #     "page_name": "home",
        #     "card_name": "新人免单",
        #     "referrer_link": [],
        #     "absolute_position": 0,
        #     "position": 0,
        #     "card_type": "function_entrance",
        #     "transaction_type": "operation",
        #     "url": "gengmei://common_webview?url=https://backend.igengmei.com/phantom/visual_special/1975"
        # },
        # "type": "on_click_card",

        result = mitm_query(action="on_click_card",page_name="home",absolute_position=0,card_type="function_entrance")
        AssertLen.maidiannumber(result)
        url = result[0].get("params").get("url")
        find_element.notnull((url,))


    def test_07_home(self):
        '点击底部社区按钮埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        #点击底部社区按钮
        ele = find_element.element(self.driver,home_Page.content_click)
        ele.click()
        backtoapp.backapp(self.driver,2)

        # "params": {
        #     "referrer": "",
        #     "sec_tab_name": "",
        #     "tab_type": "out_tab",
        #     "referrer_id": "2883",
        #     "page_name": "community_home",
        #     "tab_name": "community_home",
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "position": "1",
        #     "business_id": ""
        # },
        # "type": "on_click_tab",

        result = mitm_query(action="on_click_tab", tab_type="out_tab", page_name="new_community_home", tab_name="community_home")
        AssertLen.maidiannumber(result)


    def test_08_home(self):
        '轮播图旁豆腐块点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        ele = find_element.element(self.driver,home_Page.new_people)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "module_id": 1184,
        #     "referrer_link": [
        #
        #     ],
        #     "position": "1",
        #     "card_name": "",
        #     "card_id": "",
        #     "referrer": "",
        #     "transaction_type": "operation",
        #     "referrer_id": "",
        #     "card_type": "gadget",
        #     "page_name": "home",
        #     "business_id": "",
        #     "in_pages_pos": "gadget",
        #     "card_content_type": "45",
        #     "module_type": 22
        # }, "type":"on_click_card",
        result = mitm_query(action="on_click_card", card_type="gadget", page_name="home")
        AssertLen.maidiannumber(result)


    def test_09_home(self):
        '机构榜单点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        ele = find_element.element(self.driver,home_Page.mechanism_List)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_link": [
        #
        #     ],
        #     "position": 0,
        #     "card_name": "机构榜单",
        #     "card_id": "",
        #     "referrer": "",
        #     "transaction_type": "operation",
        #     "referrer_id": "",
        #     "card_type": "card",
        #     "page_name": "home",
        #     "business_id": "",
        #     "card_content_type": "hospital_rank",
        #     "in_page_pos": "排行榜"
        # }, "type":"on_click_card",
        result = mitm_query(action="on_click_card", card_name="机构榜单", card_type="card", page_name="home", card_content_type="hospital_rank")
        AssertLen.maidiannumber(result)


    def test_10_home(self):
        '医生榜单点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        ele = find_element.element(self.driver,home_Page.doctor_List)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_link": [
        #
        #     ],
        #     "position": 1,
        #     "card_name": "医生榜单",
        #     "card_id": "",
        #     "referrer": "",
        #     "transaction_type": "operation",
        #     "referrer_id": "",
        #     "card_type": "card",
        #     "page_name": "home",
        #     "business_id": "",
        #     "card_content_type": "doctor_rank",
        #     "in_page_pos": "排行榜"
        # }, "type":"on_click_card",
        result = mitm_query(action="on_click_card", card_type="card", page_name="home", card_content_type="doctor_rank")
        AssertLen.maidiannumber(result)


    def test_11_home(self):
        '速美榜单点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        ele = find_element.element(self.driver,home_Page.sumei_List)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_link": [
        #
        #     ],
        #     "position": 0,
        #     "card_name": "项目榜单",
        #     "card_id": "5",
        #     "referrer": "",
        #     "transaction_type": "operation",
        #     "referrer_id": "",
        #     "card_type": "card",
        #     "page_name": "home",
        #     "business_id": "",
        #     "card_content_type": "items_rank",
        #     "in_page_pos": "排行榜"
        # }, type		on_click_card
        result = mitm_query(action="on_click_card", card_name="项目榜单", card_type="card", page_name="home", card_content_type="items_rank")
        AssertLen.maidiannumber(result)

    def test_12_home(self):
        '点击双眼皮榜点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 下滑
        ele2 = find_element.element(self.driver,home_Page.doctor_List)
        ele1 = find_element.element(self.driver,home_Page.banner_click)
        TouchAction(self.driver).press(ele2).wait(1000).move_to(ele1).release().perform()
        # 点击双眼皮榜按钮
        ele = find_element.element(self.driver,home_Page.double_eye_List)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer_link": [
        #
        #     ],
        #     "position": 1,
        #     "card_name": "项目榜单",
        #     "card_id": "4",
        #     "referrer": "",
        #     "transaction_type": "operation",
        #     "referrer_id": "",
        #     "card_type": "card",
        #     "page_name": "home",
        #     "business_id": "",
        #     "card_content_type": "items_rank",
        #     "in_page_pos": "排行榜"
        # },
        #     "type":"on_click_card",
        result = mitm_query(action="on_click_card", card_name="项目榜单", card_type="card", page_name="home", card_content_type="items_rank",  in_page_pos="排行榜")
        AssertLen.maidiannumber(result)


    def test_13_home(self):
        '首页悬浮窗点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击悬浮窗
        ele = find_element.element(self.driver,home_Page.home_suspension)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "button_name": "open",
        #     "referrer_link": [
        #
        #     ],
        #     "url": "gengmei:\/\/common_webview?url=http%3A\/\/backend.igengmei.com\/\/phantom\/visual_special\/2311%3Fusepage%3Dwk",
        #     "page_name": "home",
        #     "popup_name": "small_popup"
        # }, "type":"on_click_button",

        result = mitm_query(action="on_click_button", button_name="open", page_name="home", popup_name="small_popup")
        AssertLen.maidiannumber(result)



    def test_14_home(self):
        '首页悬浮窗关闭按钮点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 点击悬浮窗关闭按钮
        ele = find_element.element(self.driver,home_Page.home_suspension_close)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "button_name": "close",
        #     "referrer_link": [
        #
        #     ],
        #     "url": "gengmei:\/\/common_webview?url=http%3A\/\/backend.igengmei.com\/\/phantom\/visual_special\/2311%3Fusepage%3Dwk",
        #     "page_name": "home",
        #     "popup_name": "small_popup"
        # },  "type":"on_click_button",
        result = mitm_query(action="on_click_button", button_name="close", page_name="home", popup_name="small_popup")
        AssertLen.maidiannumber(result)


    def test_15_home(self):
        '点击feed流双眼皮节tab点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 首页下滑
        ele2 = find_element.element(self.driver, home_Page.doctor_List)
        ele1 = find_element.element(self.driver, home_Page.banner_click)
        TouchAction(self.driver).press(ele2).wait(1000).move_to(ele1).release().perform()
        # 点击双眼皮节按钮
        ele = find_element.element(self.driver,home_Page.home_feed_double_eye_tab_click)
        ele.click()
        backtoapp.backapp(self.driver,2)
        # "params": {
        #     "referrer": "",
        #     "sec_tab_name": "",
        #     "tab_type": "in_tab",
        #     "referrer_id": "",
        #     "page_name": "home",
        #     "tab_name": "五月大促",
        #     "from_tab_name": "五月大促",
        #     "referrer_link": [
        #         "home"
        #     ],
        #     "position": "1",
        #     "business_id": "",
        #     "extra_param": "{\"click_type\":\"\",\"feed_type\":\"customize_commodity\"}"
        # },
        # "type": "on_click_tab",
        result = mitm_query(action="on_click_tab", tab_type="in_tab", page_name="home")
        AssertLen.maidiannumber(result)


    def test_16_home(self):
        '点击feed流直播tab点击埋点'
        # 关闭弹窗点击
        find_element.ElementNotAssert(self.driver, home_Page.dialog_home_img_cancel)
        # 首页下滑
        ele2 = find_element.element(self.driver, home_Page.doctor_List)
        ele1 = find_element.element(self.driver, home_Page.banner_click)
        TouchAction(self.driver).press(ele2).wait(1000).move_to(ele1).release().perform()
        # 点击双眼皮节按钮
        ele = find_element.element(self.driver, home_Page.home_feed_zhibo_tab_click)
        ele.click()
        backtoapp.backapp(self.driver, 2)
         #    "params": {
         #        "page_name": "home",
         #        "position": 2,
         #        "referrer": "",
         #        "business_id": "",
         #        "tab_type": "in_tab",
         #        "referrer_link": [
         #
         #        ],
         #        "referrer_id": "",
         #        "extra_param": "{
         #    \"feed_type\" : \"home_video\",
         #    \"click_type\" : \"click\"
         #    }",
         #    "tab_name": "直播",
         #    "sec_tab_name": "直播"
         #
         # },
         # "type": "on_click_tab",
        result = mitm_query(action="on_click_tab", page_name="home", tab_type="in_tab", tab_name="直播")
        AssertLen.maidiannumber(result)

