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 ...
随机推荐
- PHP_01之PHP概述、数据、语法
1.定义:PHP Hypertext Preprocessor,超文本预处理器,服务器端脚本语言:2.环境:WAMP:Window+Apache+PHP+MySQL: LAMP:Linux+Apach ...
- javascript_core_08之闭包、对象、原型
1.闭包: ①外层函数包裹受保护的变量和操作变量的内层函数: ②外层函数将内层函数返回到外部: ③调用外部函数,获得内层函数的对象: 2.面向对象:用对象描述现实一个具体事物属性和功能,按需调用功能, ...
- html_02之表单、其它
1.表单属性action:处理表单数据服务器端处理程序地址,默认提交本页: 2.表单属性method:①get:明文,数据显示地址栏,长度<2KB,向服务器请求数据时使用:②post:密文,提交 ...
- 代码生成AnimatorController
0.出发点 现在的项目需要设置多套动画组合,全部是由策划在XML文件中设置完成,如果完全的手动在AnimatorController中去做不但工作量大而且如果将来有配置修改了还要一个个去找到对应的自状 ...
- 《BI那点儿事》Microsoft 聚类分析算法——三国人物身份划分
什么是聚类分析? 聚类分析属于探索性的数据分析方法.通常,我们利用聚类分析将看似无序的对象进行分组.归类,以达到更好地理解研究对象的目的.聚类结果要求组内对象相似性较高,组间对象相似性较低.在三国数据 ...
- Angular从0到1:function(上)
1.前言 Angular作为最流行的前端MV*框架,在WEB开发中占据了重要的地位.接下来,我们就一步一步从官方api结合实践过程,来学习一下这个强大的框架吧. Note:每个function描述标题 ...
- XML序列化的时候如何支持Namespace
我曾经不止一次(当然不仅仅是我意识到这个问题)说到过,XML标准中的Namespace的设计其实是一个较为失败的设计,它有它的优点,但缺点更多. http://zzk.cnblogs.com/s?w= ...
- php基础教程-变量
变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念.变量可以通过变量名访问.在指令式语言中,变量通常是可变的:但在纯函数式语言(如Haskell)中,变量可能是不可变(immutable) ...
- Oracle ASM diskgroup在主机重启后启动失败
环境:RHEL 6.4 + Oracle 11.2.0.3 + ASM单实例 1.重启主机后,+DATA diskgroup启动不成功,现象如下: [grid@JY-DB ~]$ crsctl sta ...
- 【Java基础】序列化与反序列化深入分析
一.前言 复习Java基础知识点的序列化与反序列化过程,整理了如下学习笔记. 二.为什么需要序列化与反序列化 程序运行时,只要需要,对象可以一直存在,并且我们可以随时访问对象的一些状态信息,如果程序终 ...