软件测试神一样的超级好用的selenium 原创java util
来源:互联网 发布:纯第三方数据一元 编辑:程序博客网 时间:2024/05/14 05:53
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SeleniumUtil {
private static WebDriver webDriver;
private static WebElement webElement;
private static Robot robot;
private static By byelement;
//javascript
public void Script ( String script , WebElement element ) {
JavascriptExecutor js=(JavascriptExecutor)webDriver;
if ( element==null ) {
js.executeScript(script);
} else {
js.executeScript(script, element);
}
}
//javascript 返回数据
public String ScriptString ( String script ) {
JavascriptExecutor js=(JavascriptExecutor)webDriver;
String s=(String) js.executeScript(script);
return s;
}
/**
* 初始化
* @param select
* 1:Firefox
* 2:IE
* 3:Chrome
* 4:后台执行
* @param browserpath
* 选择本地浏览路径
*/
public void Init ( int switchbrowser , String browserpath ) {
switch ( switchbrowser ) {
case 1:
System.setProperty ( "webdriver.firefox.bin" , browserpath );
webDriver = new FirefoxDriver();
webDriver.manage().window().maximize();
break;
case 2:
System.setProperty("webdriver.ie.driver", browserpath);
webDriver = new InternetExplorerDriver();
webDriver.manage().window().maximize();
break;
case 3:
System.setProperty("webdriver.chrome.driver", browserpath);
webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
break;
case 4:
webDriver = new HtmlUnitDriver();
break;
}
}
//复制
protected void CopyKeys() {
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_C);
} catch (AWTException e) {
e.printStackTrace();
}
}
//粘贴
protected void PasteKeys() {
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
} catch (AWTException e) {
e.printStackTrace();
}
}
/**
* 拖拽
* @param by 要多拽的元素
* @param target 目的地
*/
protected void DragAndDrop ( By by , By target ) {
(new Actions(webDriver)).dragAndDrop(webDriver.findElement(by), webDriver.findElement(target)).perform();
}
//传入值
protected void SendKeys(By by, String value){
webDriver.findElement(by).sendKeys(value);
}
/**
* 截图
* @param outpath 导出截图后的路径 文件名是以当前时间
*/
public void GetScreen ( String outpath ) {
File screenShotFile = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShotFile, new File ( outpath + "/" + getSysTime() + ".jpg" ) );
} catch (IOException e) {
e.printStackTrace();
}
}
/**获取元素
* @param by
* @param second 查找需要时间
*/
public void Wait( final By by , int second ) {
WebDriverWait wait = new WebDriverWait(webDriver,second);
wait.until(new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver d) {
return d.findElement(by);
}
});
}
//打开浏览器
public void Start ( String url ) {
webDriver.get( url );
}
//结束
public void Stop () {
webDriver.quit();
}
//获取当前时间
public static String getSysTime(){
SimpleDateFormat filename = new SimpleDateFormat("yyyyMMdd_HHmmss");
return filename.format(new Date());
}
// 睡眠
public void Sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static WebDriver getWebDriver() {
return webDriver;
}
public static void setWebDriver(WebDriver webDriver) {
SeleniumUtil.webDriver = webDriver;
}
public static WebElement getWebElement() {
return webElement;
}
public static void setWebElement(WebElement webElement) {
SeleniumUtil.webElement = webElement;
}
public static By getByelement() {
return byelement;
}
public static void setByelement(By byelement) {
SeleniumUtil.byelement = byelement;
}
}
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SeleniumUtil {
private static WebDriver webDriver;
private static WebElement webElement;
private static Robot robot;
private static By byelement;
//javascript
public void Script ( String script , WebElement element ) {
JavascriptExecutor js=(JavascriptExecutor)webDriver;
if ( element==null ) {
js.executeScript(script);
} else {
js.executeScript(script, element);
}
}
//javascript 返回数据
public String ScriptString ( String script ) {
JavascriptExecutor js=(JavascriptExecutor)webDriver;
String s=(String) js.executeScript(script);
return s;
}
/**
* 初始化
* @param select
* 1:Firefox
* 2:IE
* 3:Chrome
* 4:后台执行
* @param browserpath
* 选择本地浏览路径
*/
public void Init ( int switchbrowser , String browserpath ) {
switch ( switchbrowser ) {
case 1:
System.setProperty ( "webdriver.firefox.bin" , browserpath );
webDriver = new FirefoxDriver();
webDriver.manage().window().maximize();
break;
case 2:
System.setProperty("webdriver.ie.driver", browserpath);
webDriver = new InternetExplorerDriver();
webDriver.manage().window().maximize();
break;
case 3:
System.setProperty("webdriver.chrome.driver", browserpath);
webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
break;
case 4:
webDriver = new HtmlUnitDriver();
break;
}
}
//复制
protected void CopyKeys() {
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_C);
} catch (AWTException e) {
e.printStackTrace();
}
}
//粘贴
protected void PasteKeys() {
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
} catch (AWTException e) {
e.printStackTrace();
}
}
/**
* 拖拽
* @param by 要多拽的元素
* @param target 目的地
*/
protected void DragAndDrop ( By by , By target ) {
(new Actions(webDriver)).dragAndDrop(webDriver.findElement(by), webDriver.findElement(target)).perform();
}
//传入值
protected void SendKeys(By by, String value){
webDriver.findElement(by).sendKeys(value);
}
/**
* 截图
* @param outpath 导出截图后的路径 文件名是以当前时间
*/
public void GetScreen ( String outpath ) {
File screenShotFile = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShotFile, new File ( outpath + "/" + getSysTime() + ".jpg" ) );
} catch (IOException e) {
e.printStackTrace();
}
}
/**获取元素
* @param by
* @param second 查找需要时间
*/
public void Wait( final By by , int second ) {
WebDriverWait wait = new WebDriverWait(webDriver,second);
wait.until(new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver d) {
return d.findElement(by);
}
});
}
//打开浏览器
public void Start ( String url ) {
webDriver.get( url );
}
//结束
public void Stop () {
webDriver.quit();
}
//获取当前时间
public static String getSysTime(){
SimpleDateFormat filename = new SimpleDateFormat("yyyyMMdd_HHmmss");
return filename.format(new Date());
}
// 睡眠
public void Sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static WebDriver getWebDriver() {
return webDriver;
}
public static void setWebDriver(WebDriver webDriver) {
SeleniumUtil.webDriver = webDriver;
}
public static WebElement getWebElement() {
return webElement;
}
public static void setWebElement(WebElement webElement) {
SeleniumUtil.webElement = webElement;
}
public static By getByelement() {
return byelement;
}
public static void setByelement(By byelement) {
SeleniumUtil.byelement = byelement;
}
}
0 5
- 软件测试神一样的超级好用的selenium 原创java util
- 【软件测试】Java中Selenium Webdriver的使用
- selenium+java 的Web测试
- java.util.Timer类可以实现多线程一样的功能
- [原创]一个关于软件开发的超级简单的但超级COOL的想法
- 自定义Toast,跟系统的一样好用,一样使用!!!
- 软件测试if else路径开源代码java中java.util.Date里面的pase方法
- 介绍一个非常好用的测试工具 跟Cppunit 一样好用(强烈推荐)
- 偶然发现一个web自动化测试的好东东---selenium
- 中兴新支点操作系统上超级好用的软件中心
- 超级好用的英语四字短语
- 超级好用的PHP分页类
- 超级好用的编辑器UltraEdit
- 超级好用的url 汉字编码函数
- PhotoShop超级好用的插件DevRocket
- 超级好用的vim配置记录
- 超级好用的正则表达式
- 超级好用的流程图js框架
- visual stodio 编译前后动作定制总结
- hibernate.properites文件,各数据库连接字符串
- CakePHP based UI 看起来很不错
- 今天的花开终有时的台词
- html 只能输入数字
- 软件测试神一样的超级好用的selenium 原创java util
- wince6 bsp platform名称修改
- extensions.conf的常用参数配置
- POJ 3414 Pots(容量BFS)
- aspxgridview 添加列
- [2013年]本人自己亲自使用800左右的性价好的平板供大家参考
- android 滑动冲突 ScrollView Viewpager listview 等
- Geohot:iOS 7越狱工具不含中国公司后门程序
- 可是小朋友不顾老奶奶说的台词话