二次开发的Selenium Demo版本
文件名你们自己命名就好,至于为什么要重写强制位移的函数呢,是因为Mac上Selenium不支持拖拽,只能这样做了,4个文件
----------------------------------------------------------------------------------------------
def login(auto):
"""
遍历上方login_config
按照其格式进行自动化操作
"""
for item in login_config:
for key, value in item.items():
try:
# 把auto与key组合起来并传入value执行
# 如auto.refresh(2)
getattr(auto, key)(value)
except Exception as error:
print(error)
def Test(auto):
"""
遍历上方login_config
按照其格式进行自动化操作
"""
for item in Test_config:
for key, value in item.items():
try:
# 把auto与key组合起来并传入value执行
# 如auto.refresh(2)
getattr(auto, key)(value)
except Exception as error:
print(error)
----------------------------------------------------------------------------------------------
# 拖动单行文本
Test1_config = [
{"drag_and_drop": ["//*[@id=\"root\"]/div/div/div[4]/div/div/div",
"/html/body/div/div/div[2]/div[3]/div[1]/div[1]/div/div[1]", 2]},
{"clear_xpath": ["//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[6]/div[2]/span/span/span[1]/input",
2]},
{"write_in_xpath": [
"//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[6]/div[2]/span/span/span[1]/input", "supplier",
2]},
{"click_xpath": ["//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[7]/span", 2]},
{"write_in_xpath": ["//*[@id='root']/div/div[2]/div[3]/div[2]/div/div[3]/div[1]/div[7]/span/input", "supplier_name",
2]}
]
# 拖动多行文本
Test2_config = [
{"drag_and_drop1": ["//*[@id='root']/div/div[1]/div[4]/div[2]/div/div",
"/html/body/div/div/div[2]/div[3]/div[1]/div[1]/div/div[1]", 2]},
{"clear_xpath": [
"//span[@class='ant-input-wrapper ant-input-group']//input[@class='ant-input' and @value='Multi-line Text']",
2]},
{"write_in_xpath": [
"//span[@class='ant-input-wrapper ant-input-group']//input[@class='ant-input' and @value='Multi-line Text']",
"Notes",
2]},
{"click_xpath": ["//div[@class='bg-c-nodeproperties']/span[@class='ant-input-affix-wrapper", 2]},
{"write_in_xpath": ["//div[@class='bg-c-nodeproperties']/span[@class='ant-input-affix-wrapper']/input",
"supplier_notes",
2]}
]
----------------------------------------------------------------------------------------------
import sys
from selenium import webdriver
from time import sleep
from datetime import datetime
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui # 重写的强制的位移函数
def move_mouse_to_element(driver, target_element):
element_y_offset = int(target_element.location.get("y"))
element_x_offset = int(target_element.location.get("x"))
element_width = int(target_element.size.get("width"))
element_height = int(target_element.size.get("height"))
inner_height = int(driver.execute_script("return innerHeight"))
screen_height = int(driver.execute_script("return outerHeight"))
window_height = int(driver.execute_script("return window.screenY"))
pyautogui.moveTo(element_x_offset + element_width / 2,
element_y_offset + screen_height - inner_height - window_height + element_height / 2 + 500,
duration=0.5) return target_element # 重写的强制位移的第二个函数
def move_mouse_to_element1(driver, target_element):
element_y_offset = int(target_element.location.get("y"))
element_x_offset = int(target_element.location.get("x"))
element_width = int(target_element.size.get("width"))
element_height = int(target_element.size.get("height"))
inner_height = int(driver.execute_script("return innerHeight"))
screen_height = int(driver.execute_script("return outerHeight"))
window_height = int(driver.execute_script("return window.screenY"))
pyautogui.moveTo(element_x_offset + element_width / 2 + 100,
element_y_offset + screen_height - inner_height - window_height + element_height / 2,
duration=0.5) return target_element # 和第一个位移函数配套用的
def drag_and_drop(driver, action, source, target):
move_mouse_to_element(driver, target)
action.drag_and_drop(source, target).perform() # 和第二个位移函数配套用的
def drag_and_drop1(driver, action, source, target):
move_mouse_to_element1(driver, target)
action.drag_and_drop(source, target).perform() # 输出的Log
def write_finish_info(func):
# @wraps(func)
def wrapper(self, *args):
func(self, *args)
now = datetime.now().strftime("%X")
name = "log_{}.txt".format('')
log = "[{}] 当前执行步骤为{}:{} {}\n".format(now, step, func.__name__, args)
sys.stdout.write("[{}] 当前执行步骤为:{} {}\n".format(now, func.__name__, args))
with open(name, "a+") as f:
f.write(log) return wrapper # 自动化所有用到的方法
class AutoTest:
"""
自动化测试的主要脚本
所有行为在此类中编写
具体的操作及操作值,由配置表控制
""" def __init__(self, driver):
self.driver = driver
self.action = ActionChains(driver) @write_finish_info
def get_url(self, entry):
"""
打开网页的方法
传入必须为列表list,会检测list长度是否2
如若不为list或长度不对,则中断后续操作
若不想中断,则把raise ValueError("get_url gets a wrong value")改为return即可
"""
if not isinstance(entry, list) and len(entry) != 2:
raise ValueError("get_url gets a wrong value")
url, s = tuple(entry)
self.driver.get(url)
sleep(s) @write_finish_info
def maximize_window(self, s):
sleep(s)
self.driver.maximize_window() @write_finish_info
def write_in_xpath(self, entry):
"""
在xpath写入值的方法
传入必须为list,会检测list长度是否为3
"""
if not isinstance(entry, list) and len(entry) != 3:
raise ValueError("write_in_xpath gets a wrong value")
xpath, words, s = tuple(entry)
try:
self.driver.find_element_by_xpath(xpath).send_keys(words)
except Exception as e:
print(e.error) sleep(s) @write_finish_info
def write_in_id(self, entry):
"""
在id写入值的方法
传入必须为list,会检测list长度是否为3
"""
if not isinstance(entry, list) and len(entry) != 3:
raise ValueError("write_in_id gets a wrong value")
id, words, s = tuple(entry)
self.driver.find_element_by_id(id).send_keys(words)
sleep(s) @write_finish_info
def click_xpath(self, entry):
"""
点击xpath的方法
传入必须为list,会检测list长度是否为2
"""
if not isinstance(entry, list) and len(entry) != 2:
raise ValueError("click_xpath gets a worng value")
xpath, s = tuple(entry)
self.driver.find_element_by_xpath(xpath).click()
sleep(s) @write_finish_info
def clear_xpath(self, entry):
"""
点击xpath的方法
传入必须为list,会检测list长度是否为2
"""
if not isinstance(entry, list) and len(entry) != 2:
raise ValueError("click_xpath gets a worng value")
xpath, s = tuple(entry)
self.driver.find_element_by_xpath(xpath).clear()
sleep(s) @write_finish_info
def refresh(self, s):
"""
在等待s秒后,refresh
"""
sleep(s)
self.driver.refresh() @write_finish_info
def drag_and_drop(self, entry):
if not isinstance(entry, list) and len(entry) != 4:
raise ValueError("drag_and_drop gets a wrong value")
action = ActionChains(self.driver)
source_xpath, target_xpath, s = tuple(entry)
source = self.driver.find_element_by_xpath(source_xpath)
target = self.driver.find_element_by_xpath(target_xpath)
move_mouse_to_element(self.driver, target)
action.drag_and_drop(source, target).perform()
sleep(s) @write_finish_info
def drag_and_drop1(self, entry):
if not isinstance(entry, list) and len(entry) != 3:
raise ValueError("drag_and_drop gets a wrong value")
action = ActionChains(self.driver)
source_xpath, target_xpath, s = tuple(entry)
source = self.driver.find_element_by_xpath(source_xpath)
target = self.driver.find_element_by_xpath(target_xpath)
move_mouse_to_element1(self.driver, target)
action.drag_and_drop(source, target).perform()
sleep(s)
----------------------------------------------------------------------------------------------
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.webdriver.common.action_chains import ActionChains
from Login import *
import time driver = webdriver.Chrome(executable_path="/Users/jerry/PycharmProjects/untitled/venv/Goole/chromedriver") auto = AutoTest(driver) while True:
step = 0
login(auto, step)
有啥疑问欢迎提出,共同进步
by-咯咯
二次开发的Selenium Demo版本的更多相关文章
- efront二次开发记要
efront系统是一套开源的在线学习系统,是用PHP编写的,内含“考试”功能.该系统的开源的是社区版,虽然看上去功能强大,但使用起来却很不符合国情.为了让公司使用,先做了一次最简化的二次开发,由于是最 ...
- 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单、书签等相关问题
2015-03-02 更新文章,由于需求修改,更改了flexpaper插件,故增加第9.10.11小节,下载代码时请注意. 先废话几句.最近用到文档在线浏览功能,之前用的是print2flash(一个 ...
- 浩顺AC671指纹考勤机二次开发(demo)
关于考勤机 AC671,是新换的机器,以前的那部机器,通过网络死活连接不上,换了AC671网络连接是好用了.但是,我要吐槽 浩顺的考勤机应该是卖了很多了吧,可是自带的软件太不给力,最后分析出来的数据一 ...
- phpcms v9版本二次开发四步曲
今晚看了一下PHPCMS V9版本,做一个实例抛砖引玉,其实很简单,以下是二次开发的一个实例以旅游模块为例1. 在phpcms\modules目录下建立一个文件夹tour2. 在phpcms\m ...
- 基于redis实现tomcat8及以上版本的tomcat集群的session持久化实现(tomcat-redis-session-manager二次开发)
前言: 本项目是基于jcoleman的tomcat-redis-session-manager二次开发版本 1.修改了小部分实现逻辑 2.去除对juni.jar包的依赖 3.去除无效代码和老版本tom ...
- Skyline 7 版本TerraExplorer Pro二次开发快速入门
年底了,给大家整理了一下Skyline 7版本的二次开发学习初级入门教程,献给那些喜欢学习的年轻朋友. 我这整理的是Web控件版本的开发示例,里面页面代码保存成html,都可以直接运行的. 测试使用的 ...
- 浩顺考勤机二次开发(第二版,附实测可用的demo)
1.背景 之前写过一次浩顺考勤机的二次开发,不过那个版本还是有一些问题,后来更换了新的考勤机,又拿到了新的二次开发包,所以就有了这次这个版本 2.关于考勤机的一些说明 2.1 首先要给考勤机设定ip, ...
- 深入理解基于selenium的二次开发
对于做web端自动化测试的人来说,可能接触selenium比QTP还要多,但是我们在做基于selenium的二次开发的时候,经常会说到二次开发是为了易于维护,很多人可能不懂得维护的价值是什么,和到底要 ...
- openfire spark 二次 开发 服务插件
==================== 废话 begin ============================ 最近老大让我为研发平台增加即时通讯功能.告诉我用comet 在web端实现即 ...
随机推荐
- Ubuntu16手动安装OpenStack
记录大佬的博客全文转载于https://www.voidking.com/dev-ubuntu16-manual-openstack-env/ 前言 <Ubuntu16安装OpenStack&g ...
- 个人第四次作业——Alpha测试
Alpha项目测试 这个作业属于哪个课程 链接 这个作业要求在哪里 链接 团队名称 愿头发与你我同在 这个作业的目标 测试非本组的另外三组项目 姓名 张伟 学号 201731024216 测试报告 一 ...
- oc---instancetype和id的异同
[instancetype和id的异同] 相同点:都可以作为方法的返回类型. 不同点: (1)instancetype可以返回方法所在类相同类型的对象,id只能返回未知类型的对象: (2)instan ...
- CentOS7主机名的查看和修改
CentOS7主机名的查看和修改 在CentOS7中,有三种定义的主机名: 静态的(Static hostname) "静态"主机名也称为内核主机名,是系统在启动时从/etc/ho ...
- SpringBoot任务篇Ⅴ --- 异步任务、定时任务、邮件任务
Java的任务在项目中需要用到的地方很多,比如,每月月末的财务报表给财务部门,定时给领导发个邮件短信等等.这时候我们就需要用到任务了,任务调度本身涉及到多线程并发.运行时间规则制定和解析.场景保持与恢 ...
- 【阿里云IoT+YF3300】12.阿里云IoT Studio入门介绍
阿里云IoT Studio是针对物联网场景提供的生产力工具,可覆盖各个物联网行业核心应用场景,帮助您高效经济地完成设备.服务及应用开发.物联网开发服务提供了移动可视化开发.Web可视化开发.服务开发与 ...
- Java:多线程概述与创建方式
目录 Java:多线程概述与创建方式 进程和线程 并发与并行 多线程的优势 线程的创建和启动 继承Thread类 start()和run() 实现Runnable接口 实现Callable接口 创建方 ...
- 暑假第四周总结(HDFS编程实践,安装HBASE)
本周根据书上以及教程的提示,对HDFS进行了编程实践,将教程所给的代码(判断文件是否存在,创建文件,读取文件)进行了应用,根据视频的讲解,对一些简单的语句有了一定的了解,但还是比较生疏.另外还根据提示 ...
- springboot使用servlet
基于注解方式: 基于配置类:
- java架构之路-(netty专题)netty的基本使用和netty聊天室
上次回顾: 上次博客,我们主要说了我们的IO模型,BIO同步阻塞,NIO同步非阻塞,AIO基于NIO二次封装的异步非阻塞,最重要的就是我们的NIO,脑海中应该有NIO的模型图. Netty概念: Ne ...