文件名你们自己命名就好,至于为什么要重写强制位移的函数呢,是因为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. [洛谷P4617] [COCI2017-2018#5] Planinarenje

    Description \(Mirko\) 和 \(Slavko\) 喜欢一起去远足. \(Mirko\) 偏好攀登山峰,而 \(Slavko\) 偏爱山谷.因此每次他们登上一座山峰后,\(Slavk ...

  2. 「 从0到1学习微服务SpringCloud 」10 服务网关Zuul

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config 「 从0到1学习微服务SpringCloud 」07 RabbitM ...

  3. 对于Makefile的基本使用

    上课不听讲的后果就是课下疯狂补知识了 原文来自https://www.cnblogs.com/chenguanfu/p/4415072.html 在Windows下,只需要简单的点击以下make,re ...

  4. Linux Centos7 在桌面添加快捷方式

    当时,刚刚安装好centos7,又下载好了jb家的软件,但是每一次都要用命令行才能运 我想要的是下面的效果,那是我后来才研究出来的 我看到了自动生成的为什么可以用,我的打开了源文件研究了一下 第一,先 ...

  5. springboot中使用Caffeine本地缓存

    Caffeine是使用Java8对Guava缓存的重写版本性能有很大提升 一 依赖 <dependency> <groupId>org.springframework.boot ...

  6. ubuntu-14.04.6配置IP

    配置环境与要求: 网卡列表如下: eth0:DHCP模式 eth1:静态模式 网络概况与要求: 192.168.2.0/24为外网(获取网络资源) 10.5.1.0/24为内网(终端服务管理) 系统默 ...

  7. 自己动手写个异步IO函数 --(基于 c# Task)

    前言    对于服务端,达到高性能.高扩展离不开异步.对于客户端,函数执行时间是1毫秒还是100毫秒差别不大,没必要为这一点点时间煞费苦心.对于异步,好多人还有误解,如: 异步就是多线程:异步就是如何 ...

  8. LeetCode227:基本计算器II

    感觉自己的思路还不错,比较简单清晰,代码量也比较少,没有用到记录运算符的变量或栈,就想把这个思路发一下博客. 题目: 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数 ...

  9. js - 构造函数-静态属性/方法-原型对象 - 前端第八课

    先来个普通对象 var duixiang={ a:"111", b:function (s) { return s+"886"; } }; console.lo ...

  10. DaSiamRPN学习

    9月14日,2018年视觉目标跟踪挑战赛(Visual-Object-Tracking Challenge 2018)的结果在ECCV Workshop上揭晓.VOT2018共设三项任务:Baseli ...