文档章节

Java Selenium封装--RemoteWebDriver

xxjbs001
 xxjbs001
发布于 2015/04/06 18:41
字数 659
阅读 59
收藏 0
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;
	}
}

本文转载自:http://blog.csdn.net/five3/article/details/13004133

共有 人打赏支持
xxjbs001
粉丝 55
博文 749
码字总数 215621
作品 0
浦东
QA/测试工程师
Selenium 1.0 vs Selenium 2.0 (Selenium Web-driver)

Selenium is browser automation tool, for more information select thislink. We already have Selenium1.0, why Selenium2.0? Selenium1.0 can't tackle following items. 1. Native keyb......

李长春
2011/07/12
0
0
selenium webdriver 环境搭建–java

selenium java环境的安装可以分为三个部分:jdk、eclipse和selenium. Jdk jdk(java development kit)是sun公司针对java开发人员的产品,是整个java的核心,包含了java的运行环境、工具和基础...

测试-雨
2015/01/05
0
0
15 Web 自动化测试 -- Selenium Grid

Selenium Grid 我其实写过多篇文章,大家可以参考可能内容会很大部分是重复的: https://www.jianshu.com/p/017adc59ca1b https://www.jianshu.com/p/29c7240e9f48 Selenium Grid 是什么? ...

米阳MeYoung
07/03
0
0
selenium + java + testNG 自动化环境搭建

kSelenium终极自动化测试环境搭建(一)Selenium+Eclipse+Junit+TestNG 第一步 安装JDK JDk1.7. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-188026......

UFO猎手
2015/05/28
0
0
测试框架Automation Center(java、QTP、selenium-java 版、...

支持selenium java语言的一个框架,留以后学习~~ 相关链接:http://www.cesoo.info/acdocs/zh_cn/index.html#Menu=ChildMenu1 框架介绍 AC是一个集成各种测试工具的java自动化测试管理与运行平...

Rebecca.Hu
2012/02/17
0
4

没有更多内容

加载失败,请刷新页面

加载更多

下一页

qduoj~前端~二次开发~打包docker镜像并上传到阿里云容器镜像仓库

上一篇文章https://my.oschina.net/finchxu/blog/1930017记录了怎么在本地修改前端,现在我要把我的修改添加到部署到本地的前端的docker容器中,然后打包这个容器成为一个本地镜像,然后把这...

虚拟世界的懒猫
今天
1
0
UML中 的各种符号含义

Class Notation A class notation consists of three parts: Class Name The name of the class appears in the first partition. Class Attributes Attributes are shown in the second par......

hutaishi
今天
1
0
20180818 上课截图

小丑鱼00
今天
1
0
Springsecurity之SecurityContextHolderStrategy

注:下面分析的版本是spring-security-4.2.x,源码的github地址是: https://github.com/spring-projects/spring-security/tree/4.2.x 先上一张图: 图1 SecurityContextHolderStrategy的三个......

汉斯-冯-拉特
今天
1
0
LNMP架构(Nginx负载均衡、ssl原理、生成ssl密钥对、Nginx配置ssl)

Nginx负载均衡 网站的访问量越来越大,服务器的服务模式也得进行相应的升级,比如分离出数据库服务器、分离出图片作为单独服务,这些是简单的数据的负载均衡,将压力分散到不同的机器上。有时...

蛋黄_Yolks
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部