首页 > 编程语言 >Java Selenium封装--RemoteWebElement

Java Selenium封装--RemoteWebElement

时间:2022-12-20 11:08:09浏览次数:49  
标签:Exception return String -- Selenium equals RemoteWebElement new public


package com.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;
}

}



标签:Exception,return,String,--,Selenium,equals,RemoteWebElement,new,public
From: https://blog.51cto.com/u_15918230/5954436

相关文章

  • linux下安装pymssql
    各版本的下载地址:​​https://pypi.python.org/pypi/pymssql/​​Windows可以下载installer文件,直接是编译好的,可以直接安装Linux下需要安装几个基础类库:Cython:pipinstall......
  • gem使用代理安装ruby-debug-base19
    gem使用代理的两种方式:方式一:>>SEThttp_proxy=http://ip_or_host:PORT>>geminstallthe_package_name方式二:>>gemlist-phttp://user:passwd@ip_or_host:8080-r安装ru......
  • 注入html源码到浏览器的几种方式
    1、通过各浏览器提供的接口调用IE的COM接口,FF的插件、Chrome的API接口等;类似的实现有Selenium的webdriver支持的各种driver,它们都是调用了浏览器的原始接口。2、通过已......
  • selenium webdriver的各种driver
    selenium官方加上第三方宣布支持的驱动有很多种;除了PC端的浏览器之外,还支持iphone、android的driver;大概记录一下selenium支持的各种driver的用途与说明。selenium可支持的P......
  • webpy中配置发送邮件服务
    官方cookbook链接:http://webpy.org/cookbook/sendmail.zh-cn前提:需要安装web.py了,因为我这个web应用是用web.py写的#!\urs\bin\envpython#encoding:utf-8importwebdefse......
  • hadoop系基础概念扫盲
    hadoop版本:hadoop1.0,hadoop2.0Hadoop1.0由一个分布式文件系统HDFS和一个离线计算框架MapReduce组成。HDFS由一个NameNode和多个DataNode组成,Map......
  • CentOS7基础网络配置
    1、网卡配置vi/etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0HWADDR=00:0C:29:6C:BB:E6NM_CONTROLLED="yes"ONBOOT=yesPEERDNS=yesBOOTPROTO=staticIPADDR=192.168......
  • MySQL使用--存储过程篇
    查看存储过程:select`name`frommysql.procwheredb='your_db_name'and`type`='PROCEDURE'SHOWprocedurestatus;查看存储过程的创建代......
  • python2.X编码问题梳理
    首先这些问题只有在python2.X版本出现,因为3.X版本中python环境就只有unicode类型的字符串了,即所有程序中处理的都会自动转换成unicode字符串。那么2.X......
  • MySQL使用--通用操作篇
    服务启动:netstartmysql(windows启动)"/etc/rc.d/init.d/mysqldstart"(linux启动)用户登录:mysql[mydb]-h127.0.0.1-uroot-ppassword;常用统计......