BasePage
的封装
使用selenium框架来操作页面时,最常用的都是一些点击,输入内容,页面切换等方法,如果每个页面我们都要写一遍这样的操作代码,重复性高,代码冗余。所以我们一般都会把这些共性的操作提取成一个基础类:BasePage
。
├─common
│ │ basepage.py
│ │ __init__.py
│ │
封装BasePage
类
basepage.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/1/22 12:48
# @Author : shisuiyi
# @File : basepage.py
# @Software: win10 Tensorflow1.13.1 python3.9
import time
import allure
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
class BasePage:
"""储存浏览器,页面的通用操作"""
def __init__(self, driver):
self.driver = driver
def goto(self, url):
self.driver.get(url)
def reload(self):
"""刷新"""
self.driver.refresh()
def wait_element_clickable(self, locator, timeout=10):
"""等待元素可以被点击"""
wait = WebDriverWait(self.driver, timeout=timeout, poll_frequency=0.2)
el = wait.until(EC.element_to_be_clickable(locator))
return el
def wait_element_visible(self, locator, timeout=10):
"""等待元素可见"""
wait = WebDriverWait(self.driver, timeout=timeout, poll_frequency=0.2)
el = wait.until(EC.visibility_of_element_located(locator))
return el
def wait_title_is(self, title, timeout=10):
"""等待 title 等于"""
wait = WebDriverWait(self.driver, timeout=timeout, poll_frequency=0.2)
# EC.title_is(title) 内置的显性等待,页面的标题是否等于
return wait.until(EC.title_is(title))
def wait_url_contains(self, url, timeout=10):
"""等待 url包含"""
wait = WebDriverWait(self.driver, timeout=timeout, poll_frequency=0.2)
return wait.until(EC.url_contains(url))
def get_element(self, locator):
"""查找元素"""
el = self.driver.find_element(*locator)
return el
def get_elements(self, locator):
"""查找多个元素"""
elements = self.driver.find_elements(*locator)
time.sleep(1)
return elements
def write(self, locator, value):
"""输入操作"""
el = self.driver.find_element(*locator)
el.send_keys(value)
return self # return self返回的是类的实例。
def click(self, locator):
"""鼠标点击。方法2"""
el = self.wait_element_clickable(locator)
# el = self.driver.find_element(*locator)
action = ActionChains(self.driver)
action.click(el).perform()
def double_click(self, locator):
el = self.wait_element_clickable(locator)
# el = self.driver.find_element(*locator)
action = ActionChains(self.driver)
action.double_click(el).perform()
def context_click(self, locator):
el = self.driver.find_element(*locator)
action = ActionChains(self.driver)
action.context_click(el).perform()
def click_and_hold(self, locator):
"""长按"""
try:
el = self.driver.find_element(*locator)
action = ActionChains(self.driver)
action.click_and_hold(el).perform()
except Exception as e:
print('[ERROR]-长按页面元素{}失败,原因:{}'.format(locator, e))
def clear_element(self, locator):
"""元素清空"""
self.wait_element_clickable(locator).clear()
return self
def select(self, locator1, locator2):
"""下拉选择"""
self.click(locator1)
self.click(locator2)
def enter(self):
"""回车"""
action = ActionChains(self.driver)
action.send_keys(Keys.ENTER).perform()
def send_file(self, locator, file_path):
"""发送文件"""
el = self.wait_element_visible(locator)
el.send_keys(file_path)
def move_to(self, locator):
"""鼠标悬停, locator = ('xpath', 'value')"""
el = self.driver.find_element(*locator)
action = ActionChains(self.driver)
action.move_to_element(el).perform()
def drag_and_drop(self, locator_start, locator_end):
start_el = self.wait_element_clickable(locator_start)
end_el = self.wait_element_clickable(locator_end)
action = ActionChains(self.driver)
action.drag_and_drop(start_el, end_el).perform()
def switch_to_iframe(self, iframe_reference, timeout=30):
"""iframe切换"""
# self.driver.switch_to.frame(iframe_reference)
wait = WebDriverWait(self.driver, timeout=timeout, poll_frequency=0.2)
wait.until(EC.frame_to_be_available_and_switch_to_it(iframe_reference))
# frame_to_be_available_and_switch_to_it 此方法会判断iframe是否可用,并且会自动切换到iframe中。
def allure_screenshot(self, name=None):
"""截图"""
f = self.driver.get_screenshot_as_png()
return allure.attach(f,
name=name,
attachment_type=allure.attachment_type.PNG)
def script(self, src):
"""
定义script方法,用于执行js脚本
"""
self.driver.execute_script(src)
def switch_window(self, n):
"""窗口切换"""
# WebDriver对象有window_handles 属性,这是一个列表对象, 里面包括了当前浏览器里面所有的窗口句柄。
self.driver.switch_to.window(self.driver.window_handles[n])
def assert_element_attribute_equal(self, locator, attr_name, expected):
"""断言元素的text文本等于"""
el = self.wait_element_visible(locator)
actual = el.get_attribute(attr_name)
print("文本", actual)
print(expected)
assert actual == expected
评论