List of usage examples for org.openqa.selenium WebDriver getCurrentUrl
String getCurrentUrl();
From source file:org.alfresco.po.share.steps.CommonActions.java
License:Open Source License
/** * Checks if the current page is share page, throws PageException if not. * * @param driver WebDriver Instance/*from w w w . j a va 2 s . co m*/ * @return SharePage * @throws PageException if the current page is not a share page */ public SharePage getSharePage(WebDriver driver) { checkIfDriverIsNull(driver); try { HtmlPage generalPage = factoryPage.getPage(driver); return (SharePage) generalPage; } catch (PageException pe) { throw new PageException("Can not cast to SharePage: Current URL: " + driver.getCurrentUrl()); } }
From source file:org.alfresco.po.share.steps.SiteActions.java
License:Open Source License
/** * Method to navigate to site dashboard url, based on siteshorturl, rather than sitename * This is to be used to navigate only as a util, not to test getting to the site dashboard * /* w w w . j a v a 2 s .co m*/ * @param driver * @param siteShortURL * @return {@link org.alfresco.po.share.site.SiteDashboardPage} */ public SiteDashboardPage openSiteURL(WebDriver driver, String siteShortURL) { String url = driver.getCurrentUrl(); String target = url.substring(0, url.indexOf("/page/")) + SITE_DASH_LOCATION_SUFFIX + getSiteShortname(siteShortURL) + "/dashboard"; driver.navigate().to(target); SiteDashboardPage siteDashboardPage = getSharePage(driver).render(); return siteDashboardPage.render(); }
From source file:org.alfresco.po.share.util.SiteUtil.java
License:Open Source License
/** * Deletes site using share/*from w w w . j av a 2s . c o m*/ * * @param siteName String site name * @return true if site deleted */ public boolean deleteSite(WebDriver driver, final String siteName) { if (siteName == null || siteName.isEmpty()) throw new IllegalArgumentException("site name is required"); try { // Alfresco.cloud.constants.CURRENT_TENANT String url = driver.getCurrentUrl(); String target = url.replaceFirst("^*/page.*", SITE_FINDER_LOCATION_SUFFIX); driver.navigate().to(target); int count = 0; while (count < 5) { if (target.equalsIgnoreCase(driver.getCurrentUrl())) { break; } count++; } SharePage page = factoryPage.getPage(driver).render(); SiteFinderPage siteFinder = page.getNav().selectSearchForSites().render(); siteFinder = siteSearchRetry(driver, siteFinder, siteName); if (siteFinder.hasResults()) { siteFinder = siteFinder.deleteSite(siteName).render(); return !siteFinder.hasResults(); } } catch (UnsupportedOperationException une) { String msg = String.format(ERROR_MESSAGE_PATTERN, siteName); throw new RuntimeException(msg, une); } catch (PageException e) { } return false; }
From source file:org.alfresco.selenium.FetchUtil.java
License:Open Source License
/** * Saves the current page as seen by the WebDriver * @param driver {@link WebDriver}/* www . ja va2 s .c o m*/ * @param filename name of the HTML file output * @throws PageCaptureException if error * @throws IOException */ public static void save(final WebDriver driver, final String filename) throws IOException { String sourceHtml = driver.getPageSource(); String currentUrl = driver.getCurrentUrl(); //download all assets: js,img and stylesheet. List<String> files = extractFiles(sourceHtml); List<String> urls = parseURL(files, getHost(driver), currentUrl); getFiles(urls, driver); String html = parseHtml(sourceHtml, files); File file = new File(OUTPUT_DIR + filename); file.delete(); FileUtils.writeStringToFile(file, html); }
From source file:org.apache.nutch.protocol.htmlunit.HtmlUnitWebDriver.java
License:Apache License
private static void takeScreenshot(WebDriver driver, Configuration conf) { try {//from w w w. ja v a 2 s . c om String url = driver.getCurrentUrl(); File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); LOG.debug("In-memory screenshot taken of: {}", url); FileSystem fs = FileSystem.get(conf); if (conf.get("screenshot.location") != null) { Path screenshotPath = new Path(conf.get("screenshot.location") + "/" + srcFile.getName()); OutputStream os = null; if (!fs.exists(screenshotPath)) { LOG.debug("No existing screenshot already exists... creating new file at {} {}.", screenshotPath, srcFile.getName()); os = fs.create(screenshotPath); } InputStream is = new BufferedInputStream(new FileInputStream(srcFile)); IOUtils.copyBytes(is, os, conf); LOG.debug("Screenshot for {} successfully saved to: {} {}", url, screenshotPath, srcFile.getName()); } else { LOG.warn("Screenshot for {} not saved to HDFS (subsequently disgarded) as value for " + "'screenshot.location' is absent from nutch-site.xml.", url); } } catch (Exception e) { cleanUpDriver(driver); throw new RuntimeException(e); } }
From source file:org.apache.nutch.protocol.interactiveselenium.BangGoodHandler.java
License:Apache License
@Override public String processDriver(WebDriver driver) { StringBuffer buffer = new StringBuffer(); // Extract content String content = driver.findElement(By.tagName("body")).getText(); buffer.append(content).append("\n"); WebElement pages = driver.findElement(By.xpath("//div[@class='page_num']")); if (pages == null) { return buffer.toString(); }/*from w w w . ja v a 2s. c o m*/ int lastPageNum = 1; try { lastPageNum = Integer .parseInt(pages.findElement(By.xpath("./b[text()='...']/following-sibing::a")).getText()); } catch (NumberFormatException e) { // do nothing } String baseUrl = FilenameUtils.removeExtension(driver.getCurrentUrl()); for (int i = 2; i <= lastPageNum; i++) { buffer.append("<a href=\"").append(baseUrl).append("-0-1-1-45-0_page").append(i).append(".html\" />\n"); } return buffer.toString(); }
From source file:org.apache.nutch.protocol.interactiveselenium.DigikeyHandler.java
License:Apache License
@Override public String processDriver(WebDriver driver) { StringBuffer buffer = new StringBuffer(); // Extract content String content = driver.findElement(By.tagName("body")).getText(); buffer.append(content).append("\n"); WebElement pages = driver.findElement(By.xpath("//div[@class='paging-inner']")); if (pages == null) { return buffer.toString(); }// w w w . j av a 2 s . co m String lastPage = pages.findElement(By.xpath(".//a[@class='Last']")).getAttribute("href"); int lastPageNum = 1; try { lastPageNum = Integer.parseInt(lastPage.substring(lastPage.lastIndexOf('/') + 1)); } catch (NumberFormatException e) { // do nothing } for (int i = 2; i <= lastPageNum; i++) { buffer.append("<a href=\"").append(driver.getCurrentUrl()).append("/page/").append(i).append("\" />\n"); } return buffer.toString(); }
From source file:org.apache.nutch.protocol.interactiveselenium.DXHandler.java
License:Apache License
@Override public String processDriver(WebDriver driver) { StringBuffer buffer = new StringBuffer(); // Extract content String content = driver.findElement(By.tagName("body")).getText(); buffer.append(content).append("\n"); WebElement pages = driver.findElement(By.xpath("//div[@class='page_wrapper']/div[@class='paging-range']")); if (pages == null) { return buffer.toString(); }// w w w .j av a2 s .c om int lastPage = Integer.parseInt(pages.findElement(By.xpath(".//span[@class='pageCount']")).getText()); for (int i = 2; i <= lastPage; i++) { buffer.append("<a href=\"").append(driver.getCurrentUrl()).append("?page=").append(i).append("\" />\n"); } return buffer.toString(); }
From source file:org.apache.nutch.protocol.interactiveselenium.ListImageHandler.java
License:Apache License
public void processDriver(WebDriver driver) { System.out.println("=========== Into Handler_vci_classifieds =========="); //Get the current page URL and store the value in variable 'url' String url = driver.getCurrentUrl(); //Print the value of variable in the console System.out.println("[ListImageHandler][processDriver] The current URL is: " + url); //Load a new page in the current browser windows driver.get(url);//from www.j a v a 2 s . c o m // form-input structure for search bar // WebElement form = driver.findElement(By.tagName("form")); // List<WebElement> inputs = null; // if (form != null) { // inputs = form.findElements(By.tagName("input")); // for (WebElement elem : inputs) { // if (elem.isDisplayed()) { // elem.clear(); // elem.sendKeys("gun\n"); // System.out.println("[ListImageHandler][processDriver] Submit keyword \"gun\""); // //form.submit(); // break; // } // } // } // direct input html tag for search bar ArrayList<String> possibleName = new ArrayList<String>(); possibleName.add("q"); possibleName.add("searchtext"); possibleName.add("txtSearch"); WebElement element; if (inputs == null) { for (int i = 0; i < possibleName.size(); i++) { element = driver.findElement(By.name(possibleName.get(i))); if (element != null) { // send with "\n" == submit element.sendKeys("gun\n"); //element.sendKeys("gun"); //form.submit(); System.out.println("[ListImageHandler][processDriver] Submit keyword \"gun\""); break; } } } // wait for finsih loading webpage, hard code timer try { System.out.println("[ListImageHandler][processDriver] before sleep"); Thread.sleep(2000); System.out.println("[ListImageHandler][processDriver] after sleep"); } catch (Exception e) { System.out.println("[ListImageHandler][processDriver] Exception caught"); } // find all image element List<WebElement> findElements = driver.findElements(By.xpath("//img")); if (url.equals("http://www.vci-classifieds.com/")) { WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("searchlist"))); findElements = driver.findElements(By.xpath(".//*[@id='searchlist']/center/table/tbody/tr/td/img")); } System.out.println("[ListImageHandler][processDriver] total Results Found: " + findElements.size()); // show all the links of image for (WebElement elem : findElements) { System.out.println(elem.getAttribute("src")); // System.out.println(elem.toString()); } System.out.println("[ListImageHandler][processDriver] " + findElements.size() + " results shown"); // log outlinks to /tmp/outlinks List<WebElement> findOutlinks = driver.findElements(By.xpath("//a")); try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("/tmp/outlinks", true)))) { // this are all the links you like to visit int count = 0; for (WebElement elem : findOutlinks) { out.println(elem.getAttribute("href")); count++; } System.out.println("[ListImageHandler][processDriver] total Outlinks Logged: " + count); } catch (IOException e) { System.err.println(e); } // driver.close(); // System.out.println("[ListImageHandler][processDriver] Close Driver"); }
From source file:org.apache.nutch.protocol.interactiveselenium.handlers.LoginHandler.java
License:Apache License
/** * Invokes a registered domain handler for handling the web page * @param driver selenium web driver instance *//*www. j a v a2 s. co m*/ @Override public String processDriver(WebDriver driver) { String domainName = driver.getCurrentUrl(); String accumulatedData = ""; if (shouldProcessURL(domainName)) { accumulatedData += domainHandlers.get(domainName).handle(driver, accumulatedData); } // // TODO: 4/17/16 change return type of parent method to generic type and return void return accumulatedData; }