下面列出了怎么用org.openqa.selenium.UnexpectedAlertBehaviour的API类实例代码及写法,或者点击链接到github查看源代码。
public void addUnexpectedAlertBehavior() {
unexpectedAlertBehaviour = new ChoiceBox<>();
unexpectedAlertBehaviour.getItems().add(null);
unexpectedAlertBehaviour.getItems().addAll(FXCollections.observableArrayList(UnexpectedAlertBehaviour.values()));
String value = BrowserConfig.instance().getValue(getBrowserName(), "browser-unexpected-alert-behaviour");
if (value != null)
unexpectedAlertBehaviour.getSelectionModel().select(UnexpectedAlertBehaviour.fromString(value));
advancedPane.addFormField("Unexpected alert behaviour:", unexpectedAlertBehaviour);
}
/**
* 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);
}
/**
* Generates a firefox webdriver.
*
* @return
* A firefox webdriver
* @throws TechnicalException
* if an error occured when Webdriver setExecutable to true.
*/
private WebDriver generateFirefoxDriver() throws TechnicalException {
final String pathWebdriver = DriverFactory.getPath(Driver.FIREFOX);
if (!new File(pathWebdriver).setExecutable(true)) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
}
log.info("Generating Firefox driver ({}) ...", pathWebdriver);
System.setProperty(Driver.FIREFOX.getDriverName(), pathWebdriver);
final FirefoxOptions firefoxOptions = new FirefoxOptions();
final FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
firefoxOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
setLoggingLevel(firefoxOptions);
// Proxy configuration
if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
firefoxOptions.setCapability(CapabilityType.PROXY, Context.getProxy());
}
if (Context.isHeadless()) {
firefoxBinary.addCommandLineOptions("--headless");
firefoxOptions.setBinary(firefoxBinary);
}
firefoxOptions.setLogLevel(FirefoxDriverLogLevel.FATAL);
return new FirefoxDriver(firefoxOptions);
}
public FirefoxUser(String userName, int timeOfWaitInSeconds) {
super(userName, timeOfWaitInSeconds);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setAcceptInsecureCerts(true);
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
FirefoxProfile profile = new FirefoxProfile();
// This flag avoids granting the access to the camera
profile.setPreference("media.navigator.permission.disabled", true);
// This flag force to use fake user media (synthetic video of multiple color)
profile.setPreference("media.navigator.streams.fake", true);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
String REMOTE_URL = System.getProperty("REMOTE_URL_FIREFOX");
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 FirefoxDriver(capabilities);
}
this.configureDriver();
}
private ChromeUser(String userName, int timeOfWaitInSeconds, ChromeOptions options) {
super(userName, timeOfWaitInSeconds);
options.setAcceptInsecureCerts(true);
options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE);
options.addArguments("--disable-infobars");
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.media_stream_mic", 1);
prefs.put("profile.default_content_setting_values.media_stream_camera", 1);
options.setExperimentalOption("prefs", prefs);
String REMOTE_URL = System.getProperty("REMOTE_URL_CHROME");
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), options);
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else {
log.info("Using local web driver");
this.driver = new ChromeDriver(options);
}
this.driver.manage().timeouts().setScriptTimeout(this.timeOfWaitInSeconds, TimeUnit.SECONDS);
this.configureDriver();
}
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();
}
static void addDefaultCapabilities(MutableCapabilities capabilities) {
Set<String> capabilityNames = capabilities.getCapabilityNames();
if (capabilityNames.contains(CapabilityType.BROWSER_NAME)
&& !capabilityNames.contains(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR)) {
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
}
}
public DO setUnhandledPromptBehaviour(UnexpectedAlertBehaviour behaviour) {
setCapability(
UNHANDLED_PROMPT_BEHAVIOUR,
Require.nonNull("Unhandled prompt behavior", behaviour));
setCapability(UNEXPECTED_ALERT_BEHAVIOUR, behaviour);
return (DO) this;
}
public IECaps(DesiredCapabilities customCaps, String version, UnexpectedAlertBehaviour alertBehaviour) {
super(customCaps);
this.version = version;
this.alertBehaviour = alertBehaviour;
}
public FireFoxCaps(DesiredCapabilities customCaps, UnexpectedAlertBehaviour alertBehaviour, Platform platform) {
super(customCaps);
this.alertBehaviour = alertBehaviour;
this.platform = platform;
}
public EdgeCaps(DesiredCapabilities customCaps, UnexpectedAlertBehaviour alertBehaviour) {
super(customCaps);
this.alertBehaviour = alertBehaviour;
}
public ChromeCaps(DesiredCapabilities customCaps, UnexpectedAlertBehaviour alertBehaviour, boolean isHeadless, Platform platform) {
super(customCaps);
this.alertBehaviour = alertBehaviour;
this.isHeadless = isHeadless;
this.platform = platform;
}
public GeckoCaps(DesiredCapabilities customCaps, UnexpectedAlertBehaviour alertBehaviour, Platform platform) {
super(customCaps);
this.alertBehaviour = alertBehaviour;
this.platform = platform;
}
/**
* Generates a chrome webdriver.
*
* @return
* A chrome webdriver
* @throws TechnicalException
* if an error occured when Webdriver setExecutable to true.
*/
private WebDriver generateGoogleChromeDriver() throws TechnicalException {
final String pathWebdriver = DriverFactory.getPath(Driver.CHROME);
if (!new File(pathWebdriver).setExecutable(true)) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
}
log.info("Generating Chrome driver ({}) ...", pathWebdriver);
System.setProperty(Driver.CHROME.getDriverName(), pathWebdriver);
final ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
chromeOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
chromeOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
setLoggingLevel(chromeOptions);
chromeOptions.addArguments("--ignore-certificate-errors");
if (Context.isHeadless()) {
chromeOptions.addArguments("--headless");
}
// Proxy configuration
if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
chromeOptions.setCapability(CapabilityType.PROXY, Context.getProxy());
}
// add Modifyheader Extensions to Chrome
if (Context.getWebdriversProperties(MODIFYHEADER_PATH) != null && !"".equals(Context.getWebdriversProperties(MODIFYHEADER_PATH))) {
chromeOptions.addExtensions(new File(Context.getWebdriversProperties(MODIFYHEADER_PATH)));
}
// Set custom downloaded file path. When you check content of downloaded file by robot.
final HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("download.default_directory", System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER);
chromeOptions.setExperimentalOption("prefs", chromePrefs);
// Set custom chromium (if you not use default chromium on your target device)
final String targetBrowserBinaryPath = Context.getWebdriversProperties(TARGET_BROWSER_BINARY_PATH);
if (targetBrowserBinaryPath != null && !"".equals(targetBrowserBinaryPath)) {
chromeOptions.setBinary(targetBrowserBinaryPath);
}
log.info("addArguments [{}] to webdriver.", Context.getWebdriversProperties(WEBDRIVER_OPTIONS_ADDITIONAL_ARGS));
for (String additionalArgument : Context.getWebdriversProperties(WEBDRIVER_OPTIONS_ADDITIONAL_ARGS).split(",")) {
log.info("addArgument [{}] to webdriver.", additionalArgument);
chromeOptions.addArguments(additionalArgument);
}
if (Context.getWebdriversProperties(REMOTE_WEBDRIVER_URL) != null && !"".equals(Context.getWebdriversProperties(REMOTE_WEBDRIVER_URL))
&& Context.getWebdriversProperties(REMOTE_WEBDRIVER_BROWSER_VERSION) != null && !"".equals(Context.getWebdriversProperties(REMOTE_WEBDRIVER_BROWSER_VERSION))
&& Context.getWebdriversProperties(REMOTE_WEBDRIVER_PLATFORM_NAME) != null && !"".equals(Context.getWebdriversProperties(REMOTE_WEBDRIVER_PLATFORM_NAME))) {
chromeOptions.setCapability("browserVersion", Context.getWebdriversProperties(REMOTE_WEBDRIVER_BROWSER_VERSION));
chromeOptions.setCapability("platformName", Context.getWebdriversProperties(REMOTE_WEBDRIVER_PLATFORM_NAME));
try {
return new RemoteWebDriver(new URL(Context.getWebdriversProperties(REMOTE_WEBDRIVER_URL)), chromeOptions);
} catch (MalformedURLException e) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_REMOTE_WEBDRIVER_URL));
}
} else {
final String withWhitelistedIps = Context.getWebdriversProperties(WITH_WHITE_LISTED_IPS);
if (withWhitelistedIps != null && !"".equals(withWhitelistedIps)) {
final ChromeDriverService service = new ChromeDriverService.Builder().withWhitelistedIps(withWhitelistedIps).withVerbose(false).build();
return new ChromeDriver(service, chromeOptions);
} else {
return new ChromeDriver(chromeOptions);
}
}
}