@After public void tearDown(Scenario scenario) { if (scenario.isFailed()) { try { logger.info("Test failed, taking screenshot"); byte[] screenshot = ((TakesScreenshot) new Augmenter().augment(getAppiumDriver())) .getScreenshotAs(OutputType.BYTES); scenario.embed(screenshot, "image/png"); } catch (WebDriverException wde) { System.err.println(wde.getMessage()); } catch (ClassCastException cce) { cce.printStackTrace(); } } application.tearDown(); }
@AfterMethod public void setScreenshot(ITestResult result) { if (!result.isSuccess()) { try { WebDriver returned = new Augmenter().augment(webDriver); if (returned != null) { File f = ((TakesScreenshot) returned).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(f, new File(SCREENSHOT_FOLDER + result.getName() + SCREENSHOT_FORMAT)); } catch (IOException e) { e.printStackTrace(); } } } catch (ScreenshotException se) { se.printStackTrace(); } } }
@Override public boolean createScreenshot(WebDriverConnector wd, File screenshotFile) throws Exception { WebDriver ad = wd.driver(); if(ad instanceof RemoteWebDriver && wd.getDriverType() == WebDriverType.REMOTE) { //for remote drivers we need to do augmenter thingy, for local we must not ad = new Augmenter().augment(ad); } if(ad instanceof TakesScreenshot) { File f = ((TakesScreenshot) ad).getScreenshotAs(OutputType.FILE); try { FileTool.copyFile(screenshotFile, f); } finally { FileTool.closeAll(f); } return true; } return false; }
/** * 鎴浘 * * @param driver * @param url * @param filePath * @return */ public static String capture(WebDriver driver, String url, String filePath) { if (driver == null) { return null; } if (!url.startsWith("http:") && !url.startsWith("https:")) { return null; } if (StringUtils.isBlank(filePath)) { return null; } try { driver.get(url); WebDriver augmentedDriver = new Augmenter().augment(driver); File screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); File file = new File(filePath); FileUtils.copyFile(screenshot, file); logger.info("capture success!"); } catch (IOException e) { logger.error("browser capture is error!", e); filePath = null; throw new RuntimeException(e); } return filePath; }
/** * 鎴浘 * * @param driver * @param url * @param filePath * @return */ public static String capture(WebDriver driver, String url) { if (driver == null) { return null; } if (!url.startsWith("http:") && !url.startsWith("https:")) { return null; } String response; try { driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); driver.get(url); WebDriver augmentedDriver = new Augmenter().augment(driver); response = augmentedDriver.getPageSource(); logger.info("content success!"); } catch (Exception e) { logger.error("browser content is error!", e); throw new RuntimeException(e); } return response; }
/** * 鎴浘 * * @param driver * @param url * @param filePath * @return */ public static String captureDataoke(WebDriver driver, String url, String filePath) { if (driver == null) { return null; } if (!url.startsWith("http:") && !url.startsWith("https:")) { return null; } if (StringUtils.isBlank(filePath)) { return null; } try { driver.get(url); WebDriver augmentedDriver = new Augmenter().augment(driver); File screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); File file = new File(filePath); FileUtils.copyFile(screenshot, file); logger.info("capture success!"); } catch (IOException e) { logger.error("browser capture is error!", e); filePath = null; throw new RuntimeException(e); } return filePath; }
/** * Tira uma foto da p�gina * * @param path * - Retorna o local que a imagem foi gravada. * @author Rog�rio Figueiredo. */ public String captureScreen(String path) { path = path + "\\"; try { WebDriver augmentedDriver = new Augmenter().augment(driver); File source = ((TakesScreenshot) augmentedDriver) .getScreenshotAs(OutputType.FILE); path = path + source.getName(); FileUtils.copyFile(source, new File(path)); } catch (IOException e) { path = "Failed to capture screenshot: " + e.getMessage(); } System.out.println("Local que foi gravado a imagem: " + path); return path; }
/** * Takes screenshot of current page (as .png). * @param baseName name for file created (without extension), * if a file already exists with the supplied name an * '_index' will be added. * @return absolute path of file created. */ public String takeScreenshot(String baseName) { String result = null; WebDriver d = driver(); if (!(d instanceof TakesScreenshot)) { d = new Augmenter().augment(d); } if (d instanceof TakesScreenshot) { TakesScreenshot ts = (TakesScreenshot) d; byte[] png = ts.getScreenshotAs(OutputType.BYTES); result = writeScreenshot(baseName, png); } return result; }
private void takeScreenshot(String testName) throws Exception { WebDriver driver = getDriver(); String screenshotDirectory = System.getProperty("screenshotDirectory"); String screenshotAbsolutePath = screenshotDirectory + File.separator + System.currentTimeMillis() + "_" + testName + ".png"; File screenshot = new File(screenshotAbsolutePath); if (createFile(screenshot)) { try { writeScreenshotToFile(driver, screenshot); } catch (ClassCastException weNeedToAugmentOurDriverObject) { writeScreenshotToFile(new Augmenter().augment(driver), screenshot); } System.out.println("Written screenshot to " + screenshotAbsolutePath); } else { System.err.println("Unable to create " + screenshotAbsolutePath); } }
@Override public void onTestFailure(ITestResult tr) { if(DriverAdapter.getDriver() != null) { WebDriver augmentedDriver = new Augmenter().augment(DriverAdapter.getDriver()); File screenShot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(screenShot, new File(System.getProperty("testReport") + "/screenshots/"+tr.getMethod().getMethodName().toLowerCase()+".jpg")); } catch(IOException e) { System.err.println(e.getMessage()); } } log("F"); super.onTestFailure(tr); }
public static void takeScreenShot() throws Exception { WebDriver augmentedDriver = new Augmenter().augment(driver); File screenshot = ((TakesScreenshot) augmentedDriver) .getScreenshotAs(OutputType.FILE); String nameScreenshot = UUID.randomUUID().toString() + ".png"; File directory = new File("."); String newFileNamePath = directory.getCanonicalPath() + pathSeparator + "target" + pathSeparator + "jbehave" + pathSeparator + "screenshots" + pathSeparator + nameScreenshot; FileUtils.copyFile(screenshot, new File(newFileNamePath)); // return newFileNamePath; /* * Reporter.log(message + "<br/><a href='file:///" + path + * "'> <img src='file:///" + path + * "' height='100' width='100'/> </a>"); */ }
public File takeScreenShotFile(Session session) { boolean event = true; long timeout = System.currentTimeMillis() + (session.getCerberus_selenium_wait_element()); //Try to capture picture. Try again until timeout is WebDriverException is raised. while (event) { try { WebDriver augmentedDriver = new Augmenter().augment(session.getDriver()); File image = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); if (image != null) { //logs for debug purposes LOG.info("WebDriverService: screen-shot taken with succes: " + image.getName() + "(size" + image.length() + ")"); } else { LOG.warn("WebDriverService: screen-shot returned null: "); } return image; } catch (WebDriverException exception) { if (System.currentTimeMillis() >= timeout) { LOG.warn(exception.toString()); } event = false; } } return null; }
private void setUpScreenshot(String screenshotPath) throws IOException { if (Boolean.valueOf(System.getProperty("LOCAL_DRIVER"))) { takeScreenshot(); writeScreenshotToFileSystem(screenshotPath); LOGGER.log(Level.FINEST, MESSAGE_LOCAL_SCREENSHOT_SUCCESSFUL + screenshotPath); } else if (Boolean.valueOf(System.getProperty("REMOTE_DRIVER"))) { driver.set(new Augmenter().augment(driver.get())); takeScreenshot(); writeScreenshotToFileSystem(screenshotPath); LOGGER.log(Level.FINEST, MESSAGE_REMOTE_SCREENSHOT_SUCCESSFUL + screenshotPath); } else if (Boolean.valueOf(System.getProperty("SAUCE_DRIVER"))) { LOGGER.log(Level.WARNING, MESSAGE_SAUCE_SCREENSHOT_FAILURE); } else { LOGGER.log(Level.WARNING, MESSAGE_CANNOT_TAKE_SCREENSHOT_UNABLE_TO_IDENTIFY_DRIVER_TYPE); } }
@Override public <X> X getScreenshotAs(OutputType<X> target) { if (getWebDriver().getClass() == RemoteWebDriver.class) { WebDriver augmentedDriver = new Augmenter().augment(getWebDriver()); return ((TakesScreenshot) augmentedDriver).getScreenshotAs(target); } else { return ((TakesScreenshot) getWebDriver()).getScreenshotAs(target); } }
@Override public <X> X getScreenshotAs(final OutputType<X> target) throws WebDriverException { if (driver.getClass() == RemoteWebDriver.class) { WebDriver augmentedDriver = new Augmenter().augment(driver); return ((TakesScreenshot) augmentedDriver).getScreenshotAs(target); } else { return ((TakesScreenshot) driver).getScreenshotAs(target); } }
@After public void afterScenario( Scenario scenario ) throws Exception { try { attachLog(); if( scenario.isFailed() ) { try { scenario.write( "Current Page URL is " + getDriver().getCurrentUrl() ); byte[] screenshot; try { screenshot = ( ( TakesScreenshot ) getDriver() ).getScreenshotAs( OutputType.BYTES ); } catch( ClassCastException weNeedToAugmentOurDriverObject ) { screenshot = ( ( TakesScreenshot ) new Augmenter().augment( getDriver() ) ).getScreenshotAs( OutputType.BYTES ); } String relativeScrnShotPath = takeScreenshot().substring(takeScreenshot().indexOf("screenshots")); Reporter.addScreenCaptureFromPath(relativeScrnShotPath, getDriver().getCurrentUrl()); } catch( WebDriverException somePlatformsDontSupportScreenshots ) { System.err.println( somePlatformsDontSupportScreenshots.getMessage() ); } } } finally { closeDriverObjects(); } }
public static void takeScreenshot(final WebDriver driver) { final WebDriver augmentedDriver = new Augmenter().augment(driver); final File screenshot = ((TakesScreenshot) augmentedDriver). getScreenshotAs(OutputType.FILE); Try.run(() -> FileUtils.copyFile(screenshot, new File("screen" + RandomStringUtils.randomNumeric(3) + ".png"))); }
public static String screenShot(BrowserEmulator be) { String dir = "screenshot"; String time = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()); String screenShotPath = dir + File.separator + time + ".png"; WebDriver augmentedDriver = null; if (GlobalSettings.browserCoreType == 1 || GlobalSettings.browserCoreType == 3) { augmentedDriver = be.getBrowserCore(); augmentedDriver.manage().window().setPosition(new Point(0, 0)); augmentedDriver.manage().window().setSize(new Dimension(9999, 9999)); } else if (GlobalSettings.browserCoreType == 2) { augmentedDriver = new Augmenter().augment(be.getBrowserCore()); } else { return "Incorrect browser type"; } try { File sourceFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(sourceFile, new File(screenShotPath)); } catch (Exception e) { e.printStackTrace(); return "Failed to screenshot"; } // Convert '\' into '/' for web image browsing. return screenShotPath.replace("\\", "/"); }
@Nullable @Override public BufferedImage createScreenshot(@Nonnull WebDriverConnector wd) throws Exception { WebDriver ad = wd.driver(); if(ad instanceof RemoteWebDriver && wd.getDriverType() == WebDriverType.REMOTE) { //for remote drivers we need to do augmenter thingy, for local we must not ad = new Augmenter().augment(ad); } if(ad instanceof TakesScreenshot) { File f = ((TakesScreenshot) ad).getScreenshotAs(OutputType.FILE); return ImageIO.read(f); } return null; }
public File captureScreenShot(String destination) throws IOException { WebDriver augmentedDriver = new Augmenter().augment(getDriver()); File srcFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); File output = new File(destination); FileUtils.copyFile(srcFile, output); return output; }
/** * Write an event message to the log. * * @param text Event message * @param makeDump if true, page source dump will be created. */ public void logEvent(final String text, final boolean makeDump) { String addStr = ""; if (!text.startsWith("<font")) { tc.getTestClassExecutionData().addSysOut(text); } if (tc.getTestClassExecutionData().getHtmlRunReporter().getTestMethodHtmlLog() != null) { if (makeDump) { try { String dumpFileName = dumpSource(true); File dumpFile = new File(dumpFileName); String screenshotFileName = dumpFileName + ".png"; if (selenium != null) { selenium.windowMaximize(); WebDriver augmentedDriver = new Augmenter().augment(webDriver); File screenShot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); FileUtil fileUtil = new FileUtil(); File screenShotFile = new File(screenshotFileName); fileUtil.copy(screenShot, screenShotFile); } String dumpFileName2 = dumpSource(false); File dumpFile2 = new File(dumpFileName2); addStr = " <small>[<a href=\"" + dumpFile.getName() + "\" target=\"_new\">source</a>]" + " [<a href=\"" + dumpFile2.getName() + "\" target=\"_new\">view</a>]" + " [<a href=\"" + dumpFile.getName() + ".png\" target=\"_new\">screenshot</a>]</small>"; } catch (Exception e) { addStr = " <small>[Dump failed]</small>"; } } tc.getTestClassExecutionData().getHtmlRunReporter().getTestMethodHtmlLog().insertText("<tr><td> </td><td bgcolor=\"#F0F0F0\">" + text + addStr + "</td></tr>\n"); } }
private static WebDriver getSeleniumGridRemoteDriver() throws MalformedURLException { final DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); desiredCapabilities.setBrowserName(System.getProperty("selenium.grid.browser", "firefox")); desiredCapabilities.setVersion(System.getProperty("selenium.grid.version", "")); desiredCapabilities.setPlatform(Platform.ANY); final URL url = new URL("http", System.getProperty("selenium.grid"), Integer.valueOf(System.getProperty( "selenium.grid.port", "4444")), "/wd/hub"); final RemoteWebDriver remoteWebDriver = new RemoteWebDriver(url, desiredCapabilities); remoteWebDriver.setFileDetector(new LocalFileDetector()); return new Augmenter().augment(remoteWebDriver); }
private void takeScreenshot(File tempDir) throws IOException { try { driver = new Augmenter().augment(driver); File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); File png = new File(tempDir, "failure" + System.currentTimeMillis() + ".png"); FileUtils.copyFile(srcFile, png); System.err.println("screenshot saved to " + png.getAbsolutePath()); } catch (Exception e) { System.err.println("could not save screen shot, see exception below"); e.printStackTrace(); } }
public <T> T getScreenshotAs(OutputType<T> target) throws WebDriverException { try { TakesScreenshot takesScreenshot = webDriver.getClass().isAnnotationPresent(Augmentable.class) ? (TakesScreenshot) new Augmenter().augment(webDriver) : (TakesScreenshot) webDriver; return takesScreenshot.getScreenshotAs(target); } catch (ClassCastException e) { throw new WebDriverException( "Taking screenshots is not supported by the concrete implementation of WebDriver [" + webDriver.getClass() + "]."); } }
private <T> T takeScreenShotOnBrowser(WebDriver driver, OutputType<T> outType) throws CrawljaxException { if (driver instanceof TakesScreenshot) { T screenshot = ((TakesScreenshot) driver).getScreenshotAs(outType); removeCanvasGeneratedByFirefoxDriverForScreenshots(); return screenshot; } else if (driver instanceof RemoteWebDriver) { WebDriver augmentedWebdriver = new Augmenter().augment(driver); return takeScreenShotOnBrowser(augmentedWebdriver, outType); } else if (driver instanceof WrapsDriver) { return takeScreenShotOnBrowser(((WrapsDriver) driver).getWrappedDriver(), outType); } else { throw new CrawljaxException("Your current WebDriver doesn't support screenshots."); } }
@Override protected WebDriver createWebDriver(final DesiredCapabilities capabilities) { String remoteWebDriverUrl = config.get(WebConstants.REMOTE_WEBDRIVER_URL, ""); Validate.notBlank(remoteWebDriverUrl, "Property '%s' must be set in configuration", WebConstants.REMOTE_WEBDRIVER_URL); URL url; try { url = new URL(remoteWebDriverUrl); } catch (MalformedURLException e) { throw new IllegalArgumentException("Illegal remote web driver hub url: " + remoteWebDriverUrl); } log.info("Starting remote web driver with capabilitiesMap: {}", capabilitiesMap); return new Augmenter().augment(new RemoteWebDriver(url, capabilities)); }
public WebDriver create(WebDriverProperties webDriverProperties) throws IOException { DesiredCapabilities desiredCapabilities = new DesiredCapabilities(webDriverProperties.getDesiredCapabilities()); desiredCapabilities.merge(browserSpecificCapabilities(webDriverProperties)); WebDriver webDriver = null; if (webDriverProperties.getUrl() != null) { RemoteWebDriver remoteDriver = new RemoteWebDriver(webDriverProperties.getUrl(), desiredCapabilities); remoteDriver.setFileDetector(new LocalFileDetector()); webDriver = remoteDriver; } else { String browserName = desiredCapabilities == null ? null : desiredCapabilities.getBrowserName(); if (Strings.isNullOrEmpty(browserName)) browserName = BrowserType.CHROME; webDriver = WebDriverType.typeFor(browserName).create(this, desiredCapabilities); } WindowProperties window = webDriverProperties.getWindow(); if (window != null) { DimensionProperties size = window.getSize(); PointProperties position = window.getPosition(); if (size != null) webDriver.manage().window().setSize(new Dimension(size.getWidth(), size.getHeight())); if (position != null) webDriver.manage().window().setPosition(new Point(position.getX(), position.getY())); if (window.isMaximized()) { webDriver.manage().window().maximize(); } } webDriver = webDriver instanceof TakesScreenshot ? webDriver : new Augmenter().augment(webDriver); for (WebDriverTransformer transformer : driverServices.getWebDriverTranformers()) webDriver = transformer.transform(webDriver); return webDriverProperties.isStateful() ? new StatefulWebDriver(webDriver) : webDriver; }
/*** * Take a screenshot of the browser content. Note that while using RemoteWebDriver sessions (ie: with * Sauce Labs), the screenshot will be a full page of content--not only the visible content where the * page is scrolled (as when using a non-RemoteWebDriver session). * * @param toSaveAs - name of the file to save the picture in (Note: must be PNG) * @throws IOException */ public static void takeScreenshotOfPage(File toSaveAs) throws IOException { WebDriver wd = SessionManager.getInstance().getCurrentSession().getWrappedDriver(); File screenshot; if(!(wd instanceof RemoteWebDriver)) { screenshot = ((TakesScreenshot) wd).getScreenshotAs(OutputType.FILE); } else { Augmenter augmenter = new Augmenter(); screenshot = ((TakesScreenshot) augmenter.augment(wd)).getScreenshotAs(OutputType.FILE); } FileUtils.copyFile(screenshot, toSaveAs); }
@Override public Browser openBrowser() { try { WebDriver driver = new RemoteWebDriver(new URL(gridUrl), this.createCapabilities()); WebDriver augmentedDriver = new Augmenter().augment(driver); return new SeleniumBrowser(augmentedDriver); } catch (Exception ex) { throw new RuntimeException(ex); } }
public void captureScreenshot(String path) throws IOException { WebDriver screenshotDriver = new Augmenter().augment(driver); FileOutputStream out = new FileOutputStream(path); byte[] screenshot = ((TakesScreenshot) screenshotDriver).getScreenshotAs(OutputType.BYTES); out.write(screenshot); out.close(); }
/** * Saves the screenshot to the file. * * @param file * the file where to save the screenshot * @throws IOException * if something goes wrong while saving */ private void saveScreenshotTo(final File file) throws IOException { byte[] bytes = null; try { bytes = ((TakesScreenshot) driver) .getScreenshotAs(OutputType.BYTES); } catch (ClassCastException e) { WebDriver augmentedDriver = new Augmenter().augment(driver); bytes = ((TakesScreenshot) augmentedDriver) .getScreenshotAs(OutputType.BYTES); } Files.write(bytes, file); }
public SeleniumScreenshotRule(WebDriver driver) { if (driver instanceof RemoteWebDriver) { webDriver = new Augmenter().augment(driver); } else { webDriver = driver; } }
/** * 屏幕截屏 */ public void screenShot() { long ts = System.currentTimeMillis(); String dir_name = "screenshot"; // 截图存放目录 if (!(new File(dir_name).isDirectory())) { new File(dir_name).mkdirs(); // 不存在则新建目录 } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String time = sdf.format(new Date()); StackTraceElement stack[] = (new Throwable()).getStackTrace(); String cn = ""; int line = 0; // for循环找到项目中最上层的类文件 for (int i = 0; i < stack.length; i++) { StackTraceElement ste = stack[i]; /* * PROJ_PACKAGE_NAME可自定义。 * 作用:调用栈中找到工程的包名,然后截图,即,避免使用底层类命名截图 */ if (ste.getClassName().contains(PROJ_PACKAGE_NAME)) { cn = ste.getFileName(); line = ste.getLineNumber(); } } LogUtil.debug("PROJ_PACKAGE_NAME:" + PROJ_PACKAGE_NAME); if (cn != "" && !cn.isEmpty()) { cn = cn.substring(0, cn.indexOf(".")); } LogUtil.info("用例:" + cn + "在第" + line + "行失败产生截图:" + time + cn + ".png"); WebDriver augmentedDriver = new Augmenter().augment(driver); File source_file = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(source_file, new File(dir_name + File.separator + time + cn + ".png")); } catch (IOException e) { LogUtil.error("用例:" + cn + "在第" + line + "行失败产生截图失败:" + e); } long te = System.currentTimeMillis(); LogUtil.info("截屏耗时ms: " + (te - ts)); }
public byte[] captureScreenShot() { WebDriver augmentedDriver = new Augmenter().augment(getDriver()); byte[] data = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.BYTES); return data; }
public static byte[] takeScreenshot(WebDriver driver) throws IOException { WebDriver augmentedDriver = new Augmenter().augment(driver); return ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.BYTES); }
/** * Helper method to launch remote web driver. * * At times there are issues with starting the remote web driver. For firefox, * problems with locking port 7054 can arise. * * See the Selenium <a * href="https://code.google.com/p/selenium/issues/detail?id=4790">bug</a> for * more info * * A special case needs to be performed for the Android browser, since it can not be cast to {@link RemoteWebDriver} * * @param capabilities web driver capabilities. * @return {@link WebDriver} instance of the remote web driver * */ protected WebDriver initRemoteWebDriver(DesiredCapabilities capabilities) { URL remoteUrl = getRemoteSeleniumUrl(); LOG.debug("Remote Selenium URL: {}", remoteUrl.toString()); WebDriver driver = null; boolean isAndroid = false; int tries = 1; if(capabilities.getCapability(CapabilityType.BROWSER_NAME).equals(ANDROID_BROWSER_NAME)) { isAndroid = true; } while (driver == null) { LOG.debug("Try {} {}", tries, capabilities.toString()); try { if(isAndroid) { driver = new AndroidDriver(remoteUrl, capabilities); } else { driver = new RemoteWebDriver(remoteUrl, capabilities); } } catch (WebDriverException e) { LOG.error("Remote WebDriver was unable to start! " + e.getMessage(), e); if (tries >= Integer.getInteger(REMOTE_WEBDRIVER_RETRY_ATTEMPTS, 10)) { throw e; } sleep(Integer.getInteger(REMOTE_WEBDRIVER_RETRY_PAUSE_MILLIS, 5000) * tries); tries++; driver = null; } } if(!isAndroid) { // allow screenshots to be taken driver = new Augmenter().augment(driver); } // Allow files from the host to be uploaded to a remote browser ((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector()); return driver; }
/** * Returns a new webdriver instance for the given browser type. */ public WebDriver getDriverWithRetryFromBackupGrid(WTFBrowser browser, ITestResult testResult) { String testName = testResult.getName(); WebDriver driver = null; int reTryMax = 3; while (reTryMax > 0) { driver = null; reTryMax--; try { if (WTFTestConfig.getBackupRemoteWebDriverHubURL() != null) { Capabilities capabilities = getDesiredCapabilities(browser); driver = new RemoteWebDriver(new URL( WTFTestConfig.getBackupRemoteWebDriverHubURL()), capabilities); //BeDashListener.setCapabilityMetrics(capabilities); if (WTFTestConfig.screenShotEnabled()) { driver = new Augmenter().augment(driver); } break; } else { return null; } } catch (Exception e) { LOG(SEVERE, String.format( "%s:: %s driver creation has failed on backup grid, reason: %s, retrying again..", testName, browser.getBrowserName(), e.getMessage())); try { // waits for a 10 seconds before retrying. Thread.sleep(10000); } catch (Exception e1) { e1.printStackTrace(); } } if (reTryMax <= 0) { LOG(SEVERE, "Driver creation has permanently failed on backup grid after multiple " + "retries.."); setTestFailed(testResult, "Unable to connect with webdriver Grid.."); } } return driver; }
@Attachment("Главная страница Twister") @Step("Делаем скриншот") private byte[] takeScreenShot() { return ((TakesScreenshot) new Augmenter().augment(driver)).getScreenshotAs(OutputType.BYTES); }