100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python selenium page object_Selenium Page Object 自动化测试框架-Page Object设计

python selenium page object_Selenium Page Object 自动化测试框架-Page Object设计

时间:2022-05-23 06:24:57

相关推荐

python selenium page object_Selenium Page Object 自动化测试框架-Page Object设计

pages

封装测试过程针对页面的操作方法,主要包括basepage、mainpage、loginpage、registepage等函数。pages目录创建时选择“Python Package”格式。

1.Basepage

BasePage是自定义页面基类,封装了基本的页面操作的方法,有find_element,send_keys 2个公共方法,具体的方法可根据测试需要进行扩展。

find_element:定位元素,识别测试页面中需驱动的元素。

Send_keys:发送键值,模拟键盘输入测试数据。

Logger = Logger(logger="BasePage").getlog()是产生一个共有日志类,在当前页面基类中调用。

from common.logger import Logger中表示导入框架中common文件夹下的logger模块。

我把多数代码加了注释,便于大家阅读学习。

具体代码如下:

from selenium import webdriver

#加载元素显示超时设置函数from selenium.webdriver.support.wait import WebDriverWait

#导入截图函数from common.cappic import Cappic

#加载预期处理函数from selenium.webdriver.support import expected_conditions as EC

import time

import os.path

#导入日志处理函数from common.loggen import Logger

logger = Logger(logger="BasePage").getlog()

#定义基础页面类文件,该类仅包含查找元素及输入数据两个子函数class BasePage(object):

def __init__(self, driver, url):

self.driver = driver

self.base_url = url

#定义查找元素超时设置,当页面中某个元素在10秒内没有显示,则抛出异常,并在日志中记录def find_element(self, *loc):

try:

# loc是表示属性元组本身,*loc表述属性元组的值,EC.visibility_of_element_located需要传入2个参数,但*loc是三个参数

# 因此,此处只能locWebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))

# 此处返回元素的属性及属性值,故使用*locreturn self.driver.find_element(*loc)

except:

#当元素找不到的时候调用截图函数Cappic(self.driver)

#元素找不到时在日志中记录信息http://logger.info(u"%s 页面中未能找到 %s 元素" % (self, loc))

def send_keys(self, loc, vaule,):

try:

#获取元素的属性值,以便于识别元素loc = getattr(self, "_%s" % loc)

#查找元素并输入相关数据self.find_element(*loc).send_keys(vaule)

except AttributeError:

#当元素找不到的时候调用截图函数Cappic(self.driver)

#元素找不到时在日志中记录信息http://logger.info(u"%s 页面中未能找到 %s 元素" % (self, loc))

2.Loginpage

LoginPage类是封装用户登陆业务所需的对象驱动方法,继承自BasePage类,有Input_username输入用户名,input_password输入密码,click_submit点击登陆按钮。Loginpage继承Basepage基类方法,故需加载Basepage,执行代码from .basepage import BasePage。

利用Firefox中Firebug对登陆页面中的用户名、密码及登陆按钮三个对象的属性分析,Loginpage中的对象识别使用By.NAME方法。

具体代码如下:

from selenium import webdriver

from pages.basepage import BasePage

from mon.by import By

from common.loggen import Logger

from common.geturl import geturl

logger = Logger(logger="LoginPage").getlog()

#创建登陆操作类,页面中的元素通过name方式识别class LoginPage(BasePage):

#设置登陆操作中所用到的三个元素属性,并以元组形式保存username = (By.NAME, 'username')

password = (By.NAME, 'password')

submit = (By.NAME, 'submit')

#定义用户名元素识别及输入函数,并将此操作写入日志def input_username(self, username):

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

http://logger.info("输入用户名:%s." % username)

#定义密码元素识别及输入函数,并将此操作写入日志def input_password(self, password):

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

http://logger.info("输入密码:%s." % password)

#定义提交按钮元素识别及输入函数,并将此操作写入日志def click_submit(self):

self.find_element(*self.submit).click()

http://logger.info("点击登陆按钮")

3.mainpage

MainPage继承BasePage基类,封装了首页的open打开主页方法,show_userid获取用户登陆id及exit_sys退出系统方法。

可通过show_userid获取用户登陆后的id信息,便于判断登陆是否成功,本次测试并未增加该项判断,读者自自行研究如何判断登陆是否成功。

利用Firefox中Firebug对主页中的userid及退出元素的属性分析,mainpage中的对象识别使用XPATH方法。

import os.path

from .basepage import BasePage

from mon.by import By

from common.loggen import Logger

from selenium import webdriver

import time

logger = Logger(logger="MainPage").getlog()

#定义主页面中所涉及到的元素,userid及退出按钮,通过xpath方式识别

class MainPage(BasePage):

userid_loc = (By.XPATH, './/*[@id=\'ECS_MEMBERZONE\']/font/font')

goodstext_loc = (By.ID,'keyword')

searchbtn_loc = (By.XPATH,'//*[@id="searchForm"]/table/tbody/tr/td[2]/input')

goodsinfo_loc = (By.CSS_SELECTOR,'.goodsItem > p:nth-child(3) > a:nth-child(1)')

buybtn = (By.XPATH,'//*[@id="ECS_FORMBUY"]/ul/li[7]/table/tbody/tr/td[1]/a/img')

paybtn = (By.XPATH,'/html/body/div[6]/div[1]/table/tbody/tr/td[2]/a/img')

posttype = (By.XPATH,'//*[@id="shippingTable"]/tbody/tr[3]/td[1]/input')

paytype = (By.XPATH,'//*[@id="paymentTable"]/tbody/tr[4]/td[1]/input')

ordersubmit = (By.XPATH,'//*[@id="theForm"]/div[15]/div[2]/input[1]')

ordernumber = (By.XPATH,'/html/body/div[6]/div/h6/font')

exit_btn_loc=(By.XPATH, './/*[@id=\'ECS_MEMBERZONE\']/font/a[2]')

# 定义打开超链接方法,并将此操作写入日志

def open(self,base_url):

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

http://logger.info("打开链接: %s." % base_url)

#定义显示userid信息,并将此操作写入日志

def show_userid(self):

userid = self.find_element(*self.userid_loc).text

http://logger.info("当前用户id是:%s." % userid)

return userid

def search_goods(self,testdata):

self.find_element(*self.goodstext_loc).send_keys(testdata)

self.find_element(*self.searchbtn_loc).click()

time.sleep(3)

goods = self.find_element(*self.goodsinfo_loc)

goodsname = goods.text

if goodsname == testdata:

http://logger.info('已查询到%s商品,执行购买操作' %testdata)

goods.click()

else:

http://logger.info('没有匹配的商品,无法完成购买操作')

def buy_goods(self):

self.find_element(*self.buybtn).click()

self.find_element(*self.paybtn).click()

self.find_element(*self.posttype).click()

self.find_element(*self.paytype).click()

self.find_element(*self.ordersubmit).click()

orderno = self.find_element(*self.ordernumber).text

if orderno != '':

http://logger.info('已生成编号为:%s的订单' %orderno)

else:

http://logger.info('订单生成失败')

#定义退出操作,点击退出按钮,并写入日志

def exit_sys(self):

self.find_element(*self.exit_btn_loc).click()

http://logger.info("退出测试系统")

4.registepage

与用户登陆类似,registepage集成Basepage基类,用户注册操作涉及用户名、email、密码、确认密码、登陆操作。

利用Firefox中Firebug对注册页面中的相关元素的属性分析,registepage中的对象识别使用By.ID和XPATH方法。

from selenium import webdriver

from .basepage import BasePage

from mon.by import By

from common.loggen import Logger

from common.geturl import geturl

logger = Logger(logger="UserRegiste").getlog()

#定义注册页面中元素的识别及操作方式,通过id及xpath识别元素class RegistePage(BasePage):

username = (By.ID, 'username')

email = (By.ID, 'email')

password=(By.ID,'password1')

confirmpw=(By.ID,'conform_password')

submit = (By.XPATH, 'html/body/div[6]/div/form/table/tbody/tr[7]/td[2]/input[3]')

#定义用户名输入操作函数,并写入日志def input_username(self, username):

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

http://logger.info("输入用户名:%s." % username)

#定义email输入操作函数,并写入日志def input_email(self,email):

self.find_element(*self.email).send_keys(email)

http://logger.info("输入email:%s." % email)

#定义密码输入操作函数,并写入日志def input_password(self, password):

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

http://logger.info("输入密码:%s." % password)

#定义确认密码输入操作函数,并写入日志def input_comfirpwd(self, comfirpwd):

self.find_element(*self.confirmpw).send_keys(comfirpwd)

http://logger.info("输入确认密码:%s." % comfirpwd)

#定义提交操作函数,并写入日志def click_submit(self):

self.find_element(*self.submit).click()

http://logger.info("点击注册按钮")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。