/** * Starts firefox through selenium to retrieve * cookie instance and process initialization. * * @param driverSupplier Driver supplier to use. * @param contest Contest identifier to use. * @return <tt>true</tt> if the init command was correctly executed, <tt>false</tt> otherwise. */ private static CommandStatus browserInit(final Supplier<WebDriver> driverSupplier, final String contest) { out.println("[Initialization] Web browser will open, please authenticate to your Google account with it."); FirefoxDriverManager.getInstance().setup(); final SeleniumCookieSupplier supplier = new SeleniumCookieSupplier(Request.getHostname() + "/codejam", FirefoxDriver::new); try { final String cookie = supplier.get(); if (cookie == null) { err.println("-> Retrieved cookie instance is null, abort."); } else { return init(cookie, contest); } } catch (final IOException | UnreachableBrowserException | GeneralSecurityException e) { err.println("-> An error occurs while creating CodeJamSession"); if (Application.isVerbose()) { e.printStackTrace(); } } return CommandStatus.FAILED; }
@Override protected void takeScreenshotImpl(String methodName) { try { final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); final IContext context = Context.getInstance(); context.addScreenshot(screenshot); } catch(UnreachableBrowserException ex) { logger.log(Level.WARN, "Could not reach browser to take screenshot"); } catch(WebDriverException webDriverEx) { if(webDriverEx.getMessage().contains("Session not found")) { logger.log(Level.WARN, "Could not reach browser to take screenshot"); } else { throw webDriverEx; } } }
public void takeScreenshot(String filename) { filename = renameIfClassPath(filename); screenShotDirectory = testClassDirectory.isEmpty() ? getDirectory("screenshots") : getDirectory("screenshots/" + testClassDirectory); if (log.isDebugEnabled()) { takeFullDesktopScreenshot(filename); } else { if (driver.isDriverStarted()) { try { filename += "_" + System.currentTimeMillis() + "_" + Thread.currentThread().getId() + ".png"; File scrFile = driver.takeScreenshot(); log.info("Creating Screenshot: " + screenShotDirectory + filename); FileUtils.copyFile(scrFile, new File(screenShotDirectory + filename)); } catch (IOException | UnreachableBrowserException e) { log.error(" Unable to take screenshot: " + e.toString()); } } else { log.error("Webdriver not started. Unable to take screenshot"); } } }
/** * Tear down. */ @AfterMethod(alwaysRun = true) public void tearDown() { try { if (null != mainDriver) { mainDriver.quit(); } if (null != context) { Map<String, IMyWebDriver> myWebDrivers = context .getBeansOfType(IMyWebDriver.class); for (IMyWebDriver myWebDriver2 : myWebDrivers.values()) { WebDriver weD = myWebDriver2.getWebDriver(); if (null != weD) weD.quit(); } } } catch (UnreachableBrowserException | NoSuchWindowException e) { // NOPMD // browser has been closed, no action needs to be done here. } if (null != context) { ((ConfigurableApplicationContext) context).close(); } }
private void checkCloseWindowAlert(String winh) throws NoAlertPresentException { try { Alert alt = getDriver().switchTo().alert(); if (alt == null) throw GlobalUtils.createInternalError("web driver"); PopupPromptDialog aDialog = new PopupPromptDialog(getDriver(), alt, alerts.size()); aDialog.setClosingWindowHandle(winh); alerts.add(aDialog); } catch (UnreachableBrowserException error) { if (getNumberOfOpenWindows() > 0) throw GlobalUtils .createInternalError("ATE multi windows handler", error); } catch (UnsupportedOperationException e) { throw GlobalUtils .createInternalError("Driver doesn't support alert handling yet", e); } catch (NoSuchWindowException windClosedAlready) { //do nothing if window closed without alert dialog intervention. for example in Chrome. throw new NoAlertPresentException(windClosedAlready); } }
/** * Get the current session associated with this thread. Because a * SessionManager instance is thread-local, the notion of current is also * specific to a thread. * * * @param createIfNotFound * set to true if a session should be created if no session is * associated with the current sessionId * @return ExtWebDriver an ExtWebDriver instance * @see getCurrentSession() * @see getSession(String) * @see switchToSession(String) */ public ExtWebDriver getCurrentSession(boolean createIfNotFound) { for (int i = 0; i < MAX_RETRIES; i++) { ExtWebDriver sel = sessions.get(currentSessionId); try { if ((sel == null) && (createIfNotFound)) { sel = getNewSession(); } return sel; } catch (Exception e) { // if the exception is of type UnreachableBrowserException, try // again if (!(e instanceof UnreachableBrowserException)) { e.printStackTrace(); } } } return null; }
/** * use {@link VisibleElementWaitFor#stale()} */ @Deprecated protected final Boolean waitForStalenessOf(final WebElement webElement) { try { return elementFinder.waitForStalenessOf(webElement); } catch (UnreachableBrowserException ignored) { return true; } }
/** * use {@link VisibleElementWaitFor#stale()} */ @Deprecated protected final Boolean waitForStalenessOf(final WebElement webElement, long timeout) { try { return elementFinder.waitForStalenessOf(webElement, timeout); } catch (UnreachableBrowserException ignored) { return true; } }
@PublicAtsApi public boolean isElementPresent() { try { return MobileElementFinder.findElement(appiumDriver, element) != null; } catch (UnreachableBrowserException ube) { throw new MobileOperationException("Check if there is connection to the target device and the Appium server is running", ube); } catch (Exception e) { // element is not present or got error checking if it is present return false; } }
@PublicAtsApi public boolean isElementDisplayed() { try { return MobileElementFinder.findElement(appiumDriver, element).isDisplayed(); } catch (UnreachableBrowserException ube) { throw new MobileOperationException("Check if there is connection to the target device and the Appium server is running", ube); } catch (Exception e) { // element is not present or got error checking if it is present return false; } }
public final void close() throws NotCurrentPageException { if (WebUIDriver.getWebDriver() == null) { return; } SeleniumTestsPageListener.informPageUnload(this); TestLogging.log("close web page"); boolean isMultipleWindow = false; if (driver.getWindowHandles().size() > 1) { isMultipleWindow = true; } try { driver.close(); } catch (final WebDriverException ignore) { } if (WebUIDriver.getWebUIDriver().getMode().equalsIgnoreCase("LOCAL")) { try { Thread.sleep(1000 * 2); } catch (final InterruptedException e) { } } try { if (isMultipleWindow) { this.selectWindow(); } else { WebUIDriver.setWebDriver(null); } } catch (final UnreachableBrowserException ex) { WebUIDriver.setWebDriver(null); } }
public void get(String url) { try { getDriver().get(url); } catch (UnreachableBrowserException e) { // this is a workaround for losing the connection or failing to start driver log.info("WebDriver died...attempting restart"); stopLastDriver(); startDriver(); getDriver().get(url); } }
public void getBaseURL() { try { driver.get(baseUrl); } catch (UnreachableBrowserException e) { System.out.println("driver.get() threw an exception."); e.printStackTrace(); } }
public void setDriver() { try { driver = new AndroidDriver(); } catch(UnreachableBrowserException e) { System.out.println("Could not set driver..."); } }
public void getBaseURL() { try { driver.get(baseUrl); } catch(UnreachableBrowserException e) { System.out.println("driver.get() threw an exception."); e.printStackTrace(); } }
private void expectWebDriverExceptionWithConnectionExceptionCause() { exception.expect(UnreachableBrowserException.class); exception.expectCause(any(ConnectException.class)); }