下面列出了怎么用org.openqa.selenium.opera.OperaOptions的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void resolve() {
try {
Optional<Object> testInstance = context.getTestInstance();
Optional<Capabilities> capabilities = annotationsReader
.getCapabilities(parameter, testInstance);
OperaOptions operaOptions = (OperaOptions) getOptions(parameter,
testInstance);
if (capabilities.isPresent()) {
operaOptions.merge(capabilities.get());
}
object = new OperaDriver(operaOptions);
} catch (Exception e) {
handleException(e);
}
}
private static void testGetOperaWebDriver(WebDriverConfiguration configuration, OperaOptions operaOptions)
throws Exception
{
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
OperaDriver expected = mock(OperaDriver.class);
whenNew(OperaDriver.class).withParameterTypes(OperaOptions.class).withArguments(operaOptions)
.thenReturn(expected);
WebDriver actual = WebDriverType.OPERA.getWebDriver(desiredCapabilities, configuration);
assertEquals(expected, actual);
}
@Test
@PrepareForTest(fullyQualifiedNames = "org.vividus.selenium.WebDriverType$6")
public void testGetOperaWebDriverWithAdditionalOptions() throws Exception
{
Map<String, String> experimentalOptionValue = singletonMap(OPTION_KEY, OPTION_VALUE);
WebDriverConfiguration configuration = new WebDriverConfiguration();
configuration.setBinaryPath(Optional.of(PATH));
configuration.setCommandLineArguments(new String[] { ARGUMENT });
configuration.setExperimentalOptions(singletonMap(MOBILE_EMULATION, experimentalOptionValue));
OperaOptions operaOptions = new OperaOptions();
operaOptions.setBinary(PATH);
operaOptions.addArguments(ARGUMENT);
operaOptions.setExperimentalOption(MOBILE_EMULATION, experimentalOptionValue);
testGetOperaWebDriver(configuration, operaOptions);
}
private void verifyIfDriverIsOpera() {
if(OPERA.equals(System.getProperty("context"))){
OperaOptions operaOptions = new OperaOptions();
operaOptions.setBinary(System.getProperty("binary"));
BrowseTheWeb.as(theActorCalled(CESAR)).setDriver(new OperaDriver(operaOptions));
}
}
@Override
public MutableCapabilities getOptions(Parameter parameter,
Optional<Object> testInstance)
throws IOException, IllegalAccessException {
OperaOptions operaOptions = new OperaOptions();
if (parameter != null) {
// @Arguments
Arguments arguments = parameter.getAnnotation(Arguments.class);
if (arguments != null) {
stream(arguments.value()).forEach(operaOptions::addArguments);
}
// @Extensions
Extensions extensions = parameter.getAnnotation(Extensions.class);
if (extensions != null) {
for (String extension : extensions.value()) {
operaOptions.addExtensions(getExtension(extension));
}
}
// @Binary
Binary binary = parameter.getAnnotation(Binary.class);
if (binary != null) {
operaOptions.setBinary(binary.value());
}
// @Options
OperaOptions optionsFromAnnotatedField = annotationsReader
.getFromAnnotatedField(testInstance, Options.class,
OperaOptions.class);
if (optionsFromAnnotatedField != null) {
operaOptions = optionsFromAnnotatedField.merge(operaOptions);
}
}
return operaOptions;
}
@Test
void testOperaOptions() throws Exception {
Parameter parameter = OperaWithOptionsJupiterTest.class
.getMethod("operaTest", OperaDriver.class).getParameters()[0];
OperaOptions operaOptions = (OperaOptions) annotationsReader
.getOptions(parameter, empty());
assertThat(operaOptions.asMap().get("operaOptions").toString(),
containsString("binary"));
}
@Test
void testAnnotatedOperaOptionsIsSelectedOverOtherAnnotatedOptions()
throws Exception {
Optional<Object> testInstance = Optional
.of(new ClassWithMultipleOptions());
OperaOptions operaOptions = (OperaOptions) annotationsReader
.getOptions(null, testInstance);
assertThat(operaOptions, notNullValue());
}
/**
* Задает options для запуска Opera драйвера
* options можно передавать, как системную переменную, например -Doptions=--load-extension=my-custom-extension
*
* @return operaOptions
*/
private OperaOptions getOperaDriverOptions(DesiredCapabilities capabilities) {
log.info("---------------Opera Driver---------------------");
OperaOptions operaOptions = !options[0].equals("") ? new OperaOptions().addArguments(options) : new OperaOptions();
operaOptions.setCapability(CapabilityType.BROWSER_VERSION, loadSystemPropertyOrDefault(CapabilityType.BROWSER_VERSION, VERSION_LATEST));
operaOptions.merge(capabilities);
return operaOptions;
}
/**
* Задает options для запуска Opera драйвера в контейнере Selenoid
* options можно передавать, как системную переменную, например -Doptions=--load-extension=my-custom-extension
*
* @return operaOptions
*/
private OperaOptions getOperaRemoteDriverOptions(DesiredCapabilities capabilities) {
log.info("---------------Opera Driver---------------------");
OperaOptions operaOptions = !this.options[0].equals("") ? (new OperaOptions()).addArguments(this.options) : new OperaOptions();
operaOptions.setCapability("browserVersion", PropertyLoader.loadSystemPropertyOrDefault("browserVersion", "latest"));
operaOptions.setCapability("browserName", "opera");
operaOptions.setBinary(PropertyLoader.loadSystemPropertyOrDefault("webdriver.opera.driver", "/usr/bin/opera"));
operaOptions.merge(capabilities);
return operaOptions;
}
public OperaUser(String userName, int timeOfWaitInSeconds) {
super(userName, timeOfWaitInSeconds);
OperaOptions options = new OperaOptions();
options.setBinary("/usr/bin/opera");
DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();
capabilities.setAcceptInsecureCerts(true);
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
options.addArguments("--use-fake-ui-for-media-stream");
options.addArguments("--use-fake-device-for-media-stream");
capabilities.setCapability(OperaOptions.CAPABILITY, options);
String REMOTE_URL = System.getProperty("REMOTE_URL_OPERA");
if (REMOTE_URL != null) {
log.info("Using URL {} to connect to remote web driver", REMOTE_URL);
try {
this.driver = new RemoteWebDriver(new URL(REMOTE_URL), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else {
log.info("Using local web driver");
this.driver = new OperaDriver(capabilities);
}
this.driver.manage().timeouts().setScriptTimeout(this.timeOfWaitInSeconds, TimeUnit.SECONDS);
this.configureDriver();
}
private static Capabilities operaWithCustomCapabilities(Capabilities originalCapabilities) {
OperaOptions options = new OperaOptions();
options.addArguments("disable-extensions");
String operaPath = System.getProperty("webdriver.opera.binary");
if (operaPath != null) {
options.setBinary(new File(operaPath));
}
if (originalCapabilities != null) {
options.merge(originalCapabilities);
}
return options;
}
private Capabilities createCapabilities() {
String property = System.getProperty("selenium.browser", "ff");
Browser browser = Browser.detect();
switch (browser) {
case CHROME:
return new ChromeOptions();
case EDGE:
return new EdgeHtmlOptions();
case CHROMIUMEDGE:
return new EdgeOptions();
case IE:
return new InternetExplorerOptions();
case FIREFOX:
case MARIONETTE:
return new FirefoxOptions();
case OPERA:
case OPERABLINK:
return new OperaOptions();
case SAFARI:
return new SafariOptions();
default:
fail("Attempt to use an unsupported browser: " + property);
// we never get here, but keep null checks happy anyway
return new DesiredCapabilities();
}
}
@Test
@PrepareForTest(fullyQualifiedNames = "org.vividus.selenium.WebDriverType$6")
public void testGetOperaWebDriver() throws Exception
{
testGetOperaWebDriver(new WebDriverConfiguration(), new OperaOptions());
}
private DesiredCapabilities getCapabilities(BrowserInstance browserInstance,
boolean enableVnc) throws IllegalAccessException, IOException {
DesiredCapabilities capabilities = browserInstance.getCapabilities();
if (enableVnc) {
capabilities.setCapability("enableVNC", true);
capabilities.setCapability("screenResolution",
getConfig().getVncScreenResolution());
}
if (getConfig().isRecording() || getConfig().isRecordingWhenFailure()) {
capabilities.setCapability("enableVideo", true);
capabilities.setCapability("videoScreenSize",
getConfig().getRecordingVideoScreenSize());
capabilities.setCapability("videoFrameRate",
getConfig().getRecordingVideoFrameRate());
}
Optional<Capabilities> optionalCapabilities = annotationsReader != null
? annotationsReader.getCapabilities(parameter, testInstance)
: Optional.of(new DesiredCapabilities());
MutableCapabilities options = browserInstance.getDriverHandler()
.getOptions(parameter, testInstance);
// Due to bug in operablink the binary path must be set
if (browserInstance.getBrowserType() == OPERA
&& browserVersion.equals("62.0")) {
String operaBinaryPathLinux = getConfig().getOperaBinaryPathLinux();
((OperaOptions) options).setBinary(operaBinaryPathLinux);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary(operaBinaryPathLinux);
OperaOptions operaOptions = new OperaOptions().merge(chromeOptions);
operaOptions.setCapability("browserName", OPERA_NAME);
options.merge(operaOptions);
log.trace("Opera options: {}", options);
}
if (optionalCapabilities.isPresent()) {
options.merge(optionalCapabilities.get());
}
capabilities.setCapability(browserInstance.getOptionsKey(), options);
log.trace("Using {}", capabilities);
return capabilities;
}
public OperaFactory() {
super((capabilities) -> {
OperaOptions operaOptions = new OperaOptions().merge(capabilities);
return new OperaDriver(operaOptions);
});
}
public WebDriver createDriver(OperaOptions options) {
return new OperaDriver(options);
}