先抽象封装一个BasePage类,这个基类拥有一些指向Webdriver实例的属性,然后每一个Page继承基类BasePage,可以通过driver管理每一个Page中的元素,而且在Page中将这些操作封装为一个一个的方法。也就是Process类。TestCase继承unittest里面的TestCase类,并且依赖page类,进行组织测试步骤的工作。
这样做的好处,就是有元素变化,只需要维护每一个Page就行了,测试步骤变化,只需要维护TestCase即可
基类封装一些常用的操作方法
基类BasePage类:
class BasePage(object):
def __init__(self,driver):
self.driver=driver
#浏览器的基本操作
def openurl(self,url):
self.driver.get(url)
Log.logsinfo('打开浏览器地址%s--success'%url)
def waittime(self):
self.driver.implicitly_wait(60)
def setmaxbrowser(self):
self.driver.maximize_window()
def setmixbrowser(self):
self.driver.minimize_window()
#元素操作方法
def click(self,element_info):
element=self.find_element(element_info)
element.click()
Log.logsinfo('【%s】进行点击--success'%element_info['element_name'])
def input(self,element_info,content):
elment=self.find_element(element_info)
elment.send_keys(content)
Log.logsinfo('对【%s】输入内容【%s】--success'%(element_info['element_name'],content))
def submit(self,element_info):
element=self.find_element(element_info)
element.submit()
Log.logsinfo('【%s】表单提交--success' % element_info['element_name'])
#页面布局操作方法
def clear(self,element_info):
element=self.find_element(element_info)
element.clear()
def switchframe(self,element_info):
element=self.find_element(element_info)
self.driver.switch_to.frame(element)
Log.logsinfo('切入框架--【%s】--success'%element_info['element_name'])
def quitframe(self):
self.driver.switch_to.default_content()
Log.logsinfo('切回默认框架--success')
#将滚动条滚到元素位置
def scrollbarelement(self,element_info):
element = self.find_element(element_info)
self.driver.execute_script('arguments[0].scrollIntoView();',element)
#selenium 执行脚本
def deleteelementattribute(self,element_info,attribute_name):
element=self.find_element(element_info)
self.driver.execute_script('arguments[0].removeAttribute("%s");'%attribute_name,element)
def updateelementattribute(self,element_info,attribute_name,value):
element=self.find_element(element_info)
self.driver.execute_script('arguments[0].setAttribute("%s","%s");'%(attribute_name,value),element)
#Alert 操作
def accept(self):
self.driver.switch_to.alert.accept()
def dismiss(self):
self.driver.switch_to.alert.dismiss()
def dismiss(self,contect):
self.driver.switch_to.alert.send_keys(contect)
#鼠标操作
def rightclick(self,element_info):
element=self.find_element(element_info)
ActionChains(self.driver).context_click(element).perform()
def releasemouse(self):
ActionChains(self.driver).release().perform()
def releasehover(self,element_info):
element=self.find_element(element_info)
ActionChains(self.driver).move_to_element(element).perform()
Page类
class LoginPage(BasePage):
def __init__(self,driver):
super(LoginPage,self).__init__(driver)
element=GetElementInfoYaml().getelementinfo()
#获取元素定位信息
self.caotao_button=element['login_page']['caotao_button']
self.username_inputbox=element['login_page']['username_inputbox']
self.userpassword_inputbox=element['login_page']['userpassword_inputbox']
self.login_button =element['login_page']['login_button']
#调用父类方法
def clickzaotao(self):
self.click(self.caotao_button)
def inputusername(self,username):
self.input(self.username_inputbox,username)
def inputuserpassword(self,password):
self.input(self.userpassword_inputbox,password)
def clicklogin(self):
self.click(self.login_button)
Process类:
#登录方法
def login(driver,url=getconfig.geturl,username='admin',password='a12345678'):
driver=LoginPage(driver)
driver.openurl(url)
driver.inputusername(username)
driver.inputuserpassword(password)
driver.clicklogin()
return driver
TestCase类:
#添加用户--正常添加用户测试
def NormalUserCreation(dri):
#登录
loginin=login(dri)
#点击组织
home_info=OraganizationHomePage(dri)
home_info.click_organization_link()
#点击添加用户
user_info=OraganizationUserPage(dri)
user_info.click_adduser()
#进入添加用户表单
adduser_info=OraganizationUserAdduserPage(dri)
adduser_info.input_username_input('test' + str(num))
adduser_info.input_userpassword_input('a.123456')
adduser_info.input_repeatpassword_input('a.123456')
adduser_info.input_realname_input('测试' + str(num))
adduser_info.clear_entrydate_input()
adduser_info.input_entrydate_input('2020-04-26')
adduser_info.click_clickpage_clickt()
adduser_info.click_Jobchoice_click()
adduser_info.click_rightsgroup_selected_click()
adduser_info.click_rightsgroup_click()
adduser_info.input_email_input('50534' + str(num) + '@qq.com')
adduser_info.input_codeaccount_input(str(num))
adduser_info.click_gender_button()
adduser_info.clear_passwordchecking_input()
adduser_info.input_passwordchecking_input('a12345678')
adduser_info.scroll_scrollbarelement()
adduser_info.click_save_button()