/** * @author wasiq.bhamla * @since 26-Apr-2017 8:54:58 PM */ public void longPress () { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); log.info (String.format ("Performing long press on element [%s]...", this.name)); try { final int beforeTap = this.device.getSetting () .getDelayBeforeTap (); final int afterTap = this.device.getSetting () .getDelayAfterTap (); this.touch.waitAction (ofSeconds (beforeTap)) .longPress (this.element) .waitAction (ofSeconds (afterTap)) .perform (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 12-May-2017 10:08:55 PM */ public void tap () { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); log.info (String.format ("Tapping on element [%s]...", this.name)); try { final int beforeTap = this.device.getSetting () .getDelayBeforeTap (); final int afterTap = this.device.getSetting () .getDelayAfterTap (); this.touch.waitAction (ofSeconds (beforeTap)) .tap (this.element) .waitAction (ofSeconds (afterTap)) .perform (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
public static void webGet2(WebDriver webDriver, String url) { try { webDriver.get(url); webGet2(webDriver, url); } catch (Exception e) { // e.printStackTrace(); if (e instanceof NoSuchSessionException) { System.out.println("浏览器关闭,程序退出"); System.exit(0); } /* * try { Thread.sleep(100); } catch (InterruptedException e1) { * e1.printStackTrace(); } JavascriptExecutor js = * (JavascriptExecutor) webDriver; * js.executeScript("window.stop();"); * System.out.println("已停止加载页面》》》》》》》》》》》》》》》》》》》》》》》》"); */ } }
@Override public byte[] captureScreen() { if (driver == null) { return null; } if (!(driver instanceof TakesScreenshot)) { return null; } try { return ((TakesScreenshot) driver) .getScreenshotAs(OutputType.BYTES); } catch (NoSuchSessionException e) { // just do nothing if WebDriver instance is in invalid state return null; } // TODO test should not fail when taking screen capture fails? }
@Override public byte[] captureScreen() { if (fluent == null) { return null; } WebDriver driver = fluent.getDriver(); if (driver == null) { return null; } if (!(driver instanceof TakesScreenshot)) { return null; } try { return ((TakesScreenshot) driver) .getScreenshotAs(OutputType.BYTES); } catch (NoSuchSessionException e) { // just do nothing if WebDriver instance is in invalid state return null; } }
/** * @author wasiq.bhamla * @since Oct 21, 2017 5:22:04 PM * @param text */ public void appendText (final String text) { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); tap (); log.info (String.format ("Appending text [%s] in element [%s]...", text, this.name)); try { this.element.sendKeys (text); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:49:52 PM */ public void clear () { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); log.info (String.format ("Clearing element [%s]...", this.name)); try { this.element.clear (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:51:07 PM * @return enabled */ public boolean enabled () { log.info (String.format ("Checking if element [%s] is enabled...", this.name)); try { return this.element.isEnabled (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } return false; }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:31:45 PM * @param text */ public void enterText (final String text) { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); tap (); clear (); log.info (String.format ("Entering text [%s] in element [%s]...", text, this.name)); try { this.element.sendKeys (text); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:49:08 PM */ public void pinch () { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); log.info (String.format ("Pinching on element [%s]...", this.name)); try { fail (NotImplementedError.class, "Pinch! Coming Soon!!"); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:50:40 PM * @return selected */ public boolean selected () { log.info (String.format ("Checking if element [%s] is selected...", this.name)); try { return this.element.isSelected (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } return false; }
/** * @author wasiq.bhamla * @since 06-May-2017 4:56:42 PM */ public void submit () { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); log.info (String.format ("Performing submit on element [%s]...", this.name)); try { this.element.submit (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 12-May-2017 10:07:14 PM * @param direction */ public void swipe (final SwipeDirection direction) { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); log.info (String.format ("Swiping [%s] on element [%s]...", direction, this.name)); try { fail (NotImplementedError.class, "Swipe! Coming Soon!!"); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:52:40 PM * @return text */ public String text () { log.info (String.format ("Getting text on element [%s]...", this.name)); try { return this.element.getText (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } return null; }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:51:40 PM * @return visible */ public boolean visible () { log.info (String.format ("Checking if element [%s] is visible...", this.name)); try { return this.element.isDisplayed (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } return false; }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:48:10 PM */ public void zoom () { DeviceChecker.checkDeviceElementEnabled (this.element, this.name); log.info (String.format ("Zooming on element [%s]...", this.name)); try { fail (NotImplementedError.class, "Zoom! Coming Soon!!"); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 8:34:05 PM */ public void hideKeyboard () { log.info ("Hiding the keyboard..."); try { this.driver.hideKeyboard (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 01-May-2017 8:24:34 PM * @param path */ private void captureScreenshot (final String path) { final String msg = "Capturing screenshot and saving at [%s]..."; log.info (format (msg, path)); try { final File srcFiler = this.driver.getScreenshotAs (OutputType.FILE); copyFile (srcFiler, path); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
@Override public void hideKeyboard () { log.info ("Hiding keyboard on device..."); try { this.driver.hideKeyboard (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 9:09:43 PM * @return activity */ public String currentActivity () { log.info ("Getting current activity name..."); try { return this.driver.currentActivity (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } return null; }
/** * @author wasiq.bhamla * @since 26-Apr-2017 9:11:35 PM * @return isLocked */ public boolean isLocked () { log.info ("Checking if device is locked..."); try { return this.driver.isLocked (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } return false; }
/** * @author wasiq.bhamla * @since 26-Apr-2017 9:11:45 PM */ public void lock () { log.info ("Locking the Android device..."); try { this.driver.lockDevice (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since Oct 21, 2017 8:27:50 PM */ public void pressBack () { log.info ("Pressing Back button on Android device..."); try { this.driver.pressKeyCode (AndroidKeyCode.BACK); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 9:11:52 PM */ public void unlock () { log.info ("Unlocking the Android device..."); try { this.driver.unlockDevice (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 08-May-2017 3:21:20 PM * @param strategy * @param keyName */ public void hideKeyboard (final String strategy, final String keyName) { final String msg = "Hiding keyboard on device using %s strategy for key %s..."; log.info (String.format (msg, strategy, keyName)); try { this.driver.hideKeyboard (strategy, keyName); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
/** * @author wasiq.bhamla * @since 26-Apr-2017 11:37:04 PM */ public void shake () { log.info ("Shaking the device..."); try { this.driver.shake (); } catch (final NoSuchSessionException e) { fail (AppiumServerStoppedError.class, SERVER_STOPPED, e); } }
public static void webGet(WebDriver webDriver, String url) { /*try{ String title = webDriver.getTitle(); }catch(Exception e){ System.out.println("浏览器关闭,程序退出"); System.exit(0); }*/ try { webDriver.get(url); } catch (Exception e) { // e.printStackTrace(); if (e instanceof NoSuchSessionException) { System.out.println("浏览器关闭,程序退出"); System.exit(0); } String msg = e.getMessage(); if(msg.contains("chrome not reachable")){ System.out.println("浏览器关闭,程序退出"); System.exit(0); } /* * try { Thread.sleep(100); } catch (InterruptedException e1) { * e1.printStackTrace(); } JavascriptExecutor js = * (JavascriptExecutor) webDriver; * js.executeScript("window.stop();"); * System.out.println("已停止加载页面》》》》》》》》》》》》》》》》》》》》》》》》"); */ } }
/** * Wrapper for interacting with a targeted driver that may or may not actually be present. */ private void attempt(Runnable action) { try { action.run(); } catch (NoSuchFrameException | NoSuchWindowException | NoSuchSessionException e) { throw new FindableNotPresentException(this, e); } }
/** * Wrapper for interacting with a targeted driver that may or may not actually be present. * Returns a result. */ private <T> T attemptAndGet(Supplier<T> action) { try { return action.get(); } catch (NoSuchFrameException | NoSuchWindowException | NoSuchSessionException e) { throw new FindableNotPresentException(this, e); } }
private Map<String, String> addElementLocatorInformation() { Map<String, String> elementData = new HashMap<>(); Boolean waitingForData = true; Boolean browserRunaway = false; String name = null; while (waitingForData) { String payload = null; try { payload = utils.getPayload(); } catch (NoSuchSessionException e) { waitingForData = false; break; } if (!payload.isEmpty()) { if (payload.contains((CharSequence) "ElementCodeName")) { // objects cannot suicide elementData = new HashMap<>(); name = utils.readVisualSearchResult(payload, Optional.of(elementData)); logger.debug("Processing payload..."); if (name == null || name.isEmpty()) { logger.info("Rejected unfinished visual search."); } else { logger.info( String.format("Received element data for the element: '%s'\n%s", name, payload)); elementData.put("ElementPageURL", utils.getCurrentUrl()); waitingForData = false; break; } } } if (browserStatus.get("runaway")) { logger.info("Detected URL change"); browserRunaway = true; waitingForData = false; } if (waitingForData) { try { // TODO: add the alternative code to // bail if waited long enough already logger.info("Waiting."); Thread.sleep(1000); } catch (InterruptedException exception) { } } } // clear results on the page utils.flushVisualSearchResult(); if (!browserRunaway) { utils.closeVisualSearch(); } return elementData; }