二次开发的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端实现即 ...
随机推荐
- 【Docker】 windows10 docker 使用
原文地址:https://www.aliyun.com/jiaocheng/872144.html 摘要:docker之前在Windows和MacOS上的版本都是通过Linux虚拟机的形式,而现在Do ...
- 分布式RPC系统框架Dubbo
导读 Apache Dubbo是一款高性能.轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现. dubbo官网:点我直达 第一 ...
- 自用代码css获取任意网址的/favicon.ico的方法教程
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用这种办法获取 如果有好的方法望评论告知 谢谢 <img :ng-src="'http://'+list.url+'/fav ...
- Day9-Python3基础-多线程、多进程
1.进程.与线程区别 2.python GIL全局解释器锁 3.线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者消费者模型 Que ...
- Spring事务梳理
Spring事务传播行为 概述 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为 事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数 ...
- ios--->ios消息机制(NSNotification 和 NSNotificationCenter)
问题的背景 IOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改 ...
- 机器学习李航——Adaboost课本例题实现
例8.1Adaboost的例子 注意求D3或者D4的时候只需要把w替换一下就行,记得还得改阈值.这个代码算个半自动的,因为还需要手动改一下. import numpy as np def getA(e ...
- ROC 曲线
Receiver Operating Characteristic (接收机操作特性曲线) 是以虚警率为横轴,以击中率为纵轴,长成如下模样: 所谓击中率(hit)是指将正样本判断为正样本的比例,而虚警 ...
- Kafka动态配置实现原理解析
问题导读 Apache Kafka在全球各个领域各大公司获得广泛使用,得益于它强大的功能和不断完善的生态.其中Kafka动态配置是一个比较高频好用的功能,下面我们就来一探究竟. 动态配置是如何设计的? ...
- WPF另类实现摄像头录像
WPF中使用第三方控件来直接进行录像的控件没有找到(aforgenet好像不维护了?WPFMediaKit好像只能实现摄像头拍照.收费的控件没有使用,不做评论.) 通过百度(感谢:https://ww ...