常见封装的方法:输入、点击、判断元素是否存在、根据句柄切换窗口、根据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应用代码(常见封装的方法一)的更多相关文章

  1. Selenium应用代码(常见封装的方法二)

    滚动窗口: //将滚动条滚到适合的位置 , 方法一 public static void setScroll(WebDriver driver,int height){ try { // String ...

  2. Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载

    元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...

  3. [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)

    转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...

  4. vue-axios的总结及项目中的常见封装方法。

    前言 我们知道 vue 2.0版本开始推荐使用 axios 来完成前端 ajax 请求,axios 是一个基于Promise 的 http 库,可以用在浏览器和 node.js 中,axios 成为v ...

  5. Selenium_用selenium webdriver实现selenium RC中的类似的方法

    最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...

  6. iOS开发学习--纯代码 UIScrollView 无限循环的实现——代码类封装

    一个简单的利用UIScrollView 实现的无线滚动banner,下面的代码实现,因为封装问题,对两个及一下的view 支持出了一点问题(view是传参进来的,不可以生成两份),但是原理是正确的,智 ...

  7. Visual Studio快速封装字段方法

    在面向对象的编程中我们常常要将各个字段封装为属性,但是当字段多的时候往往这个重复的操作会大大降低我们的开发效率,那么如何才能快速的封装字段呢?下面就给大家2个解决方法: 1.使用封装字段方法: 选中字 ...

  8. C# 加密总结 一些常见的加密方法

    C# 加密总结 一些常见的加密方法 一 散列数据 代码如下: ? private static string CalculateSHA512Hash(string input)         {   ...

  9. JavaScript基础:BOM的常见内置方法和内置对象

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. BOM的介绍 JavaScript的组成 JavaScript基础分为三 ...

随机推荐

  1. C# 委托,事件, 异步

    委托 ​ 委托可以说是方法的集合,里面放着方法列表,当调用的时候就会调用全部的方法列表 ​ 个人理解 : 当声明和创建时委托时, 它是个 对象 当使用委托,调用方法时,它是个 方法 声明委托类型 de ...

  2. Chetsheet: 2017 01.01 ~ 01.31

    Web TypeScript: the missing introduction Async HTTP API and service bus Optimizing the Performance o ...

  3. 流畅的python和cookbook学习笔记(四)

    1.数字的四舍五入 对于简单的舍入运算,使用内置的 round(value, ndigits) 函数即可. round 函数返回离它最近的偶数.也就是说,对 1.5 或者 2.5 的舍入运算都会得到 ...

  4. CommandLineRunner预加载数据

    在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–CommandLineRunner. CommandLineRunner是一个接 ...

  5. spring boot 定时任务

    定时任务实现方式 三种: 1) Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务. 最早的时候就是这样写定时任务的. 2) 开源的第三方框 ...

  6. JBPM学习第4篇:10分钟熟悉Eclipse

    http://download.jboss.org/jbpm/videos/7.0.0.Final_eclipse_getting_started.swf 打开视频练习 1.导入 evaluation ...

  7. Algorithm——最长公共前缀

    一.问题 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

  8. 11.6NOIP模拟赛解题报告

    心路历程 预计得分:\(100 + 100 + 100 = 300\) 实际得分:\(100 +100 +100 = 300\) 学OI两年终于AK了一次qwq(虽然题目炒鸡水..) 纪念一下这令人激 ...

  9. 日常捕获的野生知识 - javascript 与 类

    javascript 并不提供类这样方便实用的面向对象类型,今天学习到了,直接上代码: <!DOCTYPE html> <html lang="en"> & ...

  10. mianxiangduixiang

    package com.hanqi; public class mianxiang { public static void main(String[]args) { Cat c1 =new Cat( ...