Selenium应用代码(常见封装的方法一)
常见封装的方法:输入、点击、判断元素是否存在、根据句柄切换窗口、根据title切换窗口、滚动窗口、截图
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.util.thread.Timeout;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import Study.Day02.TestTabnumDetail;
public class CommonClass {
//封装元素输入的方法
public static int t = 1;
static String dir="F:\\Users\\Wangtest\\screenshots\\dianzhang";
public static void sendKeys(WebDriver driver,By by, String value){
driver.findElement(by).sendKeys(value);
}
//封装元素点击的方法
public static void click(WebDriver driver,By by){
driver.findElement(by).click();
}
//封装判断元素是否存在的方法
public boolean doesWebElementExist(WebDriver driver,By by){
try {
driver.findElement(by);
return false;
} catch (Exception e) {
// TODO: handle exception
return true;
}
}
//封装根据句柄切换窗口的方法
public String getWindowHandle(WebDriver driver){
String currentWindow = driver.getWindowHandle();
return currentWindow;
}
public static void SwitchNewwindow(WebDriver driver){
//得到当前句柄
String currentWindow = driver.getWindowHandle();
//得到所有窗口的句柄
Set<String> handles = driver.getWindowHandles();
//排除当前窗口的句柄,则剩下是新窗口
Iterator<String> it = handles.iterator();
while(it.hasNext()){
if(currentWindow == it.next()) continue;
driver.switchTo().window(it.next());
}
}
//封装通过title切换窗口
public boolean switchToWindow(WebDriver driver,String windowTitle){
boolean flag = false;
try {
String currentHandle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
for (String s : handles) {
if (s.equals(currentHandle))
continue;
else {
driver.switchTo().window(s);
if (driver.getTitle().contains(windowTitle)) {
flag = true;
System.out.println("Switch to window: " + windowTitle + " successfully!");
break; }
else
continue;
}
}
} catch (NoSuchWindowException e) {
System.out.printf("Window:" + windowTitle+ " cound not found!", e.fillInStackTrace());
flag = false;
}
return flag;
}
//将滚动条滚到适合的位置
public static void setScroll(WebDriver driver,int height){
try {
// String setscroll = "document.documentElement.scrollTop=" + height;
String setscroll = "document.body.scrollTop=" + height;
JavascriptExecutor jse=(JavascriptExecutor) driver;
jse.executeScript(setscroll);
} catch (Exception e) {
System.out.println("Fail to set the scroll.");
}
}
// public void TakeScreenShot(WebDriver driver,WebElement element) throws IOException {
// File screen = ((TakesScreenshot) ]this.driver).getScreenshotAs(OutputType.FILE);
//
// Point p = element.getLocation();
//
// int width = element.getSize().getWidth();
// int height = element.getSize().getHeight();
//
// Rectangle rect = new Rectangle(width, height);
//
// BufferedImage img = null;
//
// img = ImageIO.read(screen);
//
// BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width,
// rect.height);
//
// ImageIO.write(dest, "png", screen);
//
// File f = null;
//
// f = new File(dir+getDateTime()+"_"+t+".jpg");
//
// FileUtils.copyFile(screen, f);
//
// }
//获取当前时间
public static String getDateTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
return df.format(new Date());
}
//封装截图的方法
public static void ScreenShot(WebDriver driver){
File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShot, new File(dir+getDateTime()+"_"+t+".jpg"));
++t;
} catch (IOException e) {
e.printStackTrace();
}
}
}
Selenium应用代码(常见封装的方法一)的更多相关文章
- Selenium应用代码(常见封装的方法二)
滚动窗口: //将滚动条滚到适合的位置 , 方法一 public static void setScroll(WebDriver driver,int height){ try { // String ...
- Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载
元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...
- vue-axios的总结及项目中的常见封装方法。
前言 我们知道 vue 2.0版本开始推荐使用 axios 来完成前端 ajax 请求,axios 是一个基于Promise 的 http 库,可以用在浏览器和 node.js 中,axios 成为v ...
- Selenium_用selenium webdriver实现selenium RC中的类似的方法
最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...
- iOS开发学习--纯代码 UIScrollView 无限循环的实现——代码类封装
一个简单的利用UIScrollView 实现的无线滚动banner,下面的代码实现,因为封装问题,对两个及一下的view 支持出了一点问题(view是传参进来的,不可以生成两份),但是原理是正确的,智 ...
- Visual Studio快速封装字段方法
在面向对象的编程中我们常常要将各个字段封装为属性,但是当字段多的时候往往这个重复的操作会大大降低我们的开发效率,那么如何才能快速的封装字段呢?下面就给大家2个解决方法: 1.使用封装字段方法: 选中字 ...
- C# 加密总结 一些常见的加密方法
C# 加密总结 一些常见的加密方法 一 散列数据 代码如下: ? private static string CalculateSHA512Hash(string input) { ...
- JavaScript基础:BOM的常见内置方法和内置对象
本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. BOM的介绍 JavaScript的组成 JavaScript基础分为三 ...
随机推荐
- 微软的TransactionScope类是个好玩意
最近发现微软自带的TransactionScope(.Net Framework 2之后)是个好东东,提供的功能也很强大. 首先说说TransactionScope是什么,并能为我们做什么事情.其实看 ...
- Cheatsheet: 2017 05.01 ~05.31
Web Configuring Your .npmrc for an Optimal Node.js Environment Web Developer Security Checklist HTTP ...
- final 、finalize和finally的区别
2019-04-1217:29:40 (1)final用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承.内部类要访问局部变量,局部变量必须定义成final类型,比如一段代码 (2) ...
- Python爬虫教程-32-Scrapy 爬虫框架项目 Settings.py 介绍
本篇介绍项目开发的过程中,对 Setting 文件的配置和使用 Python爬虫教程-32-Scrapy 爬虫框架项目 Settings.py 介绍 settings.py 文件的使用 想要详细查看 ...
- flask框架下的jinja2模板引擎(1)(模板渲染)
#转载请留言联系 模板是什么? 在 flask 框架中,视图函数有两个作用:处理业务逻辑和返回响应内容.在大型应用中,把业务逻辑和表现内容放在一起,会增加代码的复杂度和维护成本.模板作用即是承担视图函 ...
- 程序员装B指南(转载)
转自:http://www.oschina.net/question/615783_115390 一.准备工作 "工欲善其事必先利其器." 1.电脑不一定要配置高,但是双屏是必须的 ...
- tcp-full.cc
ns2--tcp-full.cc /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */ /* * Copy ...
- redis复制+Sentinel搭建
1:实验环境 测试环境两台: master:172.16.16.34 slave:172.16.16.35 redis版本:redis3.2 要搭建的环境是,redis简单主从复制 2:安装redis ...
- Apache POI使用
使用apache poi解析 Excel文件: package excellucene; import java.io.File; import java.io.FileInputStream; im ...
- 在Server2012R2上导入Server2008R2的HyperV虚拟机
Importing Windows 2008 R2 Hyper-V VM Into Windows 8.1 For the purposes of this post, let’s try and i ...