# coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select class Pyse(object):
'''
Pyse framework for the main class, the original
selenium provided by the method of the two packaging,
making it easier to use.
''' def __init__(self, browser='chrome'):
'''
Run class initialization method, the default is proper
to drive the Firefox browser. Of course, you can also
pass parameter for other browser, Chrome browser for the "Chrome",
the Internet Explorer browser for "internet explorer" or "ie".
'''
# if browser == "firefox" or browser == "ff":
# driver = webdriver.Firefox()
# elif browser == "chrome":
# option = webdriver.ChromeOptions()
# option.add_argument("--start-maximized")
# driver = webdriver.Chrome(chrome_options=option)
# elif browser == "internet explorer" or browser == "ie":
# driver = webdriver.Ie()
# elif browser == "opera":
# driver = webdriver.Opera()
# elif browser == "phantomjs":
# driver = webdriver.PhantomJS()
# elif browser == 'edge':
# driver = webdriver.Edge()
# try:
# self.driver = driver
# except Exception:
# raise NameError(
# "Not found %s browser,You can enter 'ie', 'ff', 'opera', 'phantomjs', 'edge' or 'chrome'." % browser) if browser.lower() == 'chrome':
option = webdriver.ChromeOptions()
option.add_argument('--start-maximized')
driver = webdriver.Chrome(chrome_options=option)
elif browser.lower() == 'firefox' or browser.lower() == 'ff':
driver = webdriver.Firefox()
elif browser.lower() == 'ie' or browser.lower() == 'internet explorer':
driver = webdriver.Ie()
elif browser.lower() == 'opera':
driver = webdriver.Opera()
elif browser.lower() == 'phantomjs':
driver = webdriver.PhantomJS
elif browser.lower() == 'edge':
driver = webdriver.Edge()
try:
self.driver = driver
except Exception:
raise NameError(
"Not found %s browser,You can enter 'ie', 'ff', 'opera', 'phantomjs', 'edge' or 'chrome'." % browser) def element_wait(self, css, secs=5):
'''
Waiting for an element to display. Usage:
driver.element_wait("css=>#el",10)
'''
if "=>" not in css:
raise NameError("Positioning syntax errors, lack of '=>'.") by = css.split("=>")[0]
value = css.split("=>")[1] if by == "id":
WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.ID, value)))
elif by == "name":
WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.NAME, value)))
elif by == "class":
WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.CLASS_NAME, value)))
elif by == "link_text":
WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.LINK_TEXT, value)))
elif by == "xpath":
WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.XPATH, value)))
elif by == "css":
WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.CSS_SELECTOR, value)))
else:
raise NameError(
"Please enter the correct targeting elements,'id','name','class','link_text','xpath','css'.") def get_element(self, css):
'''
Judge element positioning way, and returns the element.
'''
if "=>" not in css:
raise NameError("Positioning syntax errors, lack of '=>'.") by = css.split("=>")[0]
value = css.split("=>")[1] if by == "id":
element = self.driver.find_element_by_id(value)
elif by == "name":
element = self.driver.find_element_by_name(value)
elif by == "class":
element = self.driver.find_element_by_class_name(value)
elif by == "link_text":
element = self.driver.find_element_by_link_text(value)
elif by == "xpath":
element = self.driver.find_element_by_xpath(value)
elif by == "css":
element = self.driver.find_element_by_css_selector(value)
else:
raise NameError(
"Please enter the correct targeting elements,'id','name','class','link_text','xpath','css'.")
return element def get(self, url):
'''
open 请求url. Usage:
driver.open("https://www.baidu.com")
'''
self.driver.get(url) def max_window(self):
'''
Set browser window maximized. Usage:
driver.max_window()
'''
self.driver.maximize_window() def set_window(self, wide, high):
'''
Set browser window wide and high. Usage:
driver.set_window(wide,high)
'''
self.driver.set_window_size(wide, high) def type(self, css, text):
'''
Operation input box 发送value Usage:
driver.type("css=>#el","selenium")
'''
self.element_wait(css)
el = self.get_element(css)
el.send_keys(text) def clear(self, css):
'''
Clear the contents of the input box. Usage:
driver.clear("css=>#el")
'''
self.element_wait(css)
el = self.get_element(css)
el.clear() def click(self, css):
'''
It can click any text / image can be clicked
Connection, check box, radio buttons, and even drop-down box etc.. Usage:
driver.click("css=>#el")
'''
self.element_wait(css)
el = self.get_element(css)
el.click() def right_click(self, css):
'''
Right click element. Usage:
driver.right_click("css=>#el")
'''
self.element_wait(css)
el = self.get_element(css)
ActionChains(self.driver).context_click(el).perform() def move_to_element(self, css):
'''
Mouse over the element. Usage:
driver.move_to_element("css=>#el")
'''
self.element_wait(css)
el = self.get_element(css)
ActionChains(self.driver).move_to_element(el).perform() def double_click(self, css):
'''
Double click element. Usage:
driver.double_click("css=>#el")
'''
self.element_wait(css)
el = self.get_element(css)
ActionChains(self.driver).double_click(el).perform() def drag_and_drop(self, el_css, ta_css):
'''
Drags an element a certain distance and then drops it. Usage:
driver.drag_and_drop("css=>#el","css=>#ta")
'''
self.element_wait(el_css)
element = self.get_element(el_css)
self.element_wait(ta_css)
target = self.get_element(ta_css)
ActionChains(self.driver).drag_and_drop(element, target).perform() def click_text(self, text):
'''
Click the element by the link text Usage:
driver.click_text("新闻")
'''
self.driver.find_element_by_partial_link_text(text).click() def close(self):
'''
Simulates the user clicking the "close" button in the titlebar of a popup
window or tab. Usage:
driver.close()
'''
self.driver.close() def quit(self):
'''
Quit the driver and close all the windows. Usage:
driver.quit()
'''
self.driver.quit() def submit(self, css):
'''
Submit the specified form. Usage:
driver.submit("css=>#el")
'''
self.element_wait(css)
el = self.get_element(css)
el.submit() def F5(self):
'''
Refresh the current page. Usage:
driver.F5()
'''
self.driver.refresh() def back(self):
self.driver.back() def go_on(self):
self.driver.forward() def js(self, script):
'''
Execute JavaScript scripts. Usage:
driver.js("window.scrollTo(200,1000);")
'''
self.driver.execute_script(script) def get_attribute(self, css, attribute):
'''
Gets the value of an element attribute. Usage:
driver.get_attribute("css=>#el","type")
'''
el = self.get_element(css)
return el.get_attribute(attribute) def get_text(self, css):
'''
Get element text information. Usage:
driver.get_text("css=>#el")
'''
self.element_wait(css)
el = self.get_element(css)
return el.text def get_display(self, css):
'''
Gets the element to display,The return result is true or false. Usage:
driver.get_display("css=>#el")
'''
self.element_wait(css)
el = self.get_element(css)
return el.is_displayed() def get_title(self):
'''
Get window title. Usage:
driver.get_title()
'''
return self.driver.title def get_url(self):
'''
Get the URL address of the current page. Usage:
driver.get_url()
'''
return self.driver.current_url def get_windows_img(self, file_path):
'''
Get the current window screenshot. Usage:
driver.get_windows_img()
'''
self.driver.get_screenshot_as_file(file_path) def wait(self, secs):
'''
Implicitly wait.All elements on the page. Usage:
driver.wait(10)
'''
self.driver.implicitly_wait(secs) def accept_alert(self):
'''
Accept warning box. Usage:
driver.accept_alert()
'''
self.driver.switch_to.alert.accept() def dismiss_alert(self):
'''
Dismisses the alert available. Usage:
driver.dismiss_alert()
'''
self.driver.switch_to.alert.dismiss() def switch_to_frame(self, css):
'''
Switch to the specified frame. Usage:
driver.switch_to_frame("css=>#el")
'''
self.element_wait(css)
iframe_el = self.get_element(css)
self.driver.switch_to.frame(iframe_el) def parent_iframe(self):
self.driver.switch_to.parent_frame() def switch_to_frame_out(self):
'''
Returns the current form machine form at the next higher level.
Corresponding relationship with switch_to_frame () method. Usage:跳转到最外层的页面
driver.switch_to_frame_out()
'''
self.driver.switch_to.default_content() def open_new_window(self, css):
'''
Open the new window and switch the handle to the newly opened window. Usage:
driver.open_new_window()
'''
original_windows = self.driver.current_window_handle
el = self.get_element(css)
el.click()
all_handles = self.driver.window_handles
for handle in all_handles:
if handle != original_windows:
self.driver.switch_to.window(handle) # def _save_png(self, name):
# self.get_windows_img(name) def wait_and_save_exception(self, css, name):
try:
self.element_wait(css, secs=5)
return True
except Exception as e:
from lib.core.path import PICTUREPATH
self.get_windows_img(PICTUREPATH + name + '.jpg')
return False def wait_and_exception(self, css):
try:
self.element_wait(css, secs=10)
return True
except Exception as e:
return False def select_by_value(self, css, value):
self.element_wait(css)
el = self.get_element(css)
Select(el).select_by_value(value) if __name__ == '__main__':
pyse = Pyse('Chrome')
pyse.get("http://")
pyse.get_element('css=>#i1').send_keys(111111)
time.sleep(3)
pyse.quit()
  

UI自动化-selenium-api封装pyse框架的更多相关文章

  1. UI自动化selenium

    1.什么是UI自动化?模拟人用代码的方式去操作页面2.为什么要做UI自动化?后期迭代的时候,老功能比较多,人工维护成本较大,重复性工作较多,这个时候就考虑因为UI自动化3.什么时候做UI自动化?项目稳 ...

  2. UI自动化(selenium+python)之元素定位的三种等待方式

    前言 在UI自动化过程中,常遇到元素未找到,代码报错的情况.这种情况下,需要用等待wait. 在selenium中可以用到三种等待方式即sleep,implicitly_wait,WebDriverW ...

  3. UI自动化学习笔记- UnitTest单元测试框架详解

    一.UnitTest基本使用 1. UnitTest框架 1.1 什么是框架 说明: 框架英文单词frame 为解决一类事情的功能集合 1.2什么是UnitTest框架 概念:UnitTest是pyt ...

  4. UI自动化(selenium+python)之浏览器驱动chromedriver安装和配置

    一.安装selenium 前提是已安装Python,python安装 自行百度,这里不概述 安装好python后,cmd打开终端窗口-->pip命令安装 :pip install seleniu ...

  5. UI自动化填写问卷(selenium)+定时任务(懒人必备)

    1.自动填报 UI自动化 selenium 开发程序动机:天天有人催着填写问卷,弄的头大.主要还是懒的每天一个个去填写内容. 开发总时长:2个小时:学习+开发+修改 遇到的小问题: 在自动化填写地图的 ...

  6. Airtest 网易 UI 自动化工具 Airtest 浅用记录

    一 使用目的 该工具主要是面向游戏UI测试基于图像识别,如游戏框架unity,Cocos-js以及网易内部的游戏框架同时也支持原生Android App 的基于元素识别的UI自动化测试.本文主要使用目 ...

  7. 网易 UI 自动化工具 Airtest 浅用记录

    一 使用目的 该工具主要是面向游戏UI测试基于图像识别,如游戏框架unity,Cocos-js以及网易内部的游戏框架 同时也支持原生Android App 的基于元素识别的UI自动化测试. 本文主要使 ...

  8. poium测试库之JavaScript API封装原理

    poium一直我在维护的一个开源项目,它的定位是以极简的方式在自动化项目中Page Objects设计模式.我在之前的文章中也有介绍. 本篇文章主要介绍一个JavaScript元素操作的封装原理. 为 ...

  9. selenium(12)-web UI自动化项目实战(PO模式,代码封装)

    web UI自动化项目实战-项目 项目使用禅道,所以你需要搭建1个禅道,搭建禅道的方法和步骤见 https://www.cnblogs.com/xinhua19/p/13151296.html 搭建U ...

随机推荐

  1. 常用的排序算法介绍和在JAVA的实现(一)

    一.写随笔的原因:排序比较常用,借此文介绍下排序常用的算法及实现,借此来MARK一下,方便以后的复习.(本人总是忘得比较快) 二.具体的内容: 1.插入排序 插入排序:在前面已经排好序的序列中找到合适 ...

  2. httpd统计的其他方法,awk,sed等

    1. https://stackoverflow.com/questions/345546/how-to-get-requests-per-second-for-apache-in-linux 2. ...

  3. Python基础:深浅拷贝

    对于数字.字符串深浅拷贝: import copy num = 0 copy_num = copy.copy(num) print("These are normal copy") ...

  4. oracle 数据库启动停止小结

    ---登录sqlplus sqlplus  /nolog conn / as sysdba shutdown immediate --启动数据库有两种方式 startup 会自动完成重启数据库的所有步 ...

  5. docker 创建容器时指定容器ip

    Docker创建容器时默认采用bridge网络,自行分配ip,不允许自己指定. 在实际部署中,我们需要指定容器ip,不允许其自行分配ip,尤其是搭建集群时,固定ip是必须的. 我们可以创建自己的bri ...

  6. 关于深度学习之TensorFlow简单实例

    1.对TensorFlow的基本操作 import tensorflow as tf import os os.environ[" a=tf.constant(2) b=tf.constan ...

  7. 对Spring Boot 及Mybatis简单应用

    因为没有系统的学习过SpringBoot,在对照一个别人的SpringBoot项目,进行简单的搭建及使用. 1.首先创建SpringBoot项目之后,这里会有默认的启动类,基本不需要配置,在类的上边有 ...

  8. ICSharpCode.SharpZipLib.Zip 压缩文件

    public class ZipFileHelper { List<string> urls = new List<string>(); void Director(strin ...

  9. CSS3 的动画属性

    通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片.Flash 动画以及 JavaScript. ㈠@keyframes 规则 ⑴浏览器支持 Firefox 支持替代的 @-moz-ke ...

  10. node.js之CommonJS

    1.CommonJS 我们只是在js文件中写下console.log('This is a test.');这句代码,node.js执行该js文件时: ( function(exports, requ ...