# -*- coding: utf-8 -*-

'''
Created on 2017年7月15日

@author: Administrator
'''
from selenium import webdriver
from selenium.common.exceptions import *


class BasePage(object):
'''
所有页面对象(page object PO)类的父类,
封装了页面的基本操作
'''

def __init__(self, driver):
  self._driver = driver


def find_element(self, loc):
  "定位一个元素"
  return self._driver.find_element(*loc)

def find_elements(self, loc):
  "定位一组元素"
  return self._driver.find_elements(*loc)

def click_element(self, loc):
  "点击一个页面元素"
  ele = self.find_element(loc)
  ele.click()

def click_link(self, loc):
  "点击一个页面链接"
  ele = self.find_element(loc)
  tagname = ele.tag_name
  if tagname != u"a":
    raise WebDriverException,"不是一个链接元素"
  ele.click()

def input_text(self, loc, text):
  "向页面的文本框内输入text"
  ele = self.find_element(loc)
  ele.clear()
  ele.send_keys(text)
  return

def select_radio(self, loc):
  "在页面点击选择单选按钮"
  ele = self.find_element(loc)
  ele.click()

def select_checkbox(self, loc):
  "在页面点击选择筛选框"
  ele = self.find_element(loc)
  ele.click()

#def deselect_checkbox(self, loc):
  #"取消选中的复选框"
  #pass

def page_should_contain(self, text):
  xpath = "//*[contains(., '%s')]" %text
  #self._driver.implicitly_wait(3)
  try:
    self._driver.find_element_by_xpath(xpath)
  except:
    return False
  #finally:
  #self._driver.implicitly_wait(10)
  return True

def get_alert_message(self):
  "获取alert窗口信息"
  msg = None
  try:
    alert = self._driver.switch_to_alert()
    msg = alert.text
    alert.accept()
  except:
    return None
  return msg