from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.remote.webdriver import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from time import sleep
from datetime import datetime
from common.logger import log
import os


class BasePage(object):

    def __init__(self, driver):
        # 获取驱动
        self.driver = driver
        # 线程等待时间，秒
        self.time_seconds = 1

    def open_page(self, url):
        """打开页面"""
        # 获取页面链接
        self.driver.get(url)
        # 设置隐式等待时间
        self.driver.implicitly_wait(10)
        # 窗口最大化
        self.driver.maximize_window()

    '''单击一个元素'''

    def single_click(self, page_element):
        page_element.click()

    def single_click_then_wait(self, page_element, seconds):
        page_element.click()
        self.wait_seconds(seconds)


    '''获取一个元素上的文本信息'''

    def get_text(self, page_element):
        text = page_element.get_attribute('textContent')
        return text

    '''填入文本'''

    def input_text(self, page_element, text):
        page_element.clear()
        page_element.send_keys(text)

    """在一定时间内定位到某个元素，且这个元素已经能呈现出来"""

    def get_element_wait_presence(self, locator):
        self.wait_seconds(self.time_seconds)
        element = WebDriverWait(self.driver, 10, 0.1).until(EC.presence_of_element_located(locator))
        return element

    def refresh_page(self):
        """刷新页面"""
        self.driver.refresh()

    def move_to_element(self, page_element):
        """移动鼠标仅限于鼠标触发型元素，需要等待元素程序才移动"""
        self.wait_seconds(2 * self.time_seconds)
        """鼠标移到page_element上"""
        action_chains = ActionChains(self.driver)
        action_chains.move_to_element(page_element).perform()

    """切换到iframe上"""

    def switch_to_iframe(self, page_element_iframe):
        self.wait_seconds(self.time_seconds)
        self.driver.switch_to.frame(page_element_iframe)

    """获取一个表的行信息"""

    def get_all_rows_from_tale(self, table_element):
        self.wait_seconds(self.time_seconds)
        rows = table_element.find_elements(By.TAG_NAME, "tr")
        return rows

    """获取一个表,指定行的列信息"""

    def get_all_columns_from_row(self, row_element):
        columns = row_element.find_elements(By.TAG_NAME, "td")
        return columns

    '''给定一个表中的某行的某列，要输入信息，需要取出该栏位中的input元素-后续看要不要将该方法移出基础库，移到对应的功能库'''

    def get_input_from_column(self, column):
        element = column.find_element(By.TAG_NAME, 'input')
        return element

    def wait_seconds(self, seconds):
        """睡眠时间-秒"""
        sleep(seconds)

    def get_screenshot(self, filename="截图"):
        """"截图"""
        # 截图根目录
        screenshot_dir = './screenshot'
        if not os.path.exists(screenshot_dir):
            # 目录不存在则创建目录
            os.mkdir(screenshot_dir)
        # 当前日期
        now_date = datetime.now().strftime('%Y%m%d')
        # 设置文件目录下按照日期新建的子文件夹
        screenshot_date_dir = os.path.join(screenshot_dir, str(now_date))
        if not os.path.exists(screenshot_date_dir):
            # 不存在则创建
            os.mkdir(screenshot_date_dir)
        # 时间戳到毫秒级
        now_time_ms = datetime.now().strftime('%H%M%S%f')
        # 拼接截图的图片名与类型，时间戳 + 文件名 + .png
        filename = now_time_ms + filename + ".png"
        # 图片目录确定
        filepath = os.path.join(screenshot_date_dir, filename)
        # 截图保存到该目录
        self.driver.get_screenshot_as_file(filepath)

    '''展示窗口句柄信息，测试用，只下日志'''

    def display_window_handle(self):
        log.info('当前窗口的title：%s' % self.driver.title)
        log.info('当前窗口的url：%s' % self.driver.current_url)
        log.info('当前窗口的句柄：%s' % self.driver.current_window_handle)
        log.info('所有句柄：%s' % self.driver.window_handles)

    '''获取当前页面title'''

    def get_window_title(self):
        """"切换页面需要时间"""
        self.wait_seconds(self.time_seconds)
        return self.driver.title

    """关闭当前页面"""

    def close_browser(self):
        self.driver.close()

    """关闭所有页面"""

    def quit_browser(self):
        self.driver.quit()
