PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-webdriver/atlassian-webdriver-core/src/main/java/com/atlassian/webdriver/DesiredCapabilitiesFactory.java

https://bitbucket.org/atlassian/atlassian-selenium
Java | 127 lines | 94 code | 11 blank | 22 comment | 5 complexity | f59c3774483fbfca80d26ae8b09bda71 MD5 | raw file
  1. package com.atlassian.webdriver;
  2. import com.atlassian.pageobjects.browser.Browser;
  3. import com.atlassian.webdriver.utils.WebDriverUtil;
  4. import org.openqa.selenium.logging.LogType;
  5. import org.openqa.selenium.logging.LoggingPreferences;
  6. import org.openqa.selenium.remote.CapabilityType;
  7. import org.openqa.selenium.remote.DesiredCapabilities;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import javax.annotation.Nonnull;
  11. import javax.annotation.Nullable;
  12. import java.util.logging.Level;
  13. import static com.atlassian.webdriver.WebDriverProperties.WEBDRIVER_CAPABILITIES;
  14. import static com.atlassian.webdriver.WebDriverProperties.WEBDRIVER_LOGGING_BROWSER;
  15. import static com.atlassian.webdriver.WebDriverProperties.WEBDRIVER_LOGGING_CLIENT;
  16. import static com.atlassian.webdriver.WebDriverProperties.WEBDRIVER_LOGGING_DRIVER;
  17. public class DesiredCapabilitiesFactory {
  18. private static final Logger log = LoggerFactory.getLogger(DesiredCapabilitiesFactory.class);
  19. /**
  20. * Return capabilities which specify a requested browser type.
  21. *
  22. * @param browserType type of browser requested
  23. * @return DesiredCapabilities capabilities object with a browser type specified
  24. * @throws UnsupportedOperationException if requested browser type is unsupported
  25. */
  26. @Nonnull
  27. public static DesiredCapabilities defaultCapabilitiesForBrowser(final Browser browserType)
  28. throws UnsupportedOperationException {
  29. final DesiredCapabilities capabilities;
  30. switch (browserType) {
  31. case FIREFOX:
  32. capabilities = DesiredCapabilities.firefox();
  33. break;
  34. case CHROME:
  35. capabilities = DesiredCapabilities.chrome();
  36. break;
  37. case EDGE:
  38. capabilities = DesiredCapabilities.edge();
  39. break;
  40. case IE:
  41. capabilities = DesiredCapabilities.internetExplorer();
  42. break;
  43. case IPHONE:
  44. // iPhone must have iWebDriver app installed
  45. capabilities = DesiredCapabilities.iphone();
  46. break;
  47. case IPAD:
  48. // iPad must have iWebDriver app installed
  49. capabilities = DesiredCapabilities.ipad();
  50. break;
  51. case ANDROID:
  52. // android must have android-server.apk installed and running
  53. capabilities = DesiredCapabilities.android();
  54. break;
  55. case SAFARI:
  56. throw new UnsupportedOperationException("Safari is not a supported Browser Type");
  57. case OPERA:
  58. throw new UnsupportedOperationException("Opera is not a supported Browser Type");
  59. default:
  60. log.error("Unknown browser: {}, defaulting to firefox.", browserType);
  61. capabilities = DesiredCapabilities.firefox();
  62. }
  63. return capabilities;
  64. }
  65. /**
  66. * Parses a list of key=value pairs from "webdriver.capabilities" system property.
  67. *
  68. * @return DesiredCapabilities
  69. * @see WebDriverUtil#createCapabilitiesFromString(java.lang.String)
  70. */
  71. @Nonnull
  72. public static DesiredCapabilities customCapabilitiesFromSystemProperty() {
  73. final String capabilitiesStr = WEBDRIVER_CAPABILITIES.getSystemProperty();
  74. log.info("Loading custom capabilities " + capabilitiesStr);
  75. return WebDriverUtil.createCapabilitiesFromString(capabilitiesStr);
  76. }
  77. /**
  78. * Parses a set of webdriver.logging.* properties and sets logging levels for a browser, a client and a driver.
  79. *
  80. * @return DesiredCapabilities
  81. */
  82. @Nonnull
  83. public static DesiredCapabilities customCapabilitiesForLogging() {
  84. final DesiredCapabilities capabilities = new DesiredCapabilities();
  85. final LoggingPreferences loggingPreferences = loggingPreferencesFromProperties();
  86. setCapabilityIfNotNull(capabilities, CapabilityType.LOGGING_PREFS, loggingPreferences);
  87. return capabilities;
  88. }
  89. @Nullable
  90. private static LoggingPreferences loggingPreferencesFromProperties() {
  91. final LoggingPreferences loggingPreferences = new LoggingPreferences();
  92. enableLogTypeIfNonNull(loggingPreferences, LogType.BROWSER, WEBDRIVER_LOGGING_BROWSER.getSystemProperty());
  93. enableLogTypeIfNonNull(loggingPreferences, LogType.CLIENT, WEBDRIVER_LOGGING_CLIENT.getSystemProperty());
  94. enableLogTypeIfNonNull(loggingPreferences, LogType.DRIVER, WEBDRIVER_LOGGING_DRIVER.getSystemProperty());
  95. // return null if no logging preferences was set
  96. return loggingPreferences.getEnabledLogTypes().isEmpty() ? null : loggingPreferences;
  97. }
  98. private static void setCapabilityIfNotNull(final DesiredCapabilities capabilities,
  99. final String capabilityName,
  100. @Nullable final Object value) {
  101. if (value != null) {
  102. capabilities.setCapability(capabilityName, value);
  103. }
  104. }
  105. private static void enableLogTypeIfNonNull(final LoggingPreferences loggingPreferences,
  106. final String logType,
  107. @Nullable final String logLevel) {
  108. try {
  109. if (logLevel != null) {
  110. loggingPreferences.enable(logType, Level.parse(logLevel));
  111. }
  112. } catch (IllegalArgumentException ex) {
  113. log.warn("Invalid log level: {} for log type: {}", logLevel, logType);
  114. }
  115. }
  116. }