PO模式目录如下:
1、把浏览器设置封装成basedriver.py
from selenium import webdriver
class BaseDriver:
def base_driver(self,browse,url):
if browse=='Firefox' or browse=='F' or browse=='f':
self.driver=webdriver.Firefox()
elif browse=='Firefox' or browse=='c' or browse=='C':
self.driver=webdriver.Chrome()
else:
self.driver=webdriver.Ie()
self.driver.get(url)
self.driver.maximize_window()
return self.driver
2、config中用yaml文件放置配置数据
3、在util中写读取yaml文件的方法
myconfig.py
import os
from util.readfile import ReadYaml
base_path=os.path.dirname(os.path.dirname(__file__))
configyaml_path=os.path.join(base_path,'config','config.yaml')
url=ReadYaml(configyaml_path,'testurl','url').get_yaml_value()
username_err=ReadYaml(configyaml_path,'username_err','username').get_yaml_value()
pwd=ReadYaml(configyaml_path,'username_err','password').get_yaml_value()
readfile.py文件
import os,yaml
class ReadYaml:
def __init__(self,filepath,key,value,index=0):
if os.path.exists(filepath):
self.filepath=filepath
self.index=index
self.key=key
self.value=value
self.data=self.read_yaml()
else:
raise FileNotFoundError("找不到文件")
def read_yaml(self):
with open(self.filepath,'r',encoding='utf-8') as f:
return list(yaml.safe_load_all(f))
def get_yaml_value(self):
return self.data[self.index][self.key][self.value]
4、封装findelement方法
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
class FindElement:
def __init__(self,driver):
self.driver=driver
def find_element(self,*loc):
try:
return WebDriverWait(self.driver,timeout=5).until(lambda x:x.find_element(*loc))
except NoSuchElementException as e:
raise e.args[0]
5、page文件包下的loginpage.py
from selenium.webdriver.common.by import By
from util.findelemt import FindElement
from selenium.webdriver.common.keys import Keys
class LoginPage:
usrname = (By.ID, "username")
pwd = (By.XPATH, '//*[@id="password"]')
loginbutton = (By.CLASS_NAME, "login_box_bth2")
usr_errtext = (By.XPATH, '//*[@type="dialog"]')
def __init__(self, driver):
self.driver = driver
self.findelement = FindElement(driver)
def send_usrname(self, name):
return self.findelement.find_element(*self.usrname).send_keys(name)
def send_pwd(self, pwd):
self.findelement.find_element(*self.usrname).send_keys(Keys.TAB)
return self.findelement.find_element(*self.pwd).send_keys(pwd)
def click_loginbutton(self):
return self.findelement.find_element(*self.loginbutton).click()
def text_usr_err(self):
return self.findelement.find_element(*self.usr_errtext).text
6、最后在case中的test.py中执行测试脚本
import unittest
from base.basedriver import BaseDriver
from util import myconfig
from page.loginpage import LoginPage
class Test(unittest.TestCase):
def setUp(self):
self.driver=BaseDriver().base_driver('c',myconfig.url)
self.loginpage=LoginPage(self.driver)
def tearDown(self):
self.driver.quit()
def test_login_usrerr(self):
self.loginpage.send_usrname(myconfig.username_err)
self.loginpage.send_pwd(myconfig.pwd)
self.loginpage.click_loginbutton()
self.assertEqual('该用户不存在!',self.loginpage.text_usr_err())
if __name__ == '__main__':
unittest.main()