Java Selenium封装--RemoteWebElement
package com.liuke.selenium.driver; import java.sql.SQLException;
import java.util.List;
import org.json.JSONException;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.ui.Select; public class JSWebElement {
private RemoteWebElement we = null;
private JavascriptExecutor jse = null; public JSWebElement(){} public JSWebElement(RemoteWebElement we){
this.we = we;
} ///
///通过元素ID定位元素
///
public JSWebElement findElementById(String using) {
try {
return new JSWebElement((RemoteWebElement)we.findElementById(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
} ///
///通过元素CSS表达式定位元素
///
public JSWebElement findElementByCssSelector(String using) {
try {
return new JSWebElement((RemoteWebElement)we.findElementByCssSelector(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
} ///
///通过元素Xpath表达式定位元素
///
public JSWebElement findElementByXPath(String using) {
try {
return new JSWebElement((RemoteWebElement)we.findElementByXPath(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
} ///
///通过链接的文字定位元素
///
public JSWebElement findElementByLinkText(String using) {
try {
return new JSWebElement((RemoteWebElement)we.findElementByLinkText(using));
}catch (NoSuchElementException e){
return new JSWebElement();
}
} ///
///通过元素DOM表达式定位元素
///
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();
}
} ///
///判断元素是否存在
///
public Boolean isExist(){
if (we != null){
return true;
}else{
return false;
}
} ///
///获取元素的HTML内容
///
public String getHtml(){
return we.getAttribute("outerHTML");
} ///
///获取元素的文本内容
///
public String getText(){
return we.getText();
} ///
///获取元素的value值
///
public String getValue(){
return this.getAttribute("value");
} ///
///获取元素的特定属性值
///
public String getAttribute(String name){
return we.getAttribute(name);
} ///
///向可输入元素发送内容,如:text、textarea、filefield等
///
public void sendKeys(String string){
String old_bg = this.setBackground("yellow");
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
we.sendKeys(string);
this.setBackground(old_bg);
} ///
///判断元素是否可用
///
public boolean isEnabled(){
return we.isEnabled();
} ///
///判断元素是否可见
///
public boolean isVisible(){
return we.isDisplayed();
} ///
///清空可编辑元素的内容。不可编辑元素次操作会抛异常
///
public void clear(){
we.clear();
} ///
///对元素进行点击操作
///
public void click(){
we.click();
} ///
///检查元素的特定属性值
///
public void checkAttr(String attribute, JSWebUtils utils) throws SQLException, JSONException
{
String [] attributes=attribute.split("=", 2);
String actual = this.we.getAttribute(attributes[0]);
if (actual == null){ actual = "null"; }
utils.checkPointBase(actual,attributes[1]);
} ///
///获取元素的CSS值
///
public String getCssValue(String name)
{
return we.getCssValue(name);
} ///
///判断元素是否被选中
///
public boolean isSelected()
{
return we.isSelected();
} ///
///可选元素进行选中操作;如:select
///
public void select(String by, String value) throws Exception
{
if (we.getTagName().equals("select")){
Select select = new Select(we);
if (by.equals("index")){
select.selectByIndex(Integer.parseInt(value));
}else if (by.equals("value")){
select.selectByValue(value);
}else if (by.equals("text")){
select.selectByVisibleText(value);
}
}else{
Exception e = new Exception("The element is not SELECT Object");
throw e;
}
} ///
///对可选中元素进行取消选择操作;如:SELECT in multiple type
///
public void deSelect(String by, String...value) throws Exception
{
if (we.getTagName().equals("select")){
Select select = new Select(we);
if (by.equals("index")){
select.deselectByIndex(Integer.parseInt(value[0]));
}else if (by.equals("value")){
select.deselectByValue(value[0]);
}else if (by.equals("text")){
select.deselectByVisibleText(value[0]);
}else if (by.equals("*")){
select.deselectAll();
}
}else{
Exception e = new Exception("The element is not SELECT Object");
throw e;
}
} ///
///判断下拉框是否为多选
///
public boolean isMultiple() throws Exception
{
if (we.getTagName().equals("select")){
Select select = new Select(we);
if (select.isMultiple()){
return true;
}else{
return false;
}
}else{
Exception e = new Exception("The element is not SELECT Object");
throw e;
}
} ///
///获取select的当前选中值
///
public String getSelectedText() throws Exception
{
if (we.getTagName().equals("select")){
String text = "";
Select select = new Select(we);
List<WebElement> options = select.getAllSelectedOptions();
for (WebElement w : options){
text += w.getText() + "\r\n";
}
return text;
}else{
Exception e = new Exception("The element is not SELECT Object");
throw e;
}
} ///
///判断指定项是否存在
///
public boolean isInclude(String name) throws Exception
{
if (we.getTagName().equals("select")){
Select select = new Select(we);
List<WebElement> options = select.getOptions();
for (WebElement w : options){
if (w.getText().equals(name)){
return true;
}
}
return false;
}else{
Exception e = new Exception("The element is not SELECT Object");
throw e;
}
} ///
///获取元素的tagname
///
public String getTagName(){
return we.getTagName();
} ///
///获取元素的id
///
public String getId(){
return we.getId();
} ///
///获取元素的绝对位置
///
public Point getLocation(){
return we.getLocation();
} ///
///获取元素的出现在屏幕可见区时的位置
///
public Point getLocationOnScreenOnceScrolledIntoView(){
return we.getLocationOnScreenOnceScrolledIntoView();
} ///
///获取元素的坐标
///
public Coordinates getCoordinates(){
return we.getCoordinates();
} ///
///获取元素的大小
///
public Dimension getSize(){
return we.getSize();
} ///
///提交元素所在form的内容
///
public void submit()
{
we.submit();
} ///
///勾选radio、checkbox
///
public void check(String...values) throws Exception
{
if (we.getTagName().equals("input")){
if (we.getAttribute("type").equals("radio")){
WebDriver wd = we.getWrappedDriver();
List<WebElement> wl = wd.findElements(By.name(we.getAttribute("name")));
if (values[0].equals("index")){
wl.get(Integer.parseInt(values[1])).click();
}else if (values[0].equals("value")){
for (WebElement w : wl){
if (w.getAttribute("value").equals(values[1])){
w.click();
break;
}
}
}
}else if (we.getAttribute("type").equals("checkbox")){
if (!we.isSelected()){
we.click();
}
}else{
Exception e = new Exception("The element is not Radio or CheckBox Object");
throw e;
}
}else{
Exception e = new Exception("The element is not INPUT Object");
throw e;
}
} ///
///取消勾选checkbox
///
public void unCheck() throws Exception
{
if (we.getTagName().equals("input") && we.getAttribute("type").equals("checkbox")){
if (we.isSelected()){
we.click();
}
}else{
Exception e = new Exception("The element is not CheckBox Object");
throw e;
}
} ///
///checkbox、radio是否勾选
///
public boolean isChecked(String...values) throws Exception
{
if (we.getTagName().equals("input")){
if (we.getAttribute("type").equals("radio")){
WebDriver wd = we.getWrappedDriver();
List<WebElement> wl = wd.findElements(By.name(we.getAttribute("name")));
if (values[0].equals("index")){
return wl.get(Integer.parseInt(values[1])).isSelected();
}else if (values[0].equals("value")){
for (WebElement w : wl){
if (w.getAttribute("value").equals(values[1])){
return w.isSelected();
}
}
}
return false;
}else if (we.getAttribute("type").equals("checkbox")){
return we.isSelected();
}else{
Exception e = new Exception("The element is not Radio or CheckBox Object");
throw e;
}
}else{
Exception e = new Exception("The element is not INPUT Object");
throw e;
}
} ///
///把元素滚动到可视区
///
public void scroll()
{
this.focus();
} ///
///高亮元素
///
public void highLight() throws InterruptedException
{
this.focus();
JavascriptExecutor js = getJSE();
String old_style = we.getAttribute("style");
for (int i = 0; i < 3; i++) {
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", this.we, "background-color: red; border: 2px solid red;" + old_style);
Thread.sleep(500);
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", this.we, old_style);
Thread.sleep(500);
}
} ///
///触发元素的特定事件
///
public void fireEvent(String event){
JavascriptExecutor js = getJSE();
js.executeScript(String.format("arguments[0].%s()", event), this.we);
} ///
///使元素获取焦点
///
public void focus(){
// this.we.sendKeys("");
JavascriptExecutor js = getJSE();
js.executeScript("arguments[0].focus();", this.we);
} ///
///对元素执行JavaScript操作;即执行元素的dom操作
///
public void executeJS(String commands){
JavascriptExecutor js = getJSE();
String[] comandArr = commands.split(";");
commands = "";
for (String comand : comandArr){
if (!comand.trim().equals("")){
commands += String.format("arguments[0].%s;", comand);
}
}
if (!commands.equals("")){
js.executeScript(commands, this.we);
}
} ///
///获取原始的RemoteWebElement对象
///
public RemoteWebElement getNativeWebElement(){
return this.we;
} private JavascriptExecutor getJSE(){
if (this.isExist()){
if (this.jse == null){
WebDriver wd = we.getWrappedDriver();
this.jse = (JavascriptExecutor) wd;
}
}
return jse;
} private String setBackground(String color){
JavascriptExecutor js = getJSE();
String old_bg = we.getCssValue("background-color");
js.executeScript("arguments[0].style.background = arguments[1];", this.we, color);
return old_bg;
} }
Java Selenium封装--RemoteWebElement的更多相关文章
- Java Selenium封装--RemoteWebDriver
package com.selenium.driver; import java.io.File; import java.io.IOException; import java.net.URL; i ...
- 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. ...
- Java+Selenium+Testng自动化测试学习(二)
Java+Selenium+TestNG自动化测试框架整合 1.简化代码 封装一个定位元素的类,类型为ElementLocation package com.test; import org.open ...
随机推荐
- salesforce 零基础开发入门学习(一)Salesforce功能介绍,IDE配置以及资源下载
目前国内已经有很多公司做salesforce,但是国内相关的资料确是少之又少.上个月末跳槽去了新公司,主要做的就是salesforce,不过当时想要看一些相关资料确实比较难.为了避免想要零基础学习的人 ...
- iOS-ARC
1. 本文的主要内容: ARC的本质 ARC的开启与关闭 ARC的修饰符 ARC与Block ARC与Toll-Free Bridging ARC的本质 ARC是编译器(时)特性,而不是运行时特性,更 ...
- Android 实现应用升级方案(暨第三方自动升级服务无法使用后的解决方案)
第三方推送升级服务不再靠谱: 以前在做Android开发的时候,在应用升级方面都是使用的第三方推送升级服务,但是目前因为一些非技术性的问题,一些第三方厂商不再提供自动升级服务,比如友盟,那么当第三方推 ...
- javascript_basic_01之概述
1.javascript组成: ①核心ECMAScript:②文档对象模型DOM(Document Object Model):③浏览器对象模型BOM(Browser Object Model): 2 ...
- 快速了解SPA单页面应用
简要 SPA单页网页应用程序这个概念并不算新,早在2003年就已经有在讨论这个概念了,不过,单页应用这个词是到了2005年才有人提出使用,SPA的概念就和它的名字一样显而易懂,就是整个网站不再像传统的 ...
- 本地MVC项目发布到IIS服务器
0瞎扯 朋友们有时候我们写个一个web程序只能使用卡西尼服务器调试,下面我教大家发布到IIS服务器上(包括本地ISS7.5和远程服务器 IIS) 1.VS发布 a.点击web项目->发布
- CSS文本方向
前面的话 一般地,正常网页文本方向都是从上到下,从左到右.实际上,有多种设置文本方向的属性,前面已经详细介绍过text-align,HTML全局属性中有一个"dir"属性就是专门用 ...
- Android线程机制——AsyncTask
对于Android为什么要使用多线程,因为从Android4.0之后,谷歌规定了网络操作不允许放在主线程中执行,由此就有了多线程的机制,有个JAVA学习经验的朋友一定知道多线程指的是什么,简单来讲就是 ...
- Oracle工具之DBNEWID
DBNEWID是Oracle提供的一个用于修改数据库DBID和DBNAME的工具. 在引进该工具之前,如果我们想修改数据库的数据库名,必须重建控制文件.但即便如此,也无法修改该数据库的DBID.众所周 ...
- 哈夫曼树(二)之 C++详解
上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...