python3+selenium二次封装调用
baseui工具类_封装selenium的常用方法
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import time
import allure
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
# allure操作之前操作之后截图操作
def shot(func):
def function(*args, **kwargs):
allure.attach(
args[0].driver.get_screenshot_as_png(),
args[1] + '之前',
allure.attachment_type.PNG)
res = func(*args, **kwargs)
allure.attach(
args[0].driver.get_screenshot_as_png(),
args[1] + '之后',
allure.attachment_type.PNG)
return res
return function
# 二次封装selenium方法
class baseUI():
def __init__(self, driver):
self.driver = driver
def local_element(self, xpath):
return WebDriverWait(
self.driver, 5, 0.3).until(
EC.presence_of_element_located(
(By.XPATH, xpath)))
@shot
def send_keys(self, step, xpath, text):
'''
文本输入框清空并填值
:param step:操作步骤
:param xpath: xpath
:param text: 填的值
:return:
'''
element = self.local_element(xpath)
element.clear()
element.send_keys(text)
@shot
def click(self, step, xpath):
'''
#点击操作
:param step: 操作步骤
:param xpath: xpath
:return:
'''
element = self.local_element(xpath)
element.click()
# self.driver.execute_script("arguments[0].click();", element)
# ActionChains(self.driver).move_to_element(element).click(element).perform()
@shot
def scroll_screen(self, step):
'''
#滚动窗口,滚到底
:param step:操作步骤
:return:
'''
js = "var q=document.documentElement.scrollTop=10000"
self.driver.execute_script(js)
@shot
def switch_to_frame(self, step, xpath):
'''
#切换到iframe里边
:param step:操作步骤
:param xpath: xpath
:return:
'''
element = self.local_element(xpath)
self.driver.switch_to.frame(element)
@shot
def switch_to_content(self, step):
'''
#切出iframe,回到默认页面
:param step:操作步骤
:return:
'''
self.driver.switch_to.default_content()
@shot
def select_by_index(self, step, xpath, index):
'''
#操作select下拉框,通过下标选择
:param step:操作步骤
:param xpath:xpath
:param index:下标
:return:
'''
element = self.local_element(xpath)
Select(element).select_by_index(index)
@shot
def select_by_value(self, step, xpath, value):
'''
#操作select下拉框,通过value值选择
:param step: 操作步骤
:param xpath: xpath
:param value: value值
:return:
'''
element = self.local_element(xpath)
Select(element).select_by_value(value)
@shot
def select_by_visible_text