Java Selenium封装--RemoteWebDriver
package com.selenium.driver;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
public class JSWebDriver{
private RemoteWebDriver wd = null;
private JavascriptExecutor jse = null; public JSWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
wd = new RemoteWebDriver(remoteAddress, desiredCapabilities);
} ///
///浏览器url导航
///
public void goTo(String url){
wd.get(url);
} ///
///浏览器退出
///
public void quit(){
wd.quit();
} ///
///浏览器后退
///
public void back(){
wd.navigate().back();
} ///
///浏览器前进
///
public void forward(){
wd.navigate().forward();
} ///
///浏览器刷新
///
public void refresh(){
wd.navigate().refresh();
} ///
///切换到新浏览器窗口;按照title、url、index;支持正则匹配
///
public void switchToWindow(String by, String value, String...match) throws Exception{
String currenthandle = wd.getWindowHandle();
Set<String> handles = wd.getWindowHandles();
int currentIndex = -1;
String searchString = "";
for(String handle : handles){
currentIndex += 1;
if(handle.equals(currenthandle)){
continue;
}else{
wd.switchTo().window(handle);
if (match.length == 1 && match[0].equals("regex")){
if (by.equals("title")){
searchString = wd.getTitle();
}else if (by.equals("url")){
searchString = wd.getCurrentUrl();
}
Pattern pattern = Pattern.compile(value);
Matcher matcher = pattern.matcher(searchString);
if(matcher.find()){
return;
}
}else{
if (by.equals("title")){
searchString = wd.getTitle();
}else if (by.equals("url")){
searchString = wd.getCurrentUrl();
}else if (by.equals("index")){
searchString = Integer.toString(currentIndex);
}
if(searchString.equals(value)){
return;
}
}
}
}
Exception e = new Exception("Swtich Window Failed, Please Make Sure The Locator Was Right.");
throw e;
} ///
///JS弹框确认
///
public void clickAlertSure(){
Alert alert = wd.switchTo().alert();
alert.accept();
} ///
///JS弹框取消
///
public void clickAlertDismiss()
{
Alert alert = wd.switchTo().alert();
alert.dismiss();
} ///
///设置prompt弹框内容
///
public void setPromptMessage(String parameter){
Alert alert = wd.switchTo().alert();
alert.sendKeys(parameter);
} ///
///获取JS弹框内容
///
public String getPromptMessage(){
Alert alert = wd.switchTo().alert();
return alert.getText();
} ///
///切换到Frame窗口;先定位到iframe元素
///
public void switchToFrame(JSWebElement jselement){
wd.switchTo().frame(jselement.getNativeWebElement());
} ///
///执行JS脚本
///
public void executeScript(String parameter){
JavascriptExecutor js = getJSE();
js.executeScript(parameter);
} ///
///获取指定cookie
///
public String getCookie(String name){
Cookie cookie=wd.manage().getCookieNamed(name);
if (cookie == null){ return "null"; }
return cookie.getValue();
} ///
///获取所有cookie
///
public Map<String, String> getCookies(){
Map<String, String> newCookies = new HashMap<String, String>();
Set<Cookie> cookies= wd.manage().getCookies();
for (Cookie cookie : cookies){
newCookies.put(cookie.getName(), cookie.getValue());
}
return newCookies;
} ///
///截取屏幕
///
public void getScreen(String filepath){
WebDriver augmentedDriver = new Augmenter().augment(this.wd);
TakesScreenshot ts = (TakesScreenshot) augmentedDriver;
File screenShotFile = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile (screenShotFile, new File(filepath));
}catch (IOException e){
e.printStackTrace();
}
} ///
///获取title
///
public String getTitle(){
return wd.getTitle();
} ///
///获取url
///
public String getUrl(){
return wd.getCurrentUrl();
} ///
///获取HTML源码
///
public String getSource(){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return wd.getPageSource();
} ///
///滚动页面到指定位置
///
public void scroll(String x, String y){
if (x.equals("left")){
x = "0";
}else if (x.equals("right")){
x = "document.body.scrollWidth";
}else if (x.equals("middle")){
x = "document.body.scrollWidth/2";
}
if (y.equals("top")){
y = "0";
}else if (y.equals("buttom")){
y = "document.body.scrollHeight";
}else if (y.equals("middle")){
y = "document.body.scrollHeight/2";
}
this.executeScript(String.format("scroll(%s,%s);", x, y));
} ///
///最大化浏览器
///
public void maximize(){
wd.manage().window().maximize();
} public JSWebElement findElementById(String using) {
try {
return new JSWebElement((RemoteWebElement)wd.findElementById(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
} public JSWebElement findElementByCssSelector(String using) {
try {
return new JSWebElement((RemoteWebElement)wd.findElementByCssSelector(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
} public JSWebElement findElementByXPath(String using) {
try {
return new JSWebElement((RemoteWebElement)wd.findElementByXPath(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
} public JSWebElement findElementByLinkText(String using) {
try {
return new JSWebElement((RemoteWebElement)wd.findElementByLinkText(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
} public JSWebElement findElementByDom(String using) {
try {
JavascriptExecutor js = this.getJSE();
WebElement we = (WebElement)js.executeScript(String.format("return %s", using));
return new JSWebElement((RemoteWebElement)we);
}catch (NoSuchElementException e){
return new JSWebElement();
}
} ///
///获取原生的RemoteWebdriver对象
///
public RemoteWebDriver getNativeWebDriver(){
return this.wd;
} private JavascriptExecutor getJSE(){
if (this.jse == null){
this.jse = (JavascriptExecutor) this.wd;
}
return jse;
}
}
Java Selenium封装--RemoteWebDriver的更多相关文章
- Java Selenium封装--RemoteWebElement
package com.liuke.selenium.driver; import java.sql.SQLException; import java.util.List; import org.j ...
- Java&Selenium截图方法封装
Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...
- Java&Selenium智能等待方法封装
Java&Selenium智能等待方法封装 ExpectedConditions方法还有很多,自然也可以继续扩展很多 package util; import org.openqa.selen ...
- Java&Selenium 模拟键盘方法封装
Java&Selenium 模拟键盘方法封装 package util; import java.awt.AWTException; import java.awt.Robot; import ...
- Java&Selenium控制滚动条方法封装
Java&Selenium控制滚动条方法封装 package util; import org.openqa.selenium.JavascriptExecutor; import org.o ...
- Java&Selenium 模拟鼠标方法封装
Java&Selenium 模拟鼠标方法封装 package util; import org.openqa.selenium.By; import org.openqa.selenium.W ...
- 《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)
1.简介 上一篇宏哥介绍了如何设计支持不同浏览器测试,宏哥的方法就是通过来切换配置文件设置的浏览器名称的值,来确定启动什么浏览器进行脚本测试.宏哥将这个叫做浏览器引擎类.这个类负责获取浏览器类型和启动 ...
- Electorn(桌面应用)自动化测试之Java+selenium实战例子
基于electorn的桌面应用,网上相关资料较少.所有记录一下.使用java+selenium+testng对该类型应用的自动化测试方法. 代码样例 package com.contract.web. ...
- IntelliJ IDEA java selenium
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...
随机推荐
- How Google TestsSoftware - The Life of a SET
SETs are Software Engineersin Test. They are software engineers who happen to write testing function ...
- Model-View-Controller(MVC) is an architectural pattern that frequently used in web applications. Which of the following statement(s) is(are) correct?
Model-View-Controller(MVC) is an architectural pattern that frequently used in web applications. Whi ...
- java基础,继承类题目:编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E
21.编写一个Java应用程序,该程序包括3个类:Monkey类.People类和主类 E.要求: (1) Monkey类中有个构造方法:Monkey (String s),并且有个public vo ...
- 关于sass的介绍和基本语法
引入 什么是sass?sass是css预处理器. 那预处理器又是什么?css本身不是一种编程语言,而预处理器是用一种专门的编程语言,进行网页样式设计,然后再编译成正常的CSS文件. 如今主流的预处理器 ...
- C#委托(delegate)
C#中委托(delegate)是一种安全地封装方法的类型,委托是面向对象的.类型安全的. 使用委托的步骤: 1.声明委托 public delegate void DelegateHandler(st ...
- (转)清理AIX的/var文件系统大小
在ROOTVG空间有限,且/var文件系统的空间已经足够大的情况下,/var文件系统空间快满了,这种情况怎么处理?IBM给了一个很好的处理流程: 使用find 命令检查在/var目录中所有大于1MB的 ...
- 如何在windows 10 x64安装佳能 CP900 驱动
佳能太无耻,为了销售CP910 , CP900 到win8 ,win8.1,win 10 都没有驱动程序,网上找的方法都不行,IT民工自有办法: 1.按住shift键,重启电脑 restart 2.在 ...
- 【模式匹配】Aho-Corasick自动机
1. 多模匹配 AC自动机(Aho-Corasick Automaton)是多模匹配算法的一种.所谓多模匹配,是指在字符串匹配中,模式串有多个.前面所介绍的KMP.BM为单模匹配,即模式串只有一个.假 ...
- Asp.net 加密解密类
namespace Wedn.Net { /// <summary> /// EncryptHelper 来′自? wedn.net /// </summary> public ...
- IOS高级编程之三:IOS 多线程编程
多线程的概念在各个操作系统上都会接触到,windows.Linux.mac os等等这些常用的操作系统,都支持多线程的概念. 当然ios中也不例外,但是线程的运行节点可能是我们平常不太注意的. 例如: ...