io.github.bonigarcia.wdm.PhantomJsDriverManager.java Source code

Java tutorial

Introduction

Here is the source code for io.github.bonigarcia.wdm.PhantomJsDriverManager.java

Source

/*
 * (C) Copyright 2016 Boni Garcia (http://bonigarcia.github.io/)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 */
package io.github.bonigarcia.wdm;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

/**
 * Manager for PhantomJs.
 *
 * @author Boni Garcia (boni.gg@gmail.com)
 * @since 1.4.0
 */
public class PhantomJsDriverManager extends BrowserManager {

    private static PhantomJsDriverManager instance;

    public PhantomJsDriverManager() {
    }

    public static synchronized PhantomJsDriverManager getInstance() {
        if (instance == null) {
            instance = new PhantomJsDriverManager();
        }
        return instance;
    }

    @Override
    public List<URL> getDrivers() throws Exception {
        String phantomjsDriverUrl = WdmConfig.getString("wdm.phantomjsDriverUrl");
        log.debug("Reading {} to find out the latest version of PhantomJS driver", phantomjsDriverUrl);

        // Switch off HtmlUnit logging
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
                "org.apache.commons.logging.impl.NoOpLog");
        java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
        java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);

        // Using HtmlUnitDriver to read package URL
        WebDriver driver = new HtmlUnitDriver();
        driver.manage().timeouts().implicitlyWait(WdmConfig.getInt("wdm.timeout"), TimeUnit.SECONDS);
        driver.get(phantomjsDriverUrl);
        WebElement downloadsTable = driver.findElement(By.id("available-downloads"));
        List<WebElement> links = downloadsTable.findElements(By
                .xpath("//table[@id='uploaded-files']/tbody/tr[@class='iterable-item']/td[@class='name']" + "/a"));
        List<URL> urlList = new ArrayList<>(links.size());
        for (WebElement element : links) {
            String href = element.getAttribute("href");
            urlList.add(new URL(href));
        }
        return urlList;
    }

    @Override
    protected String getExportParameter() {
        return WdmConfig.getString("wdm.phantomjsDriverExport");
    }

    @Override
    protected String getDriverVersion() {
        return WdmConfig.getString("wdm.phantomjsDriverVersion");
    }

    @Override
    protected URL getDriverUrl() throws MalformedURLException {
        return WdmConfig.getUrl("wdm.phantomjsDriverUrl");
    }

    @Override
    protected List<String> getDriverName() {
        return Arrays.asList("phantomjs");
    }
}