关于selenium 的POM框架
关于POM框架可以分为一个公共的方法+每个页面的封装+测试用例,而关于页面又可以继续拆分为更加细致的点
1.封装页面的元素定位
从from selenium.webdriver.common.by import By
class Login_element(object):
"""元素定位:定位用户名输入框"""
user_input = (By.NAME,"username")
"""定位密码输入框"""
pas_input = (By.NAME,"password")
"""定位登录按钮"""
login_button = (By.XPATH,"//*[@type='button']")
2.封装页面的操作
class Base_page(object):
"""封装页面操作"""
def __init__(self, drivers):
self.driver = drivers
def open_page(self,url):
"""打开页面"""
return self.driver.get(url)
def find_name(self):
"""定位user输入框"""
loc = Login_element.user_input
if not isinstance(loc, tuple):
print("参数错误,非元祖类型,loc= loc")
else:
print("定位元素信息, 定位方式为--%s , 定位值为--%s" %(loc[0],loc[1]))
element = WebDriverWait(self.driver, timeout=5, poll_frequency=0.5).until(EC.presence_of_element_located(Login_element.user_input))
return element
3.封装页面流程
class Login_cloud(object):
def login_(self,base_url, driver_, user, pwd):
"""登陆"""
Base_page(driver_).open_page(base_url)
driver_.implicitly_wait(10)
Base_page(driver_).input_user(user)
time.sleep(2)
Base_page(driver_).input_pwd(pwd)
time.sleep(2)
Base_page(driver_).click_button()
time.sleep(1)
4.用例下的conftest文件
@pytest.fixture(scope="session")
def drivers(base_url):
driver = webdriver.Chrome()
driver.maximize_window()
yield driver
driver.quit()
@pytest.fixture(scope="session")
def base_url():
url = "url"
return url
@pytest.fixture(scope="session")
def login_driver(drivers, base_url):
user = "user"
pwd = "pwd"
Login_cloud().login_pwd_none(base_url, drivers, user, pwd)
yield drivers
@pytest.fixture(scope="session")
def status_driver(login_driver):
"""定位到下一个页面"""
drivers = login_driver
drivers.find_element(By.XPATH, "//a[@href='#/status/status']").click()
time.sleep(5)
yield drivers
这样每个页面的单独操作和元素定位都已经分工完毕,测试用例直接调用页面的流程即可。数据可以通过yaml结合pytest的参数化进行管理。
|