文件名你们自己命名就好,至于为什么要重写强制位移的函数呢,是因为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版本的更多相关文章

  1. efront二次开发记要

    efront系统是一套开源的在线学习系统,是用PHP编写的,内含“考试”功能.该系统的开源的是社区版,虽然看上去功能强大,但使用起来却很不符合国情.为了让公司使用,先做了一次最简化的二次开发,由于是最 ...

  2. 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单、书签等相关问题

    2015-03-02 更新文章,由于需求修改,更改了flexpaper插件,故增加第9.10.11小节,下载代码时请注意. 先废话几句.最近用到文档在线浏览功能,之前用的是print2flash(一个 ...

  3. 浩顺AC671指纹考勤机二次开发(demo)

    关于考勤机 AC671,是新换的机器,以前的那部机器,通过网络死活连接不上,换了AC671网络连接是好用了.但是,我要吐槽 浩顺的考勤机应该是卖了很多了吧,可是自带的软件太不给力,最后分析出来的数据一 ...

  4. phpcms v9版本二次开发四步曲

    今晚看了一下PHPCMS V9版本,做一个实例抛砖引玉,其实很简单,以下是二次开发的一个实例以旅游模块为例1.   在phpcms\modules目录下建立一个文件夹tour2.  在phpcms\m ...

  5. 基于redis实现tomcat8及以上版本的tomcat集群的session持久化实现(tomcat-redis-session-manager二次开发)

    前言: 本项目是基于jcoleman的tomcat-redis-session-manager二次开发版本 1.修改了小部分实现逻辑 2.去除对juni.jar包的依赖 3.去除无效代码和老版本tom ...

  6. Skyline 7 版本TerraExplorer Pro二次开发快速入门

    年底了,给大家整理了一下Skyline 7版本的二次开发学习初级入门教程,献给那些喜欢学习的年轻朋友. 我这整理的是Web控件版本的开发示例,里面页面代码保存成html,都可以直接运行的. 测试使用的 ...

  7. 浩顺考勤机二次开发(第二版,附实测可用的demo)

    1.背景 之前写过一次浩顺考勤机的二次开发,不过那个版本还是有一些问题,后来更换了新的考勤机,又拿到了新的二次开发包,所以就有了这次这个版本 2.关于考勤机的一些说明 2.1 首先要给考勤机设定ip, ...

  8. 深入理解基于selenium的二次开发

    对于做web端自动化测试的人来说,可能接触selenium比QTP还要多,但是我们在做基于selenium的二次开发的时候,经常会说到二次开发是为了易于维护,很多人可能不懂得维护的价值是什么,和到底要 ...

  9. openfire spark 二次 开发 服务插件

    ====================  废话 begin   ============================ 最近老大让我为研发平台增加即时通讯功能.告诉我用comet 在web端实现即 ...

随机推荐

  1. mysql的压缩版安装

    MYSQL压缩版 自己建立: data(位于mysql的bin一层文件夹),my.ini(文本) my.ini(下面是文本内容) [client] port=3306 default-characte ...

  2. Qt Installer Framework翻译(4)

    教程:创建安装程序 本教程描述如何为一个小项目创建一个简单的安装程序: 本节描述创建安装程序所必须完成的步骤: 创建一个包文件夹,其中将包含所有配置文件和可安装的包. 创建一个配置文件,其中包含有关如 ...

  3. Windows Terminal入门

    目录 0.引言 1.简易安装 2.初识WT 3.初识Settings 3.1全局配置 3.2每一个终端配置 3.3配色方案 3.4键位绑定 4.连接云服务器 5.连接WSL 6.玩转Emoji 0.引 ...

  4. (转)Bypass xss过滤的测试方法

    from wooyun//五道口杀气 · 2014/01/02 19:16 0x00 背景 本文来自于<Modern Web Application Firewalls Fingerprinti ...

  5. Unity SurfaceShader详解

    声明:文章主要是总结手游开发的经验,只涉及到了前向渲染.未涉及延迟渲染. Unity的Surface Shader本质上就是VS/PS.只不过Unity经过精心设计,将shader划分为了几个关键部分 ...

  6. Java多态之Father f=new Son();

    成员变量静态方法看左边,非静态方法编译看左边,运行看右边. 左边Father f其实是定义了一个Father类的对象,而右边new Son()可以只理解为是一个重写了Father类方法的对象. 因此, ...

  7. HanLP《自然语言处理入门》笔记--1.新手上路

    1. 新手上路 自然语言处理(Natural Language Processing,NLP)是一门融合了计算机科学.人工智能及语言学的交叉学科,它们的关系如下图所示.这门学科研究的是如何通过机器学习 ...

  8. vue 新建脚手架项目npm命令

    使用国外原镜像 npm install -g @vue/cli   //yarn global add @vue/cli            使用淘宝镜像 cnpm install -g @vue/ ...

  9. lua学习之类型与值篇

    类型与值 lua 是动态类型的语言 在语言中没有类型定义的语法 每个值都携带有它的类型信息 8种基础类型 用 type 可以返回这个值的类型的名称 将一个变量用于不同类型,通常会导致混乱的代码 但合理 ...

  10. selenium 调用JavaScript代码

    selenium 调用JavaScript代码 调用JavaScript方法有两种: execute_script(): 方法解释:是同步方法,用它执行js代码会阻塞主线程执行,直到js代码执行完毕. ...