案例代码分析如下:
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class BasePage(object):
# 实例化BasePage类时,最先执行的就是__init__方法,该方法的入参,其实就是BasePage类的入参。
def __init__(self, selenium_driver, base_url, pagetitle):
self.driver = selenium_driver
self.base_url = base_url
self.pagetitle = pagetitle
# 通过title断言进入的页面是否正确。
# 使用title获取当前窗口title,检查输入的title是否在当前title中,返回比较结果(True 或 False)
def on_page(self, pagetitle):
return pagetitle in self.driver.title
# 打开页面,并校验页面链接是否加载正确
# 以单下划线_开头的方法,在使用import *时,该方法不会被导入,保证该方法为类私有的。
def _open(self, url, pagetitle):
self.driver.get(url)
self.driver.maximize_window()
assert self.on_page(pagetitle), u"打开开页面失败 %s" % url
def open(self):
self._open(self.base_url, self.pagetitle)
def find_element(self, *loc):
try:
WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
return self.driver.find_element(*loc)
except:
print u"%s 页面中未能找到 %s 元素" % (self, loc)
def switch_frame(self, loc):
return self.driver.switch_to_frame(loc)
def script(self, src):
self.driver.execute_script(src)
def send_keys(self, loc, vaule, clear_first=True, click_first=True):
try:
loc = getattr(self, "_%s" % loc)
if click_first:
self.find_element(*loc).click()
if clear_first:
self.find_element(*loc).clear()
self.find_element(*loc).send_keys(vaule)
except AttributeError:
print u"%s 页面中未能找到 %s 元素" % (self, loc)