List of usage examples for org.openqa.selenium.remote RemoteWebDriver getPageSource
@Override
public String getPageSource()
From source file:com.digitalpebble.stormcrawler.protocol.selenium.SeleniumProtocol.java
License:Apache License
public ProtocolResponse getProtocolOutput(String url, Metadata metadata) throws Exception { // TODO check that the driver is not null RemoteWebDriver driver = getDriver(); try {//from w w w . ja va2 s . c om // This will block for the page load and any // associated AJAX requests driver.get(url); // call the filters ProtocolResponse response = filters.filter(driver, metadata); if (response == null) { // if no filters got triggered byte[] content = driver.getPageSource().getBytes(); response = new ProtocolResponse(content, 200, metadata); } return response; } finally { // finished with this driver - return it to the queue drivers.put(driver); } }
From source file:com.mycompany.myproject.sample.simple.DemoWebTest.java
License:Apache License
@Test public void seleniumDemo() throws Exception { // Hard coded to firefox DesiredCapabilities caps = DesiredCapabilities.firefox(); // Hard coded selenium host RemoteWebDriver driver = new RemoteWebDriver(new URL("http://192.168.99.100:4444/wd/hub"), caps); driver.get("http://google.com"); String source = driver.getPageSource(); File file = driver.getScreenshotAs(OutputType.FILE); //TODO log the screenshot and source to a report // don't forget to close the WebDriver connection driver.quit();/* w w w . ja va 2s . c om*/ }
From source file:com.screenslicer.core.scrape.Proceed.java
License:Open Source License
public static String perform(RemoteWebDriver driver, int pageNum, String priorTextLabel) throws End, ActionFailed { try {//from w ww . ja v a 2 s. c o m Element body = Util.openElement(driver, null, null, null); String origSrc = driver.getPageSource(); String origTitle = driver.getTitle(); String origUrl = driver.getCurrentUrl(); Context context = perform(body, pageNum, priorTextLabel); if (context != null && context.node != null) { WebElement element = Util.toElement(driver, context.node); if (element != null) { boolean success = Util.click(driver, element); if (success) { Util.driverSleepLong(); String newSource = driver.getPageSource(); String newTitle = driver.getTitle(); String newUrl = driver.getCurrentUrl(); if (origSrc.hashCode() != newSource.hashCode() || !origTitle.equals(newTitle) || !origUrl.equals(newUrl)) { return context.textLabel; } } } } } catch (Throwable t) { Log.exception(t); } throw new End(); }
From source file:com.screenslicer.core.scrape.QueryKeyword.java
License:Open Source License
private static String doSearch(RemoteWebDriver driver, List<WebElement> searchBoxes, String searchQuery) throws ActionFailed { try {//from ww w . j av a 2 s . c o m for (WebElement element : searchBoxes) { try { Util.click(driver, element); element.clear(); Util.driverSleepVeryShort(); if (!CommonUtil.isEmpty(element.getAttribute("value"))) { element.sendKeys(delete); Util.driverSleepVeryShort(); } element.sendKeys(searchQuery); Util.driverSleepVeryShort(); String beforeSource = driver.getPageSource(); String beforeTitle = driver.getTitle(); String beforeUrl = driver.getCurrentUrl(); String windowHandle = driver.getWindowHandle(); element.sendKeys("\n"); Util.driverSleepLong(); Util.cleanUpNewWindows(driver, windowHandle); String afterSource = driver.getPageSource(); String afterTitle = driver.getTitle(); String afterUrl = driver.getCurrentUrl(); if (!beforeTitle.equals(afterTitle) || !beforeUrl.equals(afterUrl) || Math.abs(beforeSource.length() - afterSource.length()) > MIN_SOURCE_DIFF) { handleIframe(driver); return driver.getPageSource(); } } catch (Throwable t) { Log.exception(t); } } } catch (Throwable t) { Log.exception(t); } throw new ActionFailed(); }
From source file:com.screenslicer.core.scrape.Scrape.java
License:Open Source License
private static String getHelper(final RemoteWebDriver driver, final Node urlNode, final String url, final boolean p_cached, final String runGuid, final HtmlNode[] clicks) { final String urlHash = CommonUtil.isEmpty(url) ? null : Crypto.fastHash(url); final long time = System.currentTimeMillis(); if (urlHash != null) { synchronized (fetchLocalCacheLock) { if (fetchLocalCache.containsKey(urlHash)) { if (time - fetchLocalCache.get(urlHash) < FETCH_LOCAL_CACHE_EXPIRES) { try { return FileUtils.readFileToString(new File("./fetch_local_cache/" + urlHash), "utf-8"); } catch (Throwable t) { Log.exception(t); fetchLocalCache.remove(urlHash); }/*from ww w . j a v a 2 s . c o m*/ } else { fetchLocalCache.remove(urlHash); } } } } if (!CommonUtil.isEmpty(url)) { final Object resultLock = new Object(); final String initVal; final String[] result; synchronized (resultLock) { initVal = Random.next(); result = new String[] { initVal }; } final AtomicBoolean started = new AtomicBoolean(); Thread thread = new Thread(new Runnable() { @Override public void run() { started.set(true); boolean cached = p_cached; String newHandle = null; String origHandle = null; try { origHandle = driver.getWindowHandle(); String content = null; if (!cached) { try { Util.get(driver, url, urlNode, false); } catch (Throwable t) { if (urlNode != null) { Util.newWindow(driver); } Util.get(driver, url, false); } if (urlNode != null) { newHandle = driver.getWindowHandle(); } Util.doClicks(driver, clicks, null); content = driver.getPageSource(); if (CommonUtil.isEmpty(content)) { cached = true; } } if (cached) { if (ScreenSlicerBatch.isCancelled(runGuid)) { return; } Util.get(driver, toCacheUrl(url), false); content = driver.getPageSource(); } content = Util.clean(content, driver.getCurrentUrl()).outerHtml(); if (WebApp.DEBUG) { try { FileUtils.writeStringToFile(new File("./" + System.currentTimeMillis()), content); } catch (IOException e) { } } //TODO make iframes work // if (!CommonUtil.isEmpty(content)) { // Document doc = Jsoup.parse(content); // Elements docFrames = doc.getElementsByTag("iframe"); // List<WebElement> iframes = driver.findElementsByTagName("iframe"); // int cur = 0; // for (WebElement iframe : iframes) { // try { // driver.switchTo().frame(iframe); // String frameSrc = driver.getPageSource(); // if (!CommonUtil.isEmpty(frameSrc) && cur < docFrames.size()) { // docFrames.get(cur).html( // Util.outerHtml(Jsoup.parse(frameSrc).body().childNodes())); // } // } catch (Throwable t) { // Log.exception(t); // } // ++cur; // } // driver.switchTo().defaultContent(); // content = doc.outerHtml(); // } synchronized (resultLock) { result[0] = content; } } catch (Throwable t) { Log.exception(t); } finally { synchronized (resultLock) { if (initVal.equals(result[0])) { result[0] = null; } } Util.driverSleepRandLong(); if (newHandle != null && origHandle != null) { try { Util.cleanUpNewWindows(driver, origHandle); } catch (Throwable t) { Log.exception(t); } } } } }); thread.start(); try { while (!started.get()) { try { Thread.sleep(WAIT); } catch (Throwable t) { Log.exception(t); } } thread.join(HANG_TIME); synchronized (resultLock) { if (initVal.equals(result[0])) { try { Log.exception(new Exception("Browser is hanging")); forceQuit(); thread.interrupt(); } catch (Throwable t) { Log.exception(t); } throw new ActionFailed(); } else if (urlHash != null && !CommonUtil.isEmpty(result[0]) && result[0].length() > MIN_FETCH_CACHE_PAGE_LEN) { synchronized (fetchLocalCacheLock) { if (fetchLocalCache.size() > MAX_FETCH_LOCAL_CACHE) { try { FileUtils.deleteQuietly(new File("./fetch_local_cache")); FileUtils.forceMkdir(new File("./fetch_local_cache")); } catch (Throwable t) { Log.exception(t); } fetchLocalCache.clear(); } FileUtils.writeStringToFile(new File("./fetch_local_cache/" + urlHash), result[0], "utf-8", false); fetchLocalCache.put(urlHash, time); } } return result[0]; } } catch (Throwable t) { Log.exception(t); } } return null; }
From source file:com.screenslicer.core.service.HttpStatus.java
License:Open Source License
public static int status(RemoteWebDriver driver, boolean wait) { if (WebApp.DEBUG) { System.out.println("check status..."); }/*from w w w .j a va 2 s . c o m*/ int totalWait = 0; String src = null; while (totalWait < TIMEOUT) { src = null; try { src = driver.getPageSource(); if (!CommonUtil.isEmpty(src)) { synchronized (lock) { prevLen = len; len = src.length(); } } } catch (Throwable t) { synchronized (lock) { src = null; prevLen = 0; len = 0; } Log.exception(t); } if (WebApp.DEBUG) { synchronized (lock) { System.out.println("status=" + status); } } synchronized (lock) { if (status != 0) { break; } } boolean validLen; synchronized (lock) { validLen = prevLen == len && len > MIN_LEN; } if (validLen && hasContent(null, src)) { try { driver.getKeyboard().sendKeys(Keys.ESCAPE); } catch (Throwable t) { Log.exception(t); } Util.driverSleepVeryShort(); if (!hasContent(driver, src)) { synchronized (lock) { status = 204; } } break; } if (wait) { if (WebApp.DEBUG) { System.out.println("waiting for status..."); } try { Thread.sleep(SLEEP); } catch (InterruptedException e) { Log.exception(e); } totalWait += SLEEP; } else { break; } } int myStatus; synchronized (lock) { myStatus = status; status = -1; } return myStatus; }
From source file:com.screenslicer.core.util.Util.java
License:Open Source License
public static void get(RemoteWebDriver driver, String url, Node urlNode, boolean retry) throws ActionFailed { boolean exception = true; boolean success = true; String origHandle = null;//ww w. ja v a2s. c o m try { String source = null; boolean badUrl = true; boolean statusFail = true; if (urlNode != null) { origHandle = driver.getWindowHandle(); } for (int i = 0; i < REFRESH_TRIES && (badUrl || statusFail || exception || CommonUtil.isEmpty(source)); i++) { badUrl = false; statusFail = false; exception = false; source = null; if (WebApp.DEBUG) { System.out.println("getting url..."); } try { driver.getKeyboard().sendKeys(Keys.ESCAPE); } catch (Throwable t) { Log.exception(t); } if (urlNode != null) { try { cleanUpNewWindows(driver, origHandle); } catch (Throwable t) { Log.exception(t); } } try { Util.driverSleepVeryShort(); if (urlNode != null) { try { Util.clickToNewWindow(driver, toElement(driver, urlNode)); Set<String> newHandles = driver.getWindowHandles(); for (String newHandle : newHandles) { if (!origHandle.equals(newHandle)) { driver.switchTo().window(newHandle); break; } } } catch (Throwable t) { exception = true; Log.exception(t); Util.cleanUpNewWindows(driver, origHandle); } } else { driver.get("about:blank"); try { driver.get(url); } catch (TimeoutException e) { Log.exception(e); } } if (!exception) { Util.driverSleepShort(); Util.driverSleepLong(); statusFail = HttpStatus.status(driver, urlNode != null) != 200; driver.switchTo().defaultContent(); source = driver.getPageSource(); try { new URL(driver.getCurrentUrl()); badUrl = false; } catch (Throwable t) { badUrl = true; } } } catch (Throwable t) { Log.exception(t); exception = true; } if ((!retry || i + 1 == REFRESH_TRIES) && (badUrl || statusFail || exception || CommonUtil.isEmpty(source))) { try { driver.getKeyboard().sendKeys(Keys.ESCAPE); Util.driverSleepVeryShort(); } catch (Throwable t) { Log.exception(t); } success = false; if (!retry) { break; } } } if (WebApp.DEBUG) { System.out.println("getting url - done"); } } catch (Throwable t) { Log.exception(t); success = false; } if (!success) { if (urlNode != null && origHandle != null) { try { cleanUpNewWindows(driver, origHandle); } catch (Throwable t) { Log.exception(t); } } throw new ActionFailed(); } }
From source file:com.screenslicer.core.util.Util.java
License:Open Source License
public static Element openElement(final RemoteWebDriver driver, final String[] whitelist, final String[] patterns, final UrlTransform[] transforms) throws ActionFailed { try {//www . j a v a2s . c o m driver.executeScript(" var all = document.getElementsByTagName('*');" + "for(var i = 0; i < all.length; i++){" + " if(all[i].className){" + " all[i].className=all[i].className.replace(/" + NODE_MARKER + "\\d+/g,'').replace(/" + HIDDEN_MARKER + "/g,'').replace(/" + FILTERED_MARKER + "/g,'').replace(/" + FILTERED_LENIENT_MARKER + "/g,'').replace(/\\s+/g,' ').trim();" + " }" + "}" + isVisible + "for(var j = 0; j < all.length; j++){" + " all[j].className += ' " + NODE_MARKER + "'+j+' ';" + " if(!isVisible(all[j])){" + " all[j].className += ' " + HIDDEN_MARKER + " ';" + " }" + "}"); String url = driver.getCurrentUrl(); new URL(url); Element element = parse(driver.getPageSource(), url).body(); element.traverse(new NodeVisitor() { @Override public void tail(Node node, int depth) { } @Override public void head(Node node, int depth) { if (!node.nodeName().equals("#text") && !isEmpty(node)) { markVisible(node); } } }); if ((whitelist != null && whitelist.length > 0) || (patterns != null && patterns.length > 0)) { element.traverse(new NodeVisitor() { @Override public void tail(Node node, int depth) { } @Override public void head(Node node, int depth) { if (node.nodeName().equals("a")) { if (isUrlFiltered(driver.getCurrentUrl(), node.attr("href"), whitelist, patterns, transforms)) { markFiltered(node, false); } } else { String urlAttr = Util.urlFromAttr(node); if (!CommonUtil.isEmpty(urlAttr) && isUrlFiltered(driver.getCurrentUrl(), urlAttr, whitelist, patterns, transforms)) { markFiltered(node, true); } } } }); } return element; } catch (Exception e) { Log.exception(e); throw new ActionFailed(e); } }
From source file:org.miloss.fgsms.smoke.post.UITest.java
License:Mozilla Public License
@Test public void hitAllJspFiles() throws Exception { Assume.assumeFalse(drivers.isEmpty()); for (int i = 0; i < drivers.size(); i++) { int success = 0; int failure = 0; RemoteWebDriver driver = drivers.get(i); for (int k = 0; k < jspRelativePaths.size(); k++) { //filters csrf String target = url + jspRelativePaths.get(k); System.out.println("nav to " + target); driver.navigate().to(target); System.out.println(driver.getTitle()); if (driver.getTitle().toLowerCase().contains("error report")) { // Assert.fail(target + " failed with " + driver.getTitle() + " body " // + driver.getPageSource()); System.err.println( target + " failed with " + driver.getTitle() + " body " + driver.getPageSource()); failure++;/* w ww . j ava 2s.c om*/ } else success++; } System.out.println("success: " + success + ", failures: " + failure); Assert.assertEquals("there were some failing test cases", 0, failure); } }