selenium java po模式_Selenium的PO模式(Page Object Model)

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

from selenium.webdriver.support.wait importWebDriverWait

from selenium import webdriver

classAction(object):

"""

BasePage封装所有页面都公用的方法,例如driver, url ,FindElement等

"""

#初始化driver、url、等

def __init__(self, selenium_driver, base_url, pagetitle):

self.base_url = base_url

self.pagetitle = pagetitle

self.driver = selenium_driver

#打开页面,校验页面链接是否加载正确

def _open(self, url, pagetitle):

#使用get打开访问链接地址

self.driver.get(url)

self.driver.maximize_window()

#使用assert进行校验,打开的链接地址是否与配置的地址一致。调用on_page()方法

assert self.on_page(pagetitle), u"打开开页面失败 %s"% url

#重写元素定位方法

def find_element(self,*loc):

#return self.driver.find_element(*loc)

try:

WebDriverWait(self.driver,10).until(lambda driver: driver.find_element(*loc).is_displayed())

return self.driver.find_element(*loc)

except:

print u"%s 页面中未能找到 %s 元素"%(self, loc)

#重写switch_frame方法

def switch_frame(self, loc):

return self.driver.switch_to_frame(loc)

#定义open方法,调用_open()进行打开链接

def open(self):

self._open(self.base_url, self.pagetitle)

#使用current_url获取当前窗口Url地址,进行与配置地址作比较,返回比较结果(True False)

def on_page(self, pagetitle):

return pagetitle in self.driver.title

#定义script方法,用于执行js脚本,范围执行结果

def script(self, src):

self.driver.execute_script(src)

#重写定义send_keys方法

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)

exceptAttributeError:

print u"%s 页面中未能找到 %s 元素"%(self, loc)

LoginPage.py:

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

from selenium.webdriver.common.by importBy

importBasePage

#继承BasePage类

classLoginPage(BasePage.Action):

#定位器,通过元素属性定位元素对象

username_loc =(By.ID,"idInput")

password_loc =(By.ID,"pwdInput")

submit_loc =(By.ID,"loginBtn")

span_loc =(By.CSS_SELECTOR,"div.error-tt>p")

dynpw_loc =(By.ID,"lbDynPw")

userid_loc =(By.ID,"spnUid")

#Action

def open(self):

#调用page中的_open打开连接

self._open(self.base_url, self.pagetitle)

#调用send_keys对象,输入用户名

def input_username(self, username):

self.find_element(*self.username_loc).send_keys(username)

#调用send_keys对象,输入密码

def input_password(self, password):

self.find_element(*self.password_loc).send_keys(password)

#调用send_keys对象,点击登录

def click_submit(self):

self.find_element(*self.submit_loc).click()

#用户名或密码不合理是Tip框内容展示

def show_span(self):

return self.find_element(*self.span_loc).text

#切换登录模式为动态密码登录(IE下有效)

def swich_DynPw(self):

self.find_element(*self.dynpw_loc).click()

#登录成功页面中的用户ID查找

def show_userid(self):

return self.find_element(*self.userid_loc).text

CaseLoginTest.py:

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

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

import unittest

from PO importLoginPage

from selenium import webdriver

classCaselogin126mail(unittest.TestCase):

"""

登录126邮箱的case

"""

@classmethod

def setUpClass(cls):

cls.driver = webdriver.Chrome()

cls.driver.implicitly_wait(30)

cls.url ="http://mail.126.com"

cls.username ="liuke01"

cls.password ="liuke123"

#用例执行体

def test_login_mail(self):

#声明LoginPage类对象

login_page =LoginPage.LoginPage(self.driver, self.url, u"网易")

#调用打开页面组件

login_page.open()

#调用用户名输入组件

login_page.input_username(self.username)

#调用密码输入组件

login_page.input_password(self.password)

#调用点击登录按钮组件

login_page.click_submit()

@classmethod

def tearDownClass(cls):

cls.driver.quit()

if __name__ =="__main__":

unittest.main()


版权声明:本文为weixin_30095015原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。