//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.openqa.selenium.firefox;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.Timeouts;
import org.openqa.selenium.firefox.ExtensionConnection;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.NewProfileExtensionConnection;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.internal.Killable;
import org.openqa.selenium.internal.Lock;
import org.openqa.selenium.internal.SocketLock;
import org.openqa.selenium.logging.LocalLogs;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.logging.NeedsLocalLogs;
import org.openqa.selenium.remote.BeanToJsonConverter;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.Response;
import org.openqa.selenium.remote.SessionNotFoundException;
import org.openqa.selenium.remote.RemoteWebDriver.RemoteWebDriverOptions;
import org.openqa.selenium.remote.RemoteWebDriver.RemoteWebDriverOptions.RemoteTimeouts;

public class FirefoxDriver extends RemoteWebDriver implements Killable {
public static final String BINARY = "firefox_binary";
public static final String PROFILE = "firefox_profile";
public static final boolean DEFAULT_ENABLE_NATIVE_EVENTS;
/** @deprecated */
@Deprecated
public static final boolean ACCEPT_UNTRUSTED_CERTIFICATES = true;
/** @deprecated */
@Deprecated
public static final boolean ASSUME_UNTRUSTED_ISSUER = true;
protected FirefoxBinary binary;

public FirefoxDriver() {
    this((FirefoxBinary)(new FirefoxBinary()), (FirefoxProfile)null);
}

public FirefoxDriver(FirefoxProfile profile) {
    this(new FirefoxBinary(), profile);
}

public FirefoxDriver(Capabilities desiredCapabilities) {
    this(getBinary(desiredCapabilities), extractProfile(desiredCapabilities, (Capabilities)null), desiredCapabilities);
}

public FirefoxDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
    this(getBinary(desiredCapabilities), extractProfile(desiredCapabilities, requiredCapabilities), desiredCapabilities, requiredCapabilities);
}

private static FirefoxProfile extractProfile(Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
    FirefoxProfile profile = null;
    Object raw = null;
    if(desiredCapabilities != null && desiredCapabilities.getCapability("firefox_profile") != null) {
        raw = desiredCapabilities.getCapability("firefox_profile");
    }

    if(requiredCapabilities != null && requiredCapabilities.getCapability("firefox_profile") != null) {
        raw = requiredCapabilities.getCapability("firefox_profile");
    }

    if(raw != null) {
        if(raw instanceof FirefoxProfile) {
            profile = (FirefoxProfile)raw;
        } else if(raw instanceof String) {
            try {
                profile = FirefoxProfile.fromJson((String)raw);
            } catch (IOException var5) {
                throw new WebDriverException(var5);
            }
        }
    }

    profile = getProfile(profile);
    populateProfile(profile, desiredCapabilities);
    populateProfile(profile, requiredCapabilities);
    return profile;
}

static void populateProfile(FirefoxProfile profile, Capabilities capabilities) {
    if(capabilities != null) {
        Boolean nativeEventsEnabled;
        if(capabilities.getCapability("webStorageEnabled") != null) {
            nativeEventsEnabled = (Boolean)capabilities.getCapability("webStorageEnabled");
            profile.setPreference("dom.storage.enabled", nativeEventsEnabled.booleanValue());
        }

        if(capabilities.getCapability("acceptSslCerts") != null) {
            nativeEventsEnabled = (Boolean)capabilities.getCapability("acceptSslCerts");
            profile.setAcceptUntrustedCertificates(nativeEventsEnabled.booleanValue());
        }

        if(capabilities.getCapability("loggingPrefs") != null) {
            LoggingPreferences nativeEventsEnabled1 = (LoggingPreferences)capabilities.getCapability("loggingPrefs");
            Iterator var3 = nativeEventsEnabled1.getEnabledLogTypes().iterator();

            while(var3.hasNext()) {
                String logtype = (String)var3.next();
                profile.setPreference("webdriver.log." + logtype, nativeEventsEnabled1.getLevel(logtype).intValue());
            }
        }

        if(capabilities.getCapability("nativeEvents") != null) {
            nativeEventsEnabled = (Boolean)capabilities.getCapability("nativeEvents");
            profile.setEnableNativeEvents(nativeEventsEnabled.booleanValue());
        }

    }
}

private static FirefoxBinary getBinary(Capabilities capabilities) {
    if(capabilities != null && capabilities.getCapability("firefox_binary") != null) {
        Object raw = capabilities.getCapability("firefox_binary");
        if(raw instanceof FirefoxBinary) {
            return (FirefoxBinary)raw;
        } else {
            File file = new File((String)raw);
            return new FirefoxBinary(file);
        }
    } else {
        return new FirefoxBinary();
    }
}

public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile) {
    this(binary, profile, DesiredCapabilities.firefox());
}

public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities capabilities) {
    this(binary, profile, capabilities, (Capabilities)null);
}

public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities desiredCapabilities, Capabilities requiredCapabilities) {
    super(new FirefoxDriver.LazyCommandExecutor(binary, profile, null), dropCapabilities(desiredCapabilities, new String[]{"firefox_binary", "firefox_profile"}), dropCapabilities(requiredCapabilities, new String[]{"firefox_binary", "firefox_profile"}));
    this.binary = binary;
}

public void setFileDetector(FileDetector detector) {
    throw new WebDriverException("Setting the file detector only works on remote webdriver instances obtained via RemoteWebDriver");
}

public void kill() {
    this.binary.quit();
}

public Options manage() {
    return new RemoteWebDriverOptions(this) {
        public Timeouts timeouts() {
            return new RemoteTimeouts(this) {
                public Timeouts implicitlyWait(long time, TimeUnit unit) {
                    FirefoxDriver.this.execute("setTimeout", ImmutableMap.of("type", "implicit", "ms", Long.valueOf(TimeUnit.MILLISECONDS.convert(time, unit))));
                    return this;
                }

                public Timeouts setScriptTimeout(long time, TimeUnit unit) {
                    FirefoxDriver.this.execute("setTimeout", ImmutableMap.of("type", "script", "ms", Long.valueOf(TimeUnit.MILLISECONDS.convert(time, unit))));
                    return this;
                }
            };
        }
    };
}

protected void startClient() {
    FirefoxDriver.LazyCommandExecutor exe = (FirefoxDriver.LazyCommandExecutor)this.getCommandExecutor();
    FirefoxProfile profileToUse = getProfile(exe.profile);
    ExtensionConnection connection = this.connectTo(exe.binary, profileToUse, "localhost");
    exe.setConnection(connection);

    try {
        connection.start();
    } catch (IOException var5) {
        throw new WebDriverException("An error occurred while connecting to Firefox", var5);
    }
}

private static FirefoxProfile getProfile(FirefoxProfile profile) {
    FirefoxProfile profileToUse = profile;
    String suggestedProfile = System.getProperty("webdriver.firefox.profile");
    if(profile == null && suggestedProfile != null) {
        profileToUse = (new ProfilesIni()).getProfile(suggestedProfile);
        if(profileToUse == null) {
            throw new WebDriverException(String.format("Firefox profile \'%s\' named in system property \'%s\' not found", new Object[]{suggestedProfile, "webdriver.firefox.profile"}));
        }
    } else if(profile == null) {
        profileToUse = new FirefoxProfile();
    }

    return profileToUse;
}

protected ExtensionConnection connectTo(FirefoxBinary binary, FirefoxProfile profile, String host) {
    Lock lock = this.obtainLock(profile);

    try {
        FirefoxBinary e = binary == null?new FirefoxBinary():binary;
        return new NewProfileExtensionConnection(lock, e, profile, host);
    } catch (Exception var6) {
        throw new WebDriverException(var6);
    }
}

protected Lock obtainLock(FirefoxProfile profile) {
    return new SocketLock();
}

protected void stopClient() {
    ((FirefoxDriver.LazyCommandExecutor)this.getCommandExecutor()).quit();
}

private static Capabilities dropCapabilities(Capabilities capabilities, String... keysToRemove) {
    if(capabilities == null) {
        return new DesiredCapabilities();
    } else {
        final HashSet toRemove = Sets.newHashSet(keysToRemove);
        DesiredCapabilities caps = new DesiredCapabilities(Maps.filterKeys(capabilities.asMap(), new Predicate() {
            public boolean apply(String key) {
                return !toRemove.contains(key);
            }
        }));
        Proxy proxy = Proxy.extractFrom(capabilities);
        if(proxy != null) {
            caps.setCapability("proxy", (new BeanToJsonConverter()).convert(proxy));
        }

        return caps;
    }
}

public <X> X getScreenshotAs(OutputType<X> target) {
    String base64 = this.execute("screenshot").getValue().toString();
    return target.convertFromBase64Png(base64);
}

static {
    DEFAULT_ENABLE_NATIVE_EVENTS = Platform.getCurrent().is(Platform.WINDOWS);
}

public static class LazyCommandExecutor implements CommandExecutor, NeedsLocalLogs {
    private ExtensionConnection connection;
    private final FirefoxBinary binary;
    private final FirefoxProfile profile;
    private LocalLogs logs;

    private LazyCommandExecutor(FirefoxBinary binary, FirefoxProfile profile) {
        this.logs = LocalLogs.getNullLogger();
        this.binary = binary;
        this.profile = profile;
    }

    public void setConnection(ExtensionConnection connection) {
        this.connection = connection;
        connection.setLocalLogs(this.logs);
    }

    public void quit() {
        if(this.connection != null) {
            this.connection.quit();
            this.connection = null;
        }

        if(this.profile != null) {
            this.profile.cleanTemporaryModel();
        }

    }

    public Response execute(Command command) throws IOException {
        if(this.connection == null) {
            if(command.getName().equals("quit")) {
                return new Response();
            } else {
                throw new SessionNotFoundException("The FirefoxDriver cannot be used after quit() was called.");
            }
        } else {
            return this.connection.execute(command);
        }
    }

    public void setLocalLogs(LocalLogs logs) {
        this.logs = logs;
        if(this.connection != null) {
            this.connection.setLocalLogs(logs);
        }

    }

    public URI getAddressOfRemoteServer() {
        return this.connection.getAddressOfRemoteServer();
    }
}

public static final class SystemProperty {
    public static final String BROWSER_BINARY = "webdriver.firefox.bin";
    public static final String BROWSER_LOGFILE = "webdriver.firefox.logfile";
    public static final String BROWSER_LIBRARY_PATH = "webdriver.firefox.library.path";
    public static final String BROWSER_PROFILE = "webdriver.firefox.profile";
    public static final String DRIVER_XPI_PROPERTY = "webdriver.firefox.driver";
    public static final String DRIVER_USE_MARIONETTE = "webdriver.firefox.marionette";

    public SystemProperty() {
    }
}

}

转载于:https://www.cnblogs.com/ITniu/p/7475075.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐