|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.openqa.selenium.remote.server; |
| 18 | + |
| 19 | +import org.openqa.selenium.Capabilities; |
| 20 | +import org.openqa.selenium.WebDriver; |
| 21 | +import org.openqa.selenium.WebDriverException; |
| 22 | +import org.openqa.selenium.remote.BrowserType; |
| 23 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 24 | + |
| 25 | +import java.lang.reflect.Constructor; |
| 26 | +import java.lang.reflect.InvocationTargetException; |
| 27 | +import java.util.logging.Level; |
| 28 | +import java.util.logging.Logger; |
| 29 | + |
| 30 | +/** |
| 31 | + * This driver provider that instantiates FirefoxDriver or MarionetteDriver. |
| 32 | + */ |
| 33 | +public class FirefoxDriverProvider implements DriverProvider { |
| 34 | + |
| 35 | + private static final Logger LOG = Logger.getLogger(FirefoxDriverProvider.class.getName()); |
| 36 | + |
| 37 | + @Override |
| 38 | + public Capabilities getProvidedCapabilities() { |
| 39 | + return DesiredCapabilities.firefox(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Checks that driver class can be loaded. |
| 44 | + */ |
| 45 | + @Override |
| 46 | + public boolean canCreateDriverInstances() { |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Checks that the browser name set in the provided capabilities matches the browser name |
| 52 | + * set in the desired capabilities. |
| 53 | + * @param capabilities The desired capabilities |
| 54 | + * @return true if the browser name is the same, false otherwise |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public boolean canCreateDriverInstanceFor(Capabilities capabilities) { |
| 58 | + return BrowserType.FIREFOX.equals(capabilities.getBrowserName()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public WebDriver newInstance(Capabilities capabilities) { |
| 63 | + LOG.info("Creating a new session for " + capabilities); |
| 64 | + Object marionette = capabilities.getCapability("marionette"); |
| 65 | + if (marionette != null && Boolean.valueOf(marionette.toString())) { |
| 66 | + return callConstructor("org.openqa.selenium.firefox.MarionetteDriver", capabilities); |
| 67 | + } else { |
| 68 | + return callConstructor("org.openqa.selenium.firefox.FirefoxDriver", capabilities); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private Class<? extends WebDriver> getDriverClass(String driverClassName) { |
| 73 | + try { |
| 74 | + return Class.forName(driverClassName).asSubclass(WebDriver.class); |
| 75 | + } catch (ClassNotFoundException e) { |
| 76 | + LOG.log(Level.INFO, "Driver class not found: " + driverClassName); |
| 77 | + return null; |
| 78 | + } catch (NoClassDefFoundError e) { |
| 79 | + LOG.log(Level.INFO, "Driver class not found: " + driverClassName); |
| 80 | + return null; |
| 81 | + } catch (UnsupportedClassVersionError e) { |
| 82 | + LOG.log(Level.INFO, "Driver class is built for higher Java version: " + driverClassName); |
| 83 | + return null; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private WebDriver callConstructor(String driverClassName, Capabilities capabilities) { |
| 88 | + Class<? extends WebDriver> from = getDriverClass(driverClassName); |
| 89 | + try { |
| 90 | + Constructor<? extends WebDriver> constructor = from.getConstructor(Capabilities.class); |
| 91 | + return constructor.newInstance(capabilities); |
| 92 | + } catch (NoSuchMethodException e) { |
| 93 | + try { |
| 94 | + return from.newInstance(); |
| 95 | + } catch (InstantiationException e1) { |
| 96 | + throw new WebDriverException(e); |
| 97 | + } catch (IllegalAccessException e1) { |
| 98 | + throw new WebDriverException(e); |
| 99 | + } |
| 100 | + } catch (InvocationTargetException e) { |
| 101 | + throw new WebDriverException(e); |
| 102 | + } catch (InstantiationException e) { |
| 103 | + throw new WebDriverException(e); |
| 104 | + } catch (IllegalAccessException e) { |
| 105 | + throw new WebDriverException(e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String toString() { |
| 111 | + return "Firefox/Marionette driver"; |
| 112 | + } |
| 113 | +} |
0 commit comments