Example usage for org.openqa.selenium.firefox FirefoxDriver findElements

List of usage examples for org.openqa.selenium.firefox FirefoxDriver findElements

Introduction

In this page you can find the example usage for org.openqa.selenium.firefox FirefoxDriver findElements.

Prototype

@Override
    public List<WebElement> findElements(By by) 

Source Link

Usage

From source file:com.vaadin.testbench.TestBenchDriverTest.java

@Test
public void testTestBenchDriverActsAsProxy() {
    FirefoxDriver mockDriver = createMock(FirefoxDriver.class);
    mockDriver.close();//w  ww . jav a  2 s  . c  om
    expectLastCall().once();
    WebElement mockElement = createNiceMock(WebElement.class);
    expect(mockDriver.findElement(isA(By.class))).andReturn(mockElement);
    List<WebElement> elements = Arrays.asList(mockElement);
    expect(mockDriver.findElements(isA(By.class))).andReturn(elements);
    mockDriver.get("foo");
    expectLastCall().once();
    expect(mockDriver.getCurrentUrl()).andReturn("foo");
    expect(mockDriver.getPageSource()).andReturn("<html></html>");
    expect(mockDriver.getTitle()).andReturn("bar");
    expect(mockDriver.getWindowHandle()).andReturn("baz");
    Set<String> handles = new HashSet<String>();
    expect(mockDriver.getWindowHandles()).andReturn(handles);
    Options mockOptions = createNiceMock(Options.class);
    expect(mockDriver.manage()).andReturn(mockOptions);
    Navigation mockNavigation = createNiceMock(Navigation.class);
    expect(mockDriver.navigate()).andReturn(mockNavigation);
    mockDriver.quit();
    expectLastCall().once();
    expect(((JavascriptExecutor) mockDriver).executeScript(anyObject(String.class))).andStubReturn(true);
    TargetLocator mockTargetLocator = createNiceMock(TargetLocator.class);
    expect(mockDriver.switchTo()).andReturn(mockTargetLocator);
    replay(mockDriver);

    // TestBenchDriverProxy driver = new TestBenchDriverProxy(mockDriver);
    WebDriver driver = TestBench.createDriver(mockDriver);
    driver.close();
    By mockBy = createNiceMock(By.class);
    assertTrue(driver.findElement(mockBy) instanceof TestBenchElementCommands);
    assertTrue(driver.findElements(mockBy).get(0) instanceof TestBenchElementCommands);
    driver.get("foo");
    assertEquals("foo", driver.getCurrentUrl());
    assertEquals("<html></html>", driver.getPageSource());
    assertEquals("bar", driver.getTitle());
    assertEquals("baz", driver.getWindowHandle());
    assertEquals(handles, driver.getWindowHandles());
    assertEquals(mockOptions, driver.manage());
    assertEquals(mockNavigation, driver.navigate());
    driver.quit();
    assertEquals(mockTargetLocator, driver.switchTo());

    verify(mockDriver);
}

From source file:org.ado.picasa.Main.java

License:Apache License

public static void main(String[] args) throws Exception {
    final Options options = new Options();
    options.addOption(new Option("a", "album", true, "Album name"));
    options.addOption("v", "verification-code", true, "Verification code");
    options.addOption("o", "output", true, "Albums output directory");
    options.addOption("h", "help", false, "Prints this help");
    final CommandLine cmd = new DefaultParser().parse(options, args);

    if (cmd.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("picasa-crawler", options);
        System.exit(0);/*from www  .j  av  a2s  .  c om*/
    }

    validateEnvironmentVariables();
    final File outputDirectory;
    if (cmd.hasOption("o")) {
        outputDirectory = new File(cmd.getOptionValue("o"));
    } else {
        outputDirectory = DEFAULT_OUTPUT_DIR;
    }
    FileUtils.forceMkdir(outputDirectory);
    long start = System.currentTimeMillis();

    final FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.dir", outputDirectory.getAbsolutePath());
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg,image/png");
    final FirefoxDriver driver = new FirefoxDriver(profile);
    loginIntoPicasa(cmd.getOptionValue("v"), driver);

    driver.navigate().to("https://picasaweb.google.com/home?showall=true");
    TimeUnit.SECONDS.sleep(2);

    final List<WebElement> albumLinks = driver
            .findElements(By.xpath("//p[@class='gphoto-album-cover-title']/a"));

    if (cmd.hasOption("a")) {
        final String albumName = cmd.getOptionValue("a").trim();
        System.out.println(String.format("Album: %s", albumName));
        downloadAlbum(driver, albumLinks.stream().filter(al -> al.getText().equals(albumName)).findFirst().get()
                .getAttribute("href"), outputDirectory);

    } else {

        final Set<String> albumHrefs = albumLinks.stream().map(a -> a.getAttribute("href"))
                .collect(Collectors.toSet());

        albumHrefs.forEach(a -> downloadAlbum(driver, a, outputDirectory));

    }
    TimeUnit.SECONDS.sleep(10);
    System.out.println("done");
    long end = System.currentTimeMillis();
    System.out.println(String.format("execution took %d minutes.",
            Math.min(TimeUnit.MILLISECONDS.toMinutes(end - start), 1)));
    driver.close();
}

From source file:org.ado.picasa.Main.java

License:Apache License

private static void downloadAlbum(FirefoxDriver driver, String album, File outputDir) {
    try {/*from  w  ww.j a v  a2 s .co m*/
        final String albumName = getAlbumName(album);
        System.out.println(String.format("> album name: %s  url: %s", albumName, album));
        driver.navigate().to(album);

        final List<String> photoHrefLinks = driver.findElements(By.xpath("//div[@class='goog-icon-list']//a"))
                .stream().filter(a -> MediaType.PHOTO.equals(getMediaType(a))).map(a -> a.getAttribute("href"))
                .collect(Collectors.toList());

        for (String href : photoHrefLinks) {
            downloadPhoto(driver, href);
        }

        driver.navigate().to(album);

        final List<String> videoHrefLinks = driver.findElements(By.xpath("//div[@class='goog-icon-list']//a"))
                .stream().filter(a -> MediaType.VIDEO.equals(getMediaType(a))).map(a -> a.getAttribute("href"))
                .collect(Collectors.toList());

        int index = 1;
        final FileDownloader fileDownloader = new FileDownloader(driver, outputDir.getAbsolutePath());
        for (String videoUrl : getVideoUrls(driver, videoHrefLinks, album)) {
            try {
                new FileHandler(fileDownloader.downloader(videoUrl, albumName + index++ + ".m4v"));
            } catch (Exception e) {
                System.err.println(String.format("Cannot download video '%s'.", videoUrl));
                e.printStackTrace();
            }
        }

        TimeUnit.SECONDS.sleep(10);
        System.out.println(String.format("moving photos to directory: %s", albumName));
        final File albumDirectory = new File(outputDir, albumName);
        for (File file : FileUtils.listFiles(outputDir, null, false)) {
            try {
                FileUtils.moveFileToDirectory(file, albumDirectory, true);
            } catch (IOException e) {
                FileUtils.moveFile(file,
                        new File(albumDirectory, file.getName() + "-" + System.currentTimeMillis()));
            }
        }
    } catch (Exception e) {
        System.err.println(String.format("Cannot download album '%s'", album));
        e.printStackTrace();
    }
}