1 package com.liuke.selenium.driver; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 import org.json.JSONException; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.Dimension; 8 import org.openqa.selenium.JavascriptExecutor; 9 import org.openqa.selenium.NoSuchElementException; 10 import org.openqa.selenium.Point; 11 import org.openqa.selenium.WebDriver; 12 import org.openqa.selenium.WebElement; 13 import org.openqa.selenium.interactions.internal.Coordinates; 14 import org.openqa.selenium.remote.RemoteWebElement; 15 import org.openqa.selenium.support.ui.Select; 16 17 public class JSWebElement { 18 private RemoteWebElement we = null; 19 private JavascriptExecutor jse = null; 20 21 public JSWebElement(){} 22 23 public JSWebElement(RemoteWebElement we){ 24 this.we = we; 25 } 26 27 /// 28 ///通过元素ID定位元素 29 /// 30 public JSWebElement findElementById(String using) { 31 try { 32 return new JSWebElement((RemoteWebElement)we.findElementById(using)); 33 }catch (NoSuchElementException e){ 34 return new JSWebElement(); 35 } 36 } 37 38 /// 39 ///通过元素CSS表达式定位元素 40 /// 41 public JSWebElement findElementByCssSelector(String using) { 42 try { 43 return new JSWebElement((RemoteWebElement)we.findElementByCssSelector(using)); 44 }catch (NoSuchElementException e){ 45 return new JSWebElement(); 46 } 47 } 48 49 /// 50 ///通过元素Xpath表达式定位元素 51 /// 52 public JSWebElement findElementByXPath(String using) { 53 try { 54 return new JSWebElement((RemoteWebElement)we.findElementByXPath(using)); 55 }catch (NoSuchElementException e){ 56 return new JSWebElement(); 57 } 58 } 59 60 /// 61 ///通过链接的文字定位元素 62 /// 63 public JSWebElement findElementByLinkText(String using) { 64 try { 65 return new JSWebElement((RemoteWebElement)we.findElementByLinkText(using)); 66 }catch (NoSuchElementException e){ 67 return new JSWebElement(); 68 } 69 } 70 71 /// 72 ///通过元素DOM表达式定位元素 73 /// 74 public JSWebElement findElementByDom(String using) { 75 try { 76 JavascriptExecutor js = this.getJSE(); 77 WebElement we = (WebElement)js.executeScript(String.format("return %s", using)); 78 return new JSWebElement((RemoteWebElement)we); 79 }catch (NoSuchElementException e){ 80 return new JSWebElement(); 81 } 82 } 83 84 /// 85 ///判断元素是否存在 86 /// 87 public Boolean isExist(){ 88 if (we != null){ 89 return true; 90 }else{ 91 return false; 92 } 93 } 94 95 /// 96 ///获取元素的HTML内容 97 /// 98 public String getHtml(){ 99 return we.getAttribute("outerHTML"); 100 } 101 102 /// 103 ///获取元素的文本内容 104 /// 105 public String getText(){ 106 return we.getText(); 107 } 108 109 /// 110 ///获取元素的value值 111 /// 112 public String getValue(){ 113 return this.getAttribute("value"); 114 } 115 116 /// 117 ///获取元素的特定属性值 118 /// 119 public String getAttribute(String name){ 120 return we.getAttribute(name); 121 } 122 123 /// 124 ///向可输入元素发送内容,如:text、textarea、filefield等 125 /// 126 public void sendKeys(String string){ 127 String old_bg = this.setBackground("yellow"); 128 try { 129 Thread.sleep(800); 130 } catch (InterruptedException e) { 131 e.printStackTrace(); 132 } 133 we.sendKeys(string); 134 this.setBackground(old_bg); 135 } 136 137 /// 138 ///判断元素是否可用 139 /// 140 public boolean isEnabled(){ 141 return we.isEnabled(); 142 } 143 144 /// 145 ///判断元素是否可见 146 /// 147 public boolean isVisible(){ 148 return we.isDisplayed(); 149 } 150 151 /// 152 ///清空可编辑元素的内容。不可编辑元素次操作会抛异常 153 /// 154 public void clear(){ 155 we.clear(); 156 } 157 158 /// 159 ///对元素进行点击操作 160 /// 161 public void click(){ 162 we.click(); 163 } 164 165 /// 166 ///检查元素的特定属性值 167 /// 168 public void checkAttr(String attribute, JSWebUtils utils) throws SQLException, JSONException 169 { 170 String [] attributes=attribute.split("=", 2); 171 String actual = this.we.getAttribute(attributes[0]); 172 if (actual == null){ actual = "null"; } 173 utils.checkPointBase(actual,attributes[1]); 174 } 175 176 /// 177 ///获取元素的CSS值 178 /// 179 public String getCssValue(String name) 180 { 181 return we.getCssValue(name); 182 } 183 184 /// 185 ///判断元素是否被选中 186 /// 187 public boolean isSelected() 188 { 189 return we.isSelected(); 190 } 191 192 /// 193 ///可选元素进行选中操作;如:select 194 /// 195 public void select(String by, String value) throws Exception 196 { 197 if (we.getTagName().equals("select")){ 198 Select select = new Select(we); 199 if (by.equals("index")){ 200 select.selectByIndex(Integer.parseInt(value)); 201 }else if (by.equals("value")){ 202 select.selectByValue(value); 203 }else if (by.equals("text")){ 204 select.selectByVisibleText(value); 205 } 206 }else{ 207 Exception e = new Exception("The element is not SELECT Object"); 208 throw e; 209 } 210 } 211 212 /// 213 ///对可选中元素进行取消选择操作;如:SELECT in multiple type 214 /// 215 public void deSelect(String by, String...value) throws Exception 216 { 217 if (we.getTagName().equals("select")){ 218 Select select = new Select(we); 219 if (by.equals("index")){ 220 select.deselectByIndex(Integer.parseInt(value[0])); 221 }else if (by.equals("value")){ 222 select.deselectByValue(value[0]); 223 }else if (by.equals("text")){ 224 select.deselectByVisibleText(value[0]); 225 }else if (by.equals("*")){ 226 select.deselectAll(); 227 } 228 }else{ 229 Exception e = new Exception("The element is not SELECT Object"); 230 throw e; 231 } 232 } 233 234 /// 235 ///判断下拉框是否为多选 236 /// 237 public boolean isMultiple() throws Exception 238 { 239 if (we.getTagName().equals("select")){ 240 Select select = new Select(we); 241 if (select.isMultiple()){ 242 return true; 243 }else{ 244 return false; 245 } 246 }else{ 247 Exception e = new Exception("The element is not SELECT Object"); 248 throw e; 249 } 250 } 251 252 /// 253 ///获取select的当前选中值 254 /// 255 public String getSelectedText() throws Exception 256 { 257 if (we.getTagName().equals("select")){ 258 String text = ""; 259 Select select = new Select(we); 260 List<WebElement> options = select.getAllSelectedOptions(); 261 for (WebElement w : options){ 262 text += w.getText() + "\r\n"; 263 } 264 return text; 265 }else{ 266 Exception e = new Exception("The element is not SELECT Object"); 267 throw e; 268 } 269 } 270 271 /// 272 ///判断指定项是否存在 273 /// 274 public boolean isInclude(String name) throws Exception 275 { 276 if (we.getTagName().equals("select")){ 277 Select select = new Select(we); 278 List<WebElement> options = select.getOptions(); 279 for (WebElement w : options){ 280 if (w.getText().equals(name)){ 281 return true; 282 } 283 } 284 return false; 285 }else{ 286 Exception e = new Exception("The element is not SELECT Object"); 287 throw e; 288 } 289 } 290 291 /// 292 ///获取元素的tagname 293 /// 294 public String getTagName(){ 295 return we.getTagName(); 296 } 297 298 /// 299 ///获取元素的id 300 /// 301 public String getId(){ 302 return we.getId(); 303 } 304 305 /// 306 ///获取元素的绝对位置 307 /// 308 public Point getLocation(){ 309 return we.getLocation(); 310 } 311 312 /// 313 ///获取元素的出现在屏幕可见区时的位置 314 /// 315 public Point getLocationOnScreenOnceScrolledIntoView(){ 316 return we.getLocationOnScreenOnceScrolledIntoView(); 317 } 318 319 /// 320 ///获取元素的坐标 321 /// 322 public Coordinates getCoordinates(){ 323 return we.getCoordinates(); 324 } 325 326 /// 327 ///获取元素的大小 328 /// 329 public Dimension getSize(){ 330 return we.getSize(); 331 } 332 333 /// 334 ///提交元素所在form的内容 335 /// 336 public void submit() 337 { 338 we.submit(); 339 } 340 341 /// 342 ///勾选radio、checkbox 343 /// 344 public void check(String...values) throws Exception 345 { 346 if (we.getTagName().equals("input")){ 347 if (we.getAttribute("type").equals("radio")){ 348 WebDriver wd = we.getWrappedDriver(); 349 List<WebElement> wl = wd.findElements(By.name(we.getAttribute("name"))); 350 if (values[0].equals("index")){ 351 wl.get(Integer.parseInt(values[1])).click(); 352 }else if (values[0].equals("value")){ 353 for (WebElement w : wl){ 354 if (w.getAttribute("value").equals(values[1])){ 355 w.click(); 356 break; 357 } 358 } 359 } 360 }else if (we.getAttribute("type").equals("checkbox")){ 361 if (!we.isSelected()){ 362 we.click(); 363 } 364 }else{ 365 Exception e = new Exception("The element is not Radio or CheckBox Object"); 366 throw e; 367 } 368 }else{ 369 Exception e = new Exception("The element is not INPUT Object"); 370 throw e; 371 } 372 } 373 374 /// 375 ///取消勾选checkbox 376 /// 377 public void unCheck() throws Exception 378 { 379 if (we.getTagName().equals("input") && we.getAttribute("type").equals("checkbox")){ 380 if (we.isSelected()){ 381 we.click(); 382 } 383 }else{ 384 Exception e = new Exception("The element is not CheckBox Object"); 385 throw e; 386 } 387 } 388 389 /// 390 ///checkbox、radio是否勾选 391 /// 392 public boolean isChecked(String...values) throws Exception 393 { 394 if (we.getTagName().equals("input")){ 395 if (we.getAttribute("type").equals("radio")){ 396 WebDriver wd = we.getWrappedDriver(); 397 List<WebElement> wl = wd.findElements(By.name(we.getAttribute("name"))); 398 if (values[0].equals("index")){ 399 return wl.get(Integer.parseInt(values[1])).isSelected(); 400 }else if (values[0].equals("value")){ 401 for (WebElement w : wl){ 402 if (w.getAttribute("value").equals(values[1])){ 403 return w.isSelected(); 404 } 405 } 406 } 407 return false; 408 }else if (we.getAttribute("type").equals("checkbox")){ 409 return we.isSelected(); 410 }else{ 411 Exception e = new Exception("The element is not Radio or CheckBox Object"); 412 throw e; 413 } 414 }else{ 415 Exception e = new Exception("The element is not INPUT Object"); 416 throw e; 417 } 418 } 419 420 /// 421 ///把元素滚动到可视区 422 /// 423 public void scroll() 424 { 425 this.focus(); 426 } 427 428 /// 429 ///高亮元素 430 /// 431 public void highLight() throws InterruptedException 432 { 433 this.focus(); 434 JavascriptExecutor js = getJSE(); 435 String old_style = we.getAttribute("style"); 436 for (int i = 0; i < 3; i++) { 437 js.executeScript("arguments[0].setAttribute('style', arguments[1]);", this.we, "background-color: red; border: 2px solid red;" + old_style); 438 Thread.sleep(500); 439 js.executeScript("arguments[0].setAttribute('style', arguments[1]);", this.we, old_style); 440 Thread.sleep(500); 441 } 442 } 443 444 /// 445 ///触发元素的特定事件 446 /// 447 public void fireEvent(String event){ 448 JavascriptExecutor js = getJSE(); 449 js.executeScript(String.format("arguments[0].%s()", event), this.we); 450 } 451 452 /// 453 ///使元素获取焦点 454 /// 455 public void focus(){ 456 // this.we.sendKeys(""); 457 JavascriptExecutor js = getJSE(); 458 js.executeScript("arguments[0].focus();", this.we); 459 } 460 461 /// 462 ///对元素执行JavaScript操作;即执行元素的dom操作 463 /// 464 public void executeJS(String commands){ 465 JavascriptExecutor js = getJSE(); 466 String[] comandArr = commands.split(";"); 467 commands = ""; 468 for (String comand : comandArr){ 469 if (!comand.trim().equals("")){ 470 commands += String.format("arguments[0].%s;", comand); 471 } 472 } 473 if (!commands.equals("")){ 474 js.executeScript(commands, this.we); 475 } 476 } 477 478 /// 479 ///获取原始的RemoteWebElement对象 480 /// 481 public RemoteWebElement getNativeWebElement(){ 482 return this.we; 483 } 484 485 private JavascriptExecutor getJSE(){ 486 if (this.isExist()){ 487 if (this.jse == null){ 488 WebDriver wd = we.getWrappedDriver(); 489 this.jse = (JavascriptExecutor) wd; 490 } 491 } 492 return jse; 493 } 494 495 private String setBackground(String color){ 496 JavascriptExecutor js = getJSE(); 497 String old_bg = we.getCssValue("background-color"); 498 js.executeScript("arguments[0].style.background = arguments[1];", this.we, color); 499 return old_bg; 500 } 501 502 }