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

  1. Java Selenium封装--RemoteWebDriver

    package com.selenium.driver; import java.io.File; import java.io.IOException; import java.net.URL; i ...

  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. Java+Selenium+Testng自动化测试学习(二)

    Java+Selenium+TestNG自动化测试框架整合 1.简化代码 封装一个定位元素的类,类型为ElementLocation package com.test; import org.open ...

随机推荐

  1. web应用程序性能优化

    web应用程序基本上都是在浏览器地址栏输入一段网站,然后进入,最后浏览器显示你想要的东西. 这就是用户所能体会到的东西.那作为程序员我们看到了什么呢? 一次HTTP 请求主要的流程是: 1.DNS服务 ...

  2. SeaJS与RequireJS最大的区别

    SeaJS与RequireJS最大的区别 U_U 2013-06-20 16:21:12 执行模块的机制大不一样-----------------------------------由于 Requir ...

  3. Aspect Oriented Programming

    AOP(Aspect Oriented Programming),面向切面编程(也叫面向方面)是目前软件开发中的一个热点.利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度 ...

  4. javascript运算符——位运算符

    × 目录 [1]二进制 [2]非 [3]与[4]或[5]异或[6]左移[7]右移[8]>>>[9]应用 前面的话 位运算符是非常底层的运算,由于其很不直观,所以并不常用.但是,其速度 ...

  5. AngularJs单元测试

    这篇文章主要介绍了angularJS中的单元测试实例,本文主要介绍利用Karma和Jasmine来进行ng模块的单元测试,并用Istanbul  来生成代码覆盖率测试报告,需要的朋友们可以参考下,以下 ...

  6. Jsp字符编码过滤器

    通过此过滤器,可以实现统一将编码设置为UTF-8. 1.首先在web.xml中配置,添加如下代码: <!-- 过滤器 --> <filter> <filter-name& ...

  7. isDebugEnabled有什么用?

    这几天在读Spring MVC源码时,发现了如下代码: if (logger.isDebugEnabled()) { logger.debug("Using ThemeResolver [& ...

  8. 关于DataTable的两篇基础文章

    DataTable有的时候还是很有用的.记录两篇不错的文章,当字典 1.http://blog.csdn.net/imagse/article/details/3085870 2.https://ms ...

  9. 《Inside UE4》-0-开篇

    <Inside UE4>-0-开篇 InsideUE4   前言 VR行业发展是越来越火热了,硬件设备上有HTC VIVE,Oculus rift,PS VR,各种魔镜:平台上有Steam ...

  10. Auto Mapper04(MVC中的配置)

    学习如何在MVC项目中配置AutoMapper. 一:首先在MVC项目中引用AutoMapper的DLL文件,接着创建一个接口,这里面我们需要定义两个方法,接口里面的方法只能定义不能实现,也没有什么修 ...