下面列出了怎么用org.openqa.selenium.ie.InternetExplorerOptions的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("unchecked")
private static DesiredCapabilities testGetIExploreWebDriver(WebDriverConfiguration configuration) throws Exception
{
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
whenNew(InternetExplorerOptions.class).withArguments(desiredCapabilities).thenReturn(internetExplorerOptions);
whenNew(InternetExplorerOptions.class).withNoArguments().thenReturn(internetExplorerOptions);
InternetExplorerDriver expected = mock(InternetExplorerDriver.class);
whenNew(InternetExplorerDriver.class)
.withParameterTypes(InternetExplorerOptions.class)
.withArguments(internetExplorerOptions)
.thenReturn(expected);
WebDriver actual = WebDriverType.IEXPLORE.getWebDriver(desiredCapabilities, configuration);
assertEquals(expected, actual);
Map<String, Object> options = (Map<String, Object>) desiredCapabilities.getCapability(IE_OPTIONS);
assertTrue((boolean) options.get(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS));
return desiredCapabilities;
}
private static WebDriver setupIeDriver() throws MalformedURLException {
LOG.i("Setting up Internet Explorer driver...");
setupDriver(iedriver());
final InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.destructivelyEnsureCleanSession();
ieOptions.setCapability("requireWindowFocus", true);
ieOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
final InternetExplorerDriverService ieService = InternetExplorerDriverService.createDefaultService();
if (!OS.isWindows()) {
Assert.fail("IE is not supported.");
}
if (appSetting().isHeadlessMode()) {
LOG.w("IE does not support headless mode. Hence, ignoring the same...");
}
return new InternetExplorerDriver(ieService, ieOptions);
}
private InternetExplorerOptions getIEOptions() {
InternetExplorerOptions caps = new InternetExplorerOptions();
caps.setCapability("version", this.version);
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
caps.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
caps.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true);
caps.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
caps.setCapability(CapabilityType.SUPPORTS_ALERTS, true);
caps.setCapability("platform", Platform.WINDOWS);
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, this.alertBehaviour);
//Found that setting this capability could increase IE tests speed. Should be checked.
caps.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
setLoggingPrefs(caps);
return caps;
}
@Override
public void resolve() {
try {
Optional<Object> testInstance = context.getTestInstance();
Optional<Capabilities> capabilities = annotationsReader
.getCapabilities(parameter, testInstance);
InternetExplorerOptions internetExplorerOptions = (InternetExplorerOptions) getOptions(
parameter, testInstance);
if (capabilities.isPresent()) {
internetExplorerOptions.merge(capabilities.get());
}
object = new InternetExplorerDriver(internetExplorerOptions);
} catch (Exception e) {
handleException(e);
}
}
@Override
public InternetExplorerOptions getCapabilities() {
InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
setCapabilities(internetExplorerOptions);
internetExplorerOptions.setPageLoadStrategy(getPageLoadStrategy());
return internetExplorerOptions;
}
private void setupIE() {
InternetExplorerOptions explorerOptions = new InternetExplorerOptions();
explorerOptions.destructivelyEnsureCleanSession();
capabilities.setCapability("se:ieOptions", explorerOptions);
capabilities.merge(explorerOptions);
}
public InternetExplorerOptions get() {
InternetExplorerOptions caps = getIEOptions();
if (!this.customCaps.asMap().isEmpty()) {
caps.merge(this.customCaps);
}
return caps;
}
/**
* Generates an ie webdriver. Unable to use it with a proxy. Causes a crash.
*
* @return
* An ie webdriver
* @throws TechnicalException
* if an error occured when Webdriver setExecutable to true.
*/
private WebDriver generateIEDriver() throws TechnicalException {
final String pathWebdriver = DriverFactory.getPath(Driver.IE);
if (!new File(pathWebdriver).setExecutable(true)) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
}
log.info("Generating IE driver ({}) ...", pathWebdriver);
System.setProperty(Driver.IE.getDriverName(), pathWebdriver);
final InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
internetExplorerOptions.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
internetExplorerOptions.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
internetExplorerOptions.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
internetExplorerOptions.setCapability(InternetExplorerDriver.NATIVE_EVENTS, false);
internetExplorerOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
internetExplorerOptions.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
internetExplorerOptions.setCapability("disable-popup-blocking", true);
setLoggingLevel(internetExplorerOptions);
// Proxy configuration
if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
internetExplorerOptions.setCapability(CapabilityType.PROXY, Context.getProxy());
}
return new InternetExplorerDriver(internetExplorerOptions);
}
@Test
public void testInternetExplorer()
{
DesiredCapabilities capabilities = createCapabilitiesWithProxy();
InternetExplorerOptions options = new InternetExplorerOptions();
options.merge(capabilities);
Assert.assertTrue(options.getCapability(CapabilityType.PROXY) != null);
}
@Override
public MutableCapabilities getOptions(Parameter parameter,
Optional<Object> testInstance)
throws IOException, IllegalAccessException {
InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
InternetExplorerOptions optionsFromAnnotatedField = annotationsReader
.getFromAnnotatedField(testInstance, Options.class,
InternetExplorerOptions.class);
if (optionsFromAnnotatedField != null) {
internetExplorerOptions = optionsFromAnnotatedField;
}
return internetExplorerOptions;
}
/**
* Задает options для запуска IE драйвера
* options можно передавать, как системную переменную, например -Doptions=--load-extension=my-custom-extension
*
* @return internetExplorerOptions
*/
private InternetExplorerOptions getIEDriverOptions(DesiredCapabilities capabilities) {
log.info("---------------IE Driver---------------------");
InternetExplorerOptions internetExplorerOptions = !options[0].equals("") ? new InternetExplorerOptions().addCommandSwitches(options) : new InternetExplorerOptions();
internetExplorerOptions.setCapability(CapabilityType.BROWSER_VERSION, loadSystemPropertyOrDefault(CapabilityType.BROWSER_VERSION, VERSION_LATEST));
internetExplorerOptions.setCapability("ie.usePerProcessProxy", "true");
internetExplorerOptions.setCapability("requireWindowFocus", "false");
internetExplorerOptions.setCapability("ie.browserCommandLineSwitches", "-private");
internetExplorerOptions.setCapability("ie.ensureCleanSession", "true");
internetExplorerOptions.merge(capabilities);
return internetExplorerOptions;
}
@Test
@EnabledOnOs(OS.WINDOWS)
@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
void canInstantiateInternetExplorerDriverWithInternetExplorerOptions() {
driver = provider.createDriver(new InternetExplorerOptions());
assertTrue(driver instanceof InternetExplorerDriver);
}
@Override
public InternetExplorerOptions getCapabilities() {
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
ieOptions.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, true);
ieOptions.setCapability("requireWindowFocus", true);
return ieOptions;
}
public static InternetExplorerOptions getInternetExplorerOptions(Map<String, Object> profile) {
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
if (profile != null) {
for (Map.Entry<String, Object> profileEntry : profile.entrySet()) {
ieOptions.setCapability(profileEntry.getKey(), profileEntry.getValue());
}
}
return ieOptions;
}
@Test
public void ensureEachOfTheKeyOptionTypesAreSafe() {
// Only include the options where we expect to get a w3c session
Stream.of(
new ChromeOptions(),
new FirefoxOptions(),
new InternetExplorerOptions())
.map(options -> RemoteWebDriver.builder().addAlternative(options))
.forEach(this::listCapabilities);
}
@Test
public void shouldAllowMetaDataToBeSet() {
Map<String, String> expected = singletonMap("cheese", "brie");
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.addMetadata("cloud:options", expected);
Map<String, Object> payload = getPayload(builder);
assertThat(payload.get("cloud:options")).isEqualTo(expected);
}
@Test
public void ifARemoteUrlIsGivenThatIsUsedForTheSession() throws MalformedURLException {
URL expected = new URL("http://localhost:3000/woohoo/cheese");
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.url(expected.toExternalForm());
RemoteWebDriverBuilder.Plan plan = builder.getPlan();
assertThat(plan.isUsingDriverService()).isFalse();
assertThat(plan.getRemoteHost()).isEqualTo(expected);
}
@Test
public void shouldUseADriverServiceIfGivenOneRegardlessOfOtherChoices() throws IOException {
DriverService expected = new FakeDriverService();
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.withDriverService(expected);
RemoteWebDriverBuilder.Plan plan = builder.getPlan();
assertThat(plan.isUsingDriverService()).isTrue();
assertThat(plan.getDriverService()).isEqualTo(expected);
}
@Test
public void settingBothDriverServiceAndUrlIsAnError() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.url("http://example.com/cheese/peas/wd")
.withDriverService(new FakeDriverService()));
}
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();
}
}
/**
* setDriver method to create driver instance
*
* @param browser
* @param environment
* @param platform
* @param optPreferences
* @throws Exception
*/
@SafeVarargs
public final void setDriver(String browser,
String platform,
String environment,
Map<String, Object>... optPreferences)
throws Exception {
DesiredCapabilities caps = null;
String getPlatform = null;
props.load(new FileInputStream(Global_VARS.SE_PROPS));
switch (browser) {
case "firefox":
caps = DesiredCapabilities.firefox();
FirefoxOptions ffOpts = new FirefoxOptions();
FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.setPreference("browser.autofocus", true);
ffProfile.setPreference("browser.tabs.remote.autostart.2", false);
caps.setCapability(FirefoxDriver.PROFILE, ffProfile);
caps.setCapability("marionette", true);
// then pass them to the local WebDriver
if ( environment.equalsIgnoreCase("local") ) {
System.setProperty("webdriver.gecko.driver", props.getProperty("gecko.driver.windows.path"));
webDriver.set(new FirefoxDriver(ffOpts.merge(caps)));
}
break;
case "chrome":
caps = DesiredCapabilities.chrome();
ChromeOptions chOptions = new ChromeOptions();
Map<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("credentials_enable_service", false);
chOptions.setExperimentalOption("prefs", chromePrefs);
chOptions.addArguments("--disable-plugins", "--disable-extensions", "--disable-popup-blocking");
caps.setCapability(ChromeOptions.CAPABILITY, chOptions);
caps.setCapability("applicationCacheEnabled", false);
if ( environment.equalsIgnoreCase("local") ) {
System.setProperty("webdriver.chrome.driver", props.getProperty("chrome.driver.windows.path"));
webDriver.set(new ChromeDriver(chOptions.merge(caps)));
}
break;
case "internet explorer":
caps = DesiredCapabilities.internetExplorer();
InternetExplorerOptions ieOpts = new InternetExplorerOptions();
ieOpts.requireWindowFocus();
ieOpts.merge(caps);
caps.setCapability("requireWindowFocus", true);
if ( environment.equalsIgnoreCase("local") ) {
System.setProperty("webdriver.ie.driver", props.getProperty("ie.driver.windows.path"));
webDriver.set(new InternetExplorerDriver(ieOpts.merge(caps)));
}
break;
}
getEnv = environment;
getPlatform = platform;
sessionId.set(((RemoteWebDriver) webDriver.get()).getSessionId().toString());
sessionBrowser.set(caps.getBrowserName());
sessionVersion.set(caps.getVersion());
sessionPlatform.set(getPlatform);
System.out.println("\n*** TEST ENVIRONMENT = "
+ getSessionBrowser().toUpperCase()
+ "/" + getSessionPlatform().toUpperCase()
+ "/" + getEnv.toUpperCase()
+ "/Selenium Version=" + props.getProperty("selenium.revision")
+ "/Session ID=" + getSessionId() + "\n");
getDriver().manage().timeouts().implicitlyWait(IMPLICIT_TIMEOUT, TimeUnit.SECONDS);
getDriver().manage().window().maximize();
}
/**
* Pass options which will be applied when starting a Internet Explorer browser through Selenium
* @param options Internet Explorer options
*/
@PublicAtsApi
public void setInternetExplorerDriverOptions( InternetExplorerOptions options ) {
internetExplorerOptions = options;
}
/**
* @return the InternetExplorer options
*/
@PublicAtsApi
public InternetExplorerOptions getInternetExplorerDriverOptions() {
return internetExplorerOptions;
}
@Override
public WebDriver create(Capabilities capabilities) {
LOG.info("Starting the initialization of '{}' WebDriver instance", ID);
LOG.debug("Initializing WebDriver with following capabilities: {}", capabilities);
return new InternetExplorerDriver(new InternetExplorerOptions(capabilities));
}
public InternetExplorerFactory() {
super((capabilities) -> {
InternetExplorerOptions ieOptions = new InternetExplorerOptions().merge(capabilities);
return new InternetExplorerDriver(ieOptions);
});
}
public WebDriver createDriver(InternetExplorerOptions options) {
return new InternetExplorerDriver(options);
}
@Override
public WebDriver getWebDriver(Capabilities capabilities) {
return new InternetExplorerDriver(new InternetExplorerOptions(capabilities));
}
public LocallyBuiltInternetExplorerDriver(Capabilities capabilities) {
super(getService(), new InternetExplorerOptions().merge(capabilities));
}