io.github.bonigarcia.seljup.CapabilitiesHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-jupiter Show documentation
Show all versions of selenium-jupiter Show documentation
JUnit 5 extension for Selenium WebDriver
The newest version!
/*
* (C) Copyright 2021 Boni Garcia (https://bonigarcia.github.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.github.bonigarcia.seljup;
import static java.io.File.createTempFile;
import static java.lang.invoke.MethodHandles.lookup;
import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chromium.ChromiumDriver;
import org.openqa.selenium.chromium.ChromiumOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;
import org.slf4j.Logger;
import com.google.gson.internal.LinkedTreeMap;
import io.github.bonigarcia.seljup.BrowsersTemplate.Browser;
import io.github.bonigarcia.seljup.config.Config;
import io.github.bonigarcia.wdm.WebDriverManager;
/**
* Driver handler.
*
* @author Boni Garcia
* @since 4.0.0
*/
public class CapabilitiesHandler {
static final Logger log = getLogger(lookup().lookupClass());
Config config;
AnnotationsReader annotationsReader;
Parameter parameter;
ExtensionContext extensionContext;
Optional browser;
Optional browserType;
boolean isGeneric;
boolean isOpera;
public CapabilitiesHandler(Config config,
AnnotationsReader annotationsReader, Parameter parameter,
ExtensionContext extensionContext, Optional browser,
Optional browserType, boolean isGeneric,
boolean isOpera) {
this.config = config;
this.annotationsReader = annotationsReader;
this.parameter = parameter;
this.extensionContext = extensionContext;
this.browser = browser;
this.browserType = browserType;
this.isGeneric = isGeneric;
this.isOpera = isOpera;
}
public Optional getCapabilities() {
Optional> optionsClass = getOptionsClass();
if (optionsClass.isPresent()) {
Capabilities options = getOptions(optionsClass.get());
if (options != null) {
return Optional.of(options);
}
}
return Optional.empty();
}
private Optional> getOptionsClass() {
Class> type = parameter.getType();
log.trace("Getting capabilities for type={} -- browserType={}", type,
browserType);
if (type == ChromeDriver.class || (browserType.isPresent()
&& browserType.get().isChromeBased())) {
return Optional.of(ChromeOptions.class);
} else if (type == FirefoxDriver.class || (browserType.isPresent()
&& browserType.get() == BrowserType.FIREFOX)) {
return Optional.of(FirefoxOptions.class);
} else if (isOpera || (browserType.isPresent()
&& browserType.get() == BrowserType.OPERA)) {
return Optional.of(ChromeOptions.class);
} else if (type == EdgeDriver.class || (browserType.isPresent()
&& browserType.get() == BrowserType.EDGE)) {
return Optional.of(EdgeOptions.class);
} else if (type == SafariDriver.class || (browserType.isPresent()
&& browserType.get() == BrowserType.SAFARI)) {
return Optional.of(SafariOptions.class);
} else if (type == InternetExplorerDriver.class) {
return Optional.of(InternetExplorerOptions.class);
} else if (type == ChromiumDriver.class) {
return Optional.of(ChromiumOptions.class);
} else if (isGeneric) {
String defaultBrowser = WebDriverManager.getInstance().config()
.getDefaultBrowser();
browserType = Optional.of(Browser.toBrowserType(defaultBrowser));
}
return Optional.empty();
}
private Capabilities getOptions(
Class extends Capabilities> optionsClass) {
Optional