# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/23
# 描述:
import logging
import os

import pytest
from selenium import webdriver

from unit.public.DBUnit import MysqlDataBase
from unit.public.DataDic import DataDic
from unit.public.YamlUtil import UtilYaml

log = logging.getLogger(__name__)
dd = DataDic()


@pytest.fixture(scope="session", autouse=True)
def init_browser(pytestconfig):
    """初始化测试"""
    log.info('初始化浏览器......')
    client = dd.get_value('client')
    chrome_options = webdriver.ChromeOptions()
    # chrome_options.add_argument('--user-agent=""')  # 设置请求头的User-Agent
    # chrome_options.add_argument('--window-size=1280x1024')  # 设置浏览器分辨率（窗口大小）
    chrome_options.add_argument('--start-maximized')  # 最大化运行（全屏窗口）,不设置，取元素会报错
    chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])  # 禁用浏览器正在被自动化程序控制的提示
    chrome_options.add_argument('--incognito')  # 隐身模式（无痕模式）
    # chrome_options.add_argument('--hide-scrollbars')  # 隐藏滚动条, 应对一些特殊页面
    # chrome_options.add_argument('--disable-javascript')  # 禁用javascript
    # chrome_options.add_argument('--blink-settings=imagesEnabled=false')  # 不加载图片, 提升速度
    # chrome_options.add_argument('--headless')  # 浏览器不提供可视化页面
    chrome_options.add_argument('--ignore-certificate-errors')  # 禁用扩展插件并实现窗口最大化
    # chrome_options.add_argument('--disable-gpu')  # 禁用GPU加速
    # chrome_options.add_argument('–disable-software-rasterizer')
    # chrome_options.add_argument('--disable-extensions')
    driver = webdriver.Chrome(
        executable_path=os.path.join(dd.get_value('root_path'), 'driver',
                                     'chromedriver.exe')) if client == 'localhost' else webdriver.Remote(
        command_executor='http://{}:5555/wd/hub'.format(client), options=chrome_options)
    # driver = webdriver.Chrome(
    #     ChromeDriverManager(path=r'.\\driver').install()) if client == 'localhost' else webdriver.Remote(
    #     command_executor='http://{}:5555/wd/hub'.format(client), options=chrome_options)
    dd.set_value('driver', driver)
    driver.maximize_window()

    log.info('读取项目配置文件......')
    project_conf_path = os.path.join(dd.get_value('root_path'), 'cases', 'UI', dd.get_value('project'),
                                     'envConfig.yaml')
    project_conf = UtilYaml(project_conf_path).load_yaml()
    dd.set_value('project_conf', project_conf)
    dd.set_value('minNum', project_conf['minNum'])

    log.info('初始化数据库连接......')
    dd.set_value('db', MysqlDataBase(project_conf['dbinfo']))

    url = dd.get_value('url')
    log.info('开打网址：{}'.format(url))
    driver.get(url)
