|
2#
楼主 |
发表于 2018-3-14 14:33:27
|
只看该作者
- 240 str = driver.findElementByXPath(locateValue).getText().trim();
- 241 break;
- 242 default:
- 243 logger.error("定位方式:"+locateWay+"不被支持");
- 244 Assert.fail("定位方式:"+locateWay+"不被支持");
- 245
- 246 }
- 247 return str;
- 248
- 249 }
- 250
- 251 /**提交*/
- 252 public void submit(By by){
- 253 WebElement element=findElement(by);
- 254 try{
- 255 element.submit();
- 256 }catch(Exception e){
- 257 logger.error("在元素:"+getLocatorByElement(element, ">")+"做的提交操作失败",e);
- 258 Assert.fail("在元素:"+getLocatorByElement(element, ">")+"做的提交操作失败",e);
- 259 }
- 260 logger.info("在元素:"+getLocatorByElement(element, ">")+"做了提交操作");
- 261 }
- 262
- 263 /**
- 264 * 获得webview页面的标题
- 265 * */
- 266 public String getTitle() {
- 267 return driver.getTitle();
- 268 }
- 269
- 270 /**
- 271 * 获得元素 属性的文本
- 272 * */
- 273 public String getAttributeText(By elementLocator, String attribute) {
- 274 return findElement(elementLocator).getAttribute(attribute).trim();
- 275 }
- 276
- 277 /**
- 278 * 在给定的时间内去查找元素,如果没找到则超时,抛出异常
- 279 * */
- 280 public void waitForElementToLoad(int elementTimeOut, final By By) {
- 281 logger.info("开始查找元素[" + By + "]");
- 282 try {
- 283 (new WebDriverWait(driver, elementTimeOut)).until(new ExpectedCondition<Boolean>() {
- 284
- 285 public Boolean apply(WebDriver driver) {
- 286 WebElement element = driver.findElement(By);
- 287 return element.isDisplayed();
- 288 }
- 289 });
- 290 } catch (TimeoutException e) {
- 291 logger.error("超时!! " + elementTimeOut + " 秒之后还没找到元素 [" + By + "]");
- 292 Assert.fail("超时!! " + elementTimeOut + " 秒之后还没找到元素 [" + By + "]");
- 293
- 294 }
- 295 logger.info("找到了元素 [" + By + "]");
- 296 }
- 297
- 298 /**
- 299 * 判断文本是不是和需求要求的文本一致
- 300 * **/
- 301 public void isTextCorrect(String actual, String expected) {
- 302 try {
- 303 Assert.assertEquals(actual, expected);
- 304 } catch (AssertionError e) {
- 305 logger.error("期望的文字是 [" + expected + "] 但是找到了 [" + actual + "]");
- 306 Assert.fail("期望的文字是 [" + expected + "] 但是找到了 [" + actual + "]");
- 307
- 308 }
- 309 logger.info("找到了期望的文字: [" + expected + "]");
- 310
- 311 }
- 312
- 313 /**
- 314 * 暂停当前用例的执行,暂停的时间为:sleepTime
- 315 * */
- 316 public void pause(int sleepTime) {
- 317 if (sleepTime <= 0) {
- 318 return;
- 319 }
- 320 try {
- 321 TimeUnit.SECONDS.sleep(sleepTime);
- 322 logger.info("暂停:"+sleepTime+"秒");
- 323 } catch (InterruptedException e) {
- 324 e.printStackTrace();
- 325 }
- 326
- 327 }
- 328
- 329
- 330
- 331 /** 根据元素来获取此元素的定位值 */
- 332 public String getLocatorByElement(WebElement element, String expectText) {
- 333 String text = element.toString();
- 334 String expect = null;
- 335 try {
- 336 expect = text.substring(text.indexOf(expectText) + 1, text.length() - 1);
- 337 } catch (Exception e) {
- 338 e.printStackTrace();
- 339 logger.error("failed to find the string [" + expectText + "]");
- 340
- 341 }
- 342
- 343 return expect;
- 344
- 345 }
- 346
- 347
- 348 /**
- 349 * 判断实际文本时候包含期望文本
- 350 *
- 351 * @param actual
- 352 * 实际文本
- 353 * @param expect
- 354 * 期望文本
- 355 */
- 356 public void isContains(String actual, String expect) {
- 357 try {
- 358 Assert.assertTrue(actual.contains(expect));
- 359 } catch (AssertionError e) {
- 360 logger.error("The [" + actual + "] is not contains [" + expect + "]");
- 361 Assert.fail("The [" + actual + "] is not contains [" + expect + "]");
- 362 }
- 363 logger.info("The [" + actual + "] is contains [" + expect + "]");
- 364 }
- 365
- 366 /**跳转到webview页面*/
- 367 public void switchWebview(int index){
- 368 Set<String> contexts = driver.getContextHandles();
- 369 for (String context : contexts) {
- 370 System.out.println(context);
- 371 //打印出来看看有哪些context
- 372 }
- 373 driver.context((String) contexts.toArray()[index]);
- 374
- 375 }
- 376
- 377
- 378 /**跳转到webview页面*/
- 379 public void switchWebview(String contextName){
- 380 try{
- 381 Set<String> contexts = driver.getContextHandles();
- 382 for (String context : contexts) {
- 383 System.out.println(context);
- 384 //打印出来看看有哪些context
- 385 }
- 386 driver.context(contextName);
- 387 }catch(NoSuchContextException nce){
- 388 logger.error("没有这个context:"+contextName, nce);
- 389 Assert.fail("没有这个context:"+contextName, nce);
- 390 }
- 391
- 392 }
- 393
- 394
- 395 /**
- 396 * 执行JavaScript 方法
- 397 * */
- 398 public void executeJS(String js) {
- 399 ((JavascriptExecutor) driver).executeScript(js);
- 400 logger.info("执行JavaScript语句:[" + js + "]");
- 401 }
- 402
- 403 /**
- 404 * 执行JavaScript 方法和对象
- 405 * 用法:seleniumUtil.executeJS("arguments[0].click();", seleniumUtil.findElementBy(MyOrdersPage.MOP_TAB_ORDERCLOSE));
- 406 * */
- 407 public void executeJS(String js, Object... args) {
- 408 ((JavascriptExecutor) driver).executeScript(js, args);
- 409 logger.info("执行JavaScript语句:[" + js + "]");
- 410 }
- 411
- 412 /**检查元素是不是存在*/
- 413 public boolean doesElementsExist(By byElement){
- 414 try{
- 415 findElement(byElement);
- 416 return true;
- 417 }catch(NoSuchElementException nee){
- 418
- 419 return false;
- 420 }
- 421
- 422
- 423 }
- 424
- 425 /**长按操作*/
- 426 public void longPress(By by){
- 427 TouchAction tAction=new TouchAction(driver);
- 428 tAction.longPress(findElement(by)).perform();
- 429 }
- 430
- 431 /**滑动*/
- 432 public void swipe(int beginX,int beginY,int endX,int endY){
- 433 TouchAction tAction=new TouchAction(driver);
- 434 try{
- 435 tAction.press(beginX,beginY).moveTo(endX,endY).release().perform();
- 436 }catch(Exception e){
- 437 e.printStackTrace();
- 438 }
- 439 }
- 440
- 441 /**滚动 - 根据文本模糊匹配*/
- 442 public void scroll(String text){
- 443 driver.scrollTo(text);
- 444 }
- 445
- 446 /**滚动 - 根据文本精准匹配*/
- 447 public WebElement scrollExact(String text){
- 448 return driver.scrollToExact(text);
- 449 }
- 450
- 451 /**拖拽操作*/
- 452 public void DragAndDrop(By dragElement,By dropElement){
- 453 TouchAction act=new TouchAction(driver);
- 454 act.press(findElement(dragElement)).perform();
- 455 act.moveTo(findElement(dropElement)).release().perform();
- 456 }
- 457
- 458 /**放大和缩小*/
- 459 public void zoomAndPinch(int beginX,int beginY,int endX,int endY){
- 460 int scrHeight = driver.manage().window().getSize().getHeight();
- 461 int scrWidth = driver.manage().window().getSize().getWidth();
- 462 MultiTouchAction multiTouch = new MultiTouchAction(driver);
- 463 TouchAction tAction0 = new TouchAction(driver);
- 464 TouchAction tAction1 = new TouchAction(driver);
- 465 tAction0.press(scrWidth/2,scrHeight/2).waitAction(1000).moveTo(beginX,beginY).release();
- 466 tAction1.press(scrWidth/2,scrHeight/2+40).waitAction(1000).moveTo(endX,endY).release();
- 467 multiTouch.add(tAction0).add(tAction1);
- 468 multiTouch.perform();
- 469
- 470 }
- 471
- 472 /**app置于后台运行*/
- 473 public void runBackgound(int runTimes){
- 474 driver.runAppInBackground(runTimes);
- 475
- 476 }
- 477
- 478 /**收起键盘*/
- 479 public void hideKeyboard(){
- 480 driver.hideKeyboard();
- 481 logger.info("虚拟键盘已经收起");
- 482
- 483 }
- 484
- 485 /**安装app*/
- 486 public void instalApp(String appPath){
- 487 try{
- 488 driver.installApp(appPath);
- 489 }catch(Exception e){
- 490 logger.error("app安装失败",e);
- 491 Assert.fail("app安装失败",e);
- 492 }
- 493 }
- 494
- 495 /**app是否安装*/
- 496 public boolean isAppInstalled(String appPackage){
- 497
- 498 if(driver.isAppInstalled(appPackage)){
- 499 logger.info(appPackage+":已经安装");
- 500 return true;
- 501 }else {
- 502 logger.info(appPackage+":未安装");
- 503 return false;
- 504 }
- 505 }
- 506
- 507 /**页面过长时候滑动页面 window.scrollTo(左边距,上边距); */
- 508 public void scrollPage(int x,int y){
- 509 String js ="window.scrollTo("+x+","+y+");";
- 510 ((JavascriptExecutor)driver).executeScript(js);
- 511 }
- 512
- 513
- 514
- 515 }
复制代码
|
|