selenium框架封装-pom模式
![](https://img2022.cnblogs.com/blog/1671232/202208/1671232-20220826101310709-276519828.png)
1、page 放元素定位方法
2、tests放数据的
如pages下的登录页面示例:
1、先将定位元素的方法封装,封装了获取元素相同的方法、如定位元素、发送数据、点击按钮都封装为函数
2、参数*loc返回的是一个元组,**loc是个字典(*后面的字母可随意)
basePage.py
#定位元素许多元素用的方法相同,单独写一个,其他类继承 from selenium.webdriver.common.by import By class BasePage(object): def __init__(self,driver): self.driver=driver def get_element(self,*loc): return self.driver.find_element(*loc) def input_text(self,text,*loc): #可变参数放最后面 self.get_element(*loc).send_keys(text) def click_btn(self,*loc): self.driver.find_element(*loc).click() def get_title(self): return self.driver.title class BaiduPage(BasePage): def __init__(self,driver): BasePage.__init__(self,driver) driver.get('https://www.baidu.com') def test_search(self): loc=(By.ID,'kw') loc2=(By.ID,'su') self.input_text('selenium',*loc) self.click(*loc2) if __name__ == '__main__': from selenium import webdriver driver = webdriver.Chrome() baiduPage=BaiduPage(driver) baiduPage.test_search()
3、以注册页面为例,将注册页面具体定位元素赋值
4、继承basePage里的获取元素方法,直接调用
registerPage.py
from selenium.webdriver.common.by import By from time import sleep from testcases.pom.pages.basePage import BasePage class UserRegisterPage(BasePage): username_input=(By.ID,'user_login') name_input = (By.ID,'user_name') email_input = (By.ID,'email') pwd_input = (By.ID,'user_password') conpwd_input = (By.ID,'user_password') phone_input = (By.ID,'user_password_confirmation') code_input = (By.ID,'user_phone_number') phonecode_input = (By.ID,'_rucaptcha') register_btn=(By.NAME, 'commit') def __init__(self,driver): BasePage.__init__(self,driver) def goto_register_page(self): self.driver.get('https://testerhome.com/account/sign_up') self.driver.maximize_window() def input_username(self,username): self.input_text(*self.username_input).clear() self.input_text(username,*self.username_input) def input_name(self,name): self.input_text(*self.name_input).clear() self.input_text(name,*self.name_input) def input_email(self,email): self.input_text(*self.email_input).clear() self.input_text(email,*self.email_input) def input_pwd(self,pwd): self.input_text(*self.pwd_input).clear() self.input_text(pwd,*self.pwd_input) def input_conpwd(self,conpwd): self.input_text(*self.conpwd_input).clear() self.input_text(conpwd,*self.conpwd_input) def input_phone(self,phone): self.input_text(*self.phone_input).clear() self.input_text(phone,*self.phone_input) def input_code(self,code): self.input_text(*self.code_input).clear() self.input_text(code,*self.code_input) def input_phonecode(self,phonecode): self.input_text(*self.phonecode_input).clear() self.input_text(phonecode,*self.phonecode_input) def clickbtn(self): self.click_btn(*self.register_btn) sleep(2)
5、发送数据,测试具体的用例(测试用例可写在Excel、json、yaml等文件夹里读取,读取封装在另一个文件里)
from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from util import util from selenium import webdriver from time import sleep import pytest from testcases.pom.pages.registerPage import UserRegisterPage #pytest+ddt重构,ddt可用脚本、数据、CSV、Excel、json等格式 class TestUserRegister(object): @classmethod def setup_class(cls): cls.driver = webdriver.Chrome() # cls.driver.get('https://testerhome.com/account/sign_up') # cls.driver.maximize_window() cls.registerPage=UserRegisterPage(cls.driver) cls.registerPage.goto_register_page() login_data=[ ('tdemo001','test1','test@qq.com','111','111','13000000000','1111','111','短信验证码不正确'), ('demoest001', 'test1', 'test@qq.com', '111', '111', '13000000000', '1111', '111', '注册成功'), ] # 测试登录验证码错误 @pytest.mark.parametrize('username,name,email,pwd,conpwd,phone,code,phone_code,expected',login_data) def test_register(self,username,name,email,pwd,conpwd,phone,code,phone_code,expected): self.registerPage.input_username(username) self.registerPage.input_name(name) self.registerPage.input_email(email) self.registerPage.input_pwd(pwd) self.registerPage.input_conpwd(conpwd) # 自动识别验证码 if code !='666': #默认情况下验证码给一个666 code = util.get_code(self.driver, 'rucaptcha-image') self.registerPage.input_code(code) self.registerPage.input_phone(phone) self.registerPage.input_phonecode(phone_code) self.registerPage.click_btn() # 等待alert出现 WebDriverWait(self.driver, 5).until(EC.alert_is_present()) alert = self.driver.switch_to.alert # 验证 assert alert.text == expected # self.assertIn('社区置顶') alert.accept() ''' #测试成功 def test_register_ok(self): username=util.get_random_str() name = username email=username+'@11.com' pwd='123456' conpwd='123456' phone='13000000000' #验证码自动获取 code='' phone_code = '1111' expected='注册成功' self.driver.find_element(By.ID,'user_login').clear() #同时执行调用清空,每一个输入框都清空 self.driver.find_element(By.ID, 'user_login').send_keys(username) self.driver.find_element(By.ID,'user_name').clear() #同时执行调用清空,每一个输入框都清空 self.driver.find_element(By.ID, 'user_name').send_keys(name) self.driver.find_element(By.ID,'user_email').clear() #同时执行调用清空,每一个输入框都清空 self.driver.find_element(By.ID, 'user_email').send_keys(email) self.driver.find_element(By.ID,'user_password').clear() #同时执行调用清空,每一个输入框都清空 self.driver.find_element(By.ID, 'user_password').send_keys(pwd) self.driver.find_element(By.ID,'user_password_confirmation').clear() #同时执行调用清空,每一个输入框都清空 self.driver.find_element(By.ID, 'user_password_confirmation').send_keys(conpwd) self.driver.find_element(By.ID,'user_phone_number').clear() #同时执行调用清空,每一个输入框都清空 self.driver.find_element(By.ID, 'user_phone_number').send_keys(phone) #自动识别验证码 code=util.get_code(self.driver,'rucaptcha-image') self.driver.find_element(By.NAME,'_rucaptcha').clear() #同时执行调用清空,每一个输入框都清空 self.driver.find_element(By.NAME, '_rucaptcha').send_keys(code) self.driver.find_element(By.ID,'user_phone_code').clear() #同时执行调用清空,每一个输入框都清空 self.driver.find_element(By.ID,'user_phone_code').send_keys(phone_code) self.driver.find_element(By.NAME, 'commit').click() #等待alert出现 WebDriverWait(self.driver, 5).until(EC.alert_is_present()) alert = self.driver.switch_to.alert # 验证 assert alert.text == expected # self.assertIn('社区置顶') alert.accept() ''' if __name__ == '__main__': pytest.main(['testRegister.py'])