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的更多相关文章

  1. Java Selenium封装--RemoteWebElement

    package com.liuke.selenium.driver; import java.sql.SQLException; import java.util.List; import org.j ...

  2. Java&Selenium截图方法封装

    Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...

  3. Java&Selenium智能等待方法封装

    Java&Selenium智能等待方法封装 ExpectedConditions方法还有很多,自然也可以继续扩展很多 package util; import org.openqa.selen ...

  4. Java&Selenium 模拟键盘方法封装

    Java&Selenium 模拟键盘方法封装 package util; import java.awt.AWTException; import java.awt.Robot; import ...

  5. Java&Selenium控制滚动条方法封装

    Java&Selenium控制滚动条方法封装 package util; import org.openqa.selenium.JavascriptExecutor; import org.o ...

  6. Java&Selenium 模拟鼠标方法封装

    Java&Selenium 模拟鼠标方法封装 package util; import org.openqa.selenium.By; import org.openqa.selenium.W ...

  7. 《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)

    1.简介 上一篇宏哥介绍了如何设计支持不同浏览器测试,宏哥的方法就是通过来切换配置文件设置的浏览器名称的值,来确定启动什么浏览器进行脚本测试.宏哥将这个叫做浏览器引擎类.这个类负责获取浏览器类型和启动 ...

  8. Electorn(桌面应用)自动化测试之Java+selenium实战例子

    基于electorn的桌面应用,网上相关资料较少.所有记录一下.使用java+selenium+testng对该类型应用的自动化测试方法. 代码样例 package com.contract.web. ...

  9. IntelliJ IDEA java selenium

    // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...

随机推荐

  1. struts2 OGNL表达式

    一.OGNL OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调用对 ...

  2. poj1273Drainage Ditches

    #include<iostream> /* 题意:就是寻找从源点到汇点的最大流! 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来 就是这两个点之间的 ...

  3. java中异常注意的细节2

    class A extends Exception{ A(){ super(); } A(String msg){ super(msg); } } class B extends A{ B(){ su ...

  4. 邻接矩阵无向图(二)之 C++详解

    本章是通过C++实现邻接矩阵无向图. 目录 1. 邻接矩阵无向图的介绍 2. 邻接矩阵无向图的代码说明 3. 邻接矩阵无向图的完整源码 转载请注明出处:http://www.cnblogs.com/s ...

  5. Arctext.js - 基于 CSS3 & jQuery 的文本弯曲效果

    Arctext.js 是基于 Lettering.js 的文本旋转插件,根据设置的旋转半径准确计算每个字母的旋转弧度并均匀分布.虽然 CSS3 也能够实现字符旋转效果,但是要让安排每个字母都沿着弯曲路 ...

  6. Dagger2 生成代码学习

    接上一篇文章介绍了Dagger2的初步使用,相信刚接触的人会觉得很奇怪,怎么会有很多自己没有定义的代码出现,为什么Component的创建方式是那样的.为了搞清楚这些东西,我们需要查看一下Dagger ...

  7. 索引深入浅出(4/10):非聚集索引的B树结构在聚集表

    一个表只能有一个聚集索引,数据行以此聚集索引的顺序进行存储,一个表却能有多个非聚集索引.我们已经讨论了聚集索引的结构,这篇我们会看下非聚集索引结构. 非聚集索引的逻辑呈现 简单来说,非聚集索引是表的子 ...

  8. SQL Server安全(9/11):透明数据加密(Transparent Data Encryption)

    在保密你的服务器和数据,防备当前复杂的攻击,SQL Server有你需要的一切.但在你能有效使用这些安全功能前,你需要理解你面对的威胁和一些基本的安全概念.这篇文章提供了基础,因此你可以对SQL Se ...

  9. Java魔法堂:类加载器入了个门

    一.前言 <Java魔法堂:类加载机制入了个门>中提及整个类加载流程中只有加载阶段作为码农的我们可以入手干预,其余均由JVM处理.本文将记录加载阶段的核心组件——类加载器的相关信息,以便日 ...

  10. 我的vim配置文件

    强烈拥护开源精神,高举开源大旗,今天我就分享下我自己结合网上还有自己实际使用配的vimrc,可以给各位参考下,不要见笑,具体说明我在rc里写的也很详细,可以具体看下,也希望可以借这个机会能多认识认识几 ...