Java Code Examples for org.openqa.selenium.Capabilities#getCapability()

The following examples show how to use org.openqa.selenium.Capabilities#getCapability() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoteWebDriver.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Override
public String toString() {
  Capabilities caps = getCapabilities();
  if (caps == null) {
    return super.toString();
  }

  // w3c name first
  Object platform = caps.getCapability(PLATFORM_NAME);
  if (!(platform instanceof String)) {
    platform = caps.getCapability(PLATFORM);
  }
  if (platform == null) {
    platform = "unknown";
  }

  return String.format(
      "%s: %s on %s (%s)",
      getClass().getSimpleName(),
      caps.getBrowserName(),
      platform,
      getSessionId());
}
 
Example 2
Source File: SeleniumCdpConnection.java    From selenium with Apache License 2.0 6 votes vote down vote up
public static Optional<URI> getCdpUri(Capabilities capabilities) {
  Object options = capabilities.getCapability("se:options");

  if (!(options instanceof Map)) {
    return Optional.empty();
  }

  Object cdp = ((Map<?, ?>) options).get("cdp");
  if (!(cdp instanceof String)) {
    return Optional.empty();
  }

  try {
    return Optional.of(new URI((String) cdp));
  } catch (URISyntaxException e) {
    return Optional.empty();
  }
}
 
Example 3
Source File: CapabilityHelpers.java    From java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Helper that is used for capability values retrieval.
 * Supports both prefixed W3C and "classic" capability names.
 *
 * @param <T>          The corresponding capability type.
 * @param caps         driver caps object
 * @param name         capability name
 * @param expectedType the expected capability type
 * @return The retrieved capability value or null if the cap either not present has an unexpected type
 */
@Nullable
public static <T> T getCapability(Capabilities caps, String name, Class<T> expectedType) {
    List<String> possibleNames = new ArrayList<>();
    possibleNames.add(name);
    if (!name.startsWith(APPIUM_PREFIX)) {
        possibleNames.add(APPIUM_PREFIX + name);
    }
    for (String capName : possibleNames) {
        if (caps.getCapability(capName) != null
                && expectedType.isAssignableFrom(caps.getCapability(capName).getClass())) {
            return expectedType.cast(caps.getCapability(capName));
        }
    }
    return null;
}
 
Example 4
Source File: AppiumDriver.java    From java-client with Apache License 2.0 5 votes vote down vote up
@Override
protected void startSession(Capabilities capabilities) {
    super.startSession(capabilities);
    // The RemoteWebDriver implementation overrides platformName
    // so we need to restore it back to the original value
    Object originalPlatformName = capabilities.getCapability(PLATFORM_NAME);
    Capabilities originalCaps = super.getCapabilities();
    if (originalPlatformName != null && originalCaps instanceof MutableCapabilities) {
        ((MutableCapabilities) super.getCapabilities()).setCapability(PLATFORM_NAME,
                originalPlatformName);
    }
}
 
Example 5
Source File: SafariOptions.java    From selenium with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a {@link SafariOptions} instance from given capabilities.
 * When the {@link #CAPABILITY} capability is set, all other capabilities will be ignored!
 *
 * @param capabilities Desired capabilities from which the options are derived.
 * @return SafariOptions
 * @throws WebDriverException If an error occurred during the reconstruction of the options
 */
public static SafariOptions fromCapabilities(Capabilities capabilities)
    throws WebDriverException {
  if (capabilities instanceof SafariOptions) {
    return (SafariOptions) capabilities;
  }
  Object cap = capabilities.getCapability(SafariOptions.CAPABILITY);
  if (cap instanceof SafariOptions) {
    return (SafariOptions) cap;
  } else if (cap instanceof Map) {
    return new SafariOptions(new MutableCapabilities(((Map<String, ?>) cap)));
  } else {
    return new SafariOptions(capabilities);
  }
}
 
Example 6
Source File: EdgeDriverInfo.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isSupporting(Capabilities capabilities) {
  return (BrowserType.EDGE.equals(capabilities.getBrowserName())
          || capabilities.getCapability("ms:edgeOptions") != null
          || capabilities.getCapability("edgeOptions") != null)
         &&
         (capabilities.getCapability(EdgeOptions.USE_CHROMIUM) == null
          || Objects.equals(capabilities.getCapability(EdgeOptions.USE_CHROMIUM), true));
}
 
Example 7
Source File: ChromiumDevToolsLocator.java    From selenium with Apache License 2.0 5 votes vote down vote up
public static Optional<URI> getReportedUri(String capabilityKey, Capabilities caps) {
  Object raw = caps.getCapability(capabilityKey);
  if (!(raw instanceof Map)) {
    LOG.fine("No capabilities for " + capabilityKey);
    return Optional.empty();
  }

  raw = ((Map<?, ?>) raw).get("debuggerAddress");
  if (!(raw instanceof String)) {
    LOG.fine("No debugger address");
    return Optional.empty();
  }

  int index = ((String) raw).lastIndexOf(':');
  if (index == -1 || index == ((String) raw).length() - 1) {
    LOG.fine("No index in " + raw);
    return Optional.empty();
  }

  try {
    URI uri = new URI(String.format("http://%s", raw));
    LOG.fine("URI found: " + uri);
    return Optional.of(uri);
  } catch (URISyntaxException e) {
    LOG.warning("Unable to creeate URI from: " + raw);
    return Optional.empty();
  }
}
 
Example 8
Source File: EdgeHtmlDriverInfo.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isSupporting(Capabilities capabilities) {
  return (BrowserType.EDGE.equals(capabilities.getBrowserName())
          || capabilities.getCapability("ms:edgeOptions") != null
          || capabilities.getCapability("edgeOptions") != null)
         &&
         Objects.equals(capabilities.getCapability(EdgeHtmlOptions.USE_CHROMIUM), false);
}
 
Example 9
Source File: TestUtilities.java    From selenium with Apache License 2.0 5 votes vote down vote up
public static int getChromeVersion(WebDriver driver) {
  if (!(driver instanceof HasCapabilities)) {
    // Driver does not support capabilities -- not a chromedriver at all.
    return 0;
  }
  Capabilities caps = ((HasCapabilities) driver).getCapabilities();
  String chromedriverVersion = (String) caps.getCapability("chrome.chromedriverVersion");
  if (chromedriverVersion == null) {
    Object chrome = caps.getCapability("chrome");
    if (chrome != null) {
      chromedriverVersion = (String) ((Map<?,?>) chrome).get("chromedriverVersion");
    }
  }
  if (chromedriverVersion != null) {
    String[] versionMajorMinor = chromedriverVersion.split("\\.", 2);
    if (versionMajorMinor.length > 1) {
      try {
        return Integer.parseInt(versionMajorMinor[0]);
      } catch (NumberFormatException e) {
        // First component of the version is not a number -- not a chromedriver.
        return 0;
      }
    }
  }
  return 0;

}
 
Example 10
Source File: DevToolsProvider.java    From selenium with Apache License 2.0 5 votes vote down vote up
private String getCdpUrl(Capabilities caps) {
  Object options = caps.getCapability("se:options");
  if (!(options instanceof Map)) {
    return null;
  }

  Object cdp = ((Map<?, ?>) options).get("cdp");
  return cdp == null ? null : String.valueOf(cdp);
}
 
Example 11
Source File: ConfigurationUtils.java    From Quantum with MIT License 5 votes vote down vote up
/**
 * Checks if is device.
 *
 * param driver capabilities
 * @return true, if is device
 */
public static boolean isDevice(Capabilities caps){
    //first check if driver is a mobile device:
    if (isDesktopBrowser(caps))
        return false;
    return caps.getCapability("deviceName") != null;
}
 
Example 12
Source File: ConsoleUtils.java    From Quantum with MIT License 5 votes vote down vote up
public static String getPlatformName(Capabilities caps) {
	return caps.getCapability("platformName") == null ? 
			(ConfigurationUtils.isDesktopBrowser(caps) ?
					("ANY".equals(caps.getCapability("platform")) 
						? "Desktop" : caps.getCapability("platform") + "") 
					: (caps.getCapability("os") == null ? "Device" : caps.getCapability("os") +"")) 
			: caps.getCapability("platformName") + "";
}
 
Example 13
Source File: ConsoleUtils.java    From Quantum with MIT License 5 votes vote down vote up
public static String getDeviceName(Capabilities caps) {
	return caps.getCapability("deviceName") == null ? 
			(caps.getCapability("deviceDbName") == null ?
					(caps.getCapability("description") == null ? "" : caps.getCapability("description") + "")
					: caps.getCapability("deviceDbName") + "")
			: caps.getCapability("deviceName") + "";
}
 
Example 14
Source File: SeleniumGrid.java    From Selenium-Foundation with Apache License 2.0 5 votes vote down vote up
/**
 * Add supported personalities of the specified Grid node.
 * 
 * @param config {@link SeleniumConfig} object
 * @param hubUrl {@link URL} of Grid hub
 * @param nodeEndpoint node endpoint
 * @throws IOException if an I/O error occurs
 */
@SuppressWarnings({"unchecked", "rawtypes"})
private void addNodePersonalities(SeleniumConfig config, URL hubUrl, String nodeEndpoint) throws IOException {
    for (Capabilities capabilities : GridUtility.getNodeCapabilities(config, hubUrl, nodeEndpoint)) {
        Map<String, Object> req = (Map<String, Object>) capabilities.getCapability("request");
        List<Map> capsList = (List<Map>) req.get("capabilities");
        if (capsList == null) {
            Map<String, Object> conf = (Map<String, Object>) req.get("configuration");
            capsList = (List<Map>) conf.get("capabilities");
        }
        for (Map<String, Object> capsItem : capsList) {
            personalities.put((String) capsItem.get("browserName"), config.toJson(capsItem));
        }
    }
}
 
Example 15
Source File: Device.java    From carina with Apache License 2.0 4 votes vote down vote up
public Device(Capabilities capabilities) {
    // 1. read from CONFIG and specify if any: capabilities.deviceName, capabilities.device (browserstack notation)
    // 2. read from capabilities object and set if if it is not null
    String deviceName = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_NAME);
    if (!R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_BROWSERSTACK_NAME).isEmpty()) {
        deviceName = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_BROWSERSTACK_NAME);
    }
    if (capabilities.getCapability("deviceName") != null) {
        deviceName = capabilities.getCapability("deviceName").toString();
    }
    if (capabilities.getCapability("deviceModel") != null) {
        // deviceModel is returned from capabilities with name of device for local appium runs
        deviceName = capabilities.getCapability("deviceModel").toString();
    }
    setName(deviceName);

    // TODO: should we register default device type as phone?
    String deviceType = SpecialKeywords.PHONE;
    if (!R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_TYPE).isEmpty()) {
        deviceType = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_TYPE);
    }
    if (capabilities.getCapability("deviceType") != null) {
        deviceType = capabilities.getCapability("deviceType").toString();
    }
    setType(deviceType);

    setOs(Configuration.getPlatform());

    String platformVersion = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_PLATFORM_VERSION);
    if (!R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_BROWSERSTACK_PLATFORM_VERSION).isEmpty()) {
        platformVersion = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_BROWSERSTACK_PLATFORM_VERSION);
    }
    if (capabilities.getCapability("platformVersion") != null) {
        platformVersion = capabilities.getCapability("platformVersion").toString();
    }

    setOsVersion(platformVersion);

    String deviceUdid = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_UDID);
    if (capabilities.getCapability("udid") != null) {
        deviceUdid = capabilities.getCapability("udid").toString();
    }

    setUdid(deviceUdid);
    
    String proxyPort = R.CONFIG.get(SpecialKeywords.MOBILE_PROXY_PORT);
    if (capabilities.getCapability(Parameter.PROXY_PORT.getKey()) != null) {
        proxyPort = capabilities.getCapability(Parameter.PROXY_PORT.getKey()).toString();
    }

    setProxyPort(proxyPort);
    
    setCapabilities(capabilities);
}
 
Example 16
Source File: InternetExplorerDriverInfo.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isSupporting(Capabilities capabilities) {
  return BrowserType.IE.equals(capabilities.getBrowserName()) ||
         capabilities.getCapability("se:ieOptions") != null;
}
 
Example 17
Source File: ConsoleUtils.java    From Quantum with MIT License 4 votes vote down vote up
public static String getTestName(Capabilities caps) {
	return caps.getCapability("scriptName") == null ? "" : caps.getCapability("scriptName") + "";
}
 
Example 18
Source File: ConsoleUtils.java    From Quantum with MIT License 4 votes vote down vote up
public static String getVersion(Capabilities caps) {
	return caps.getCapability("platformVersion") == null ? 
			(caps.getVersion() == null || caps.getVersion().isEmpty() ?
					caps.getBrowserName() : caps.getVersion())
			: caps.getCapability("platformVersion") + "";
}
 
Example 19
Source File: SeleniumDriver.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 4 votes vote down vote up
public String getPlatformName() {
    Platform platform = null;
    String mode = Control.exe.getExecSettings().getRunSettings().getExecutionMode();
    boolean isLocal = mode.equalsIgnoreCase("Local");
    if (runContext.Platform.equals(Platform.ANY) || runContext.Platform.equals(Platform.getCurrent())) {
        Capabilities cap;
        if (driver instanceof ExtendedHtmlUnitDriver) {
            cap = ((ExtendedHtmlUnitDriver) driver).getCapabilities();
        } else if (driver instanceof MobileDriver) {
            cap = ((RemoteWebDriver) driver).getCapabilities();
            Object platf = cap.getCapability("platformName");
            if (platf != null && !platf.toString().isEmpty()) {
                return platf.toString();
            } else {
                return (driver instanceof AndroidDriver) ? "Android" : "IOS";
            }
        } else if (driver instanceof EmptyDriver) {
            return Platform.getCurrent().name();
        } else {
            cap = ((RemoteWebDriver) driver).getCapabilities();
        }
        platform = cap.getPlatform();
        if (isLocal) {
            platform = Platform.getCurrent();
        }
        if (platform.name().equals(Platform.VISTA.name()) || platform.name().equals(Platform.XP.name())
                || platform.name().equals(Platform.WINDOWS.name()) || platform.name().equals(Platform.WIN8.name())) {
            switch (platform.getMajorVersion() + "." + platform.getMinorVersion()) {
                case "5.1":
                    return "XP";
                case "6.0":
                    return "VISTA";
                case "6.1":
                    return "WIN7";
                case "6.2":
                    return "WIN8";
                case "6.3":
                    return "WIN8.1";
                default:
                    return platform.name();
            }
        } else {
            return platform.toString();
        }
    } else if (runContext.PlatformValue.equals("WINDOWS")) {
        return "WIN";
    } else {
        if (isLocal) {
            platform = Platform.getCurrent();
            return platform.toString();
        }
        return runContext.PlatformValue;
    }
}
 
Example 20
Source File: SeleniumDriver.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 4 votes vote down vote up
public String getBrowserVersion() {
    if (runContext.BrowserVersion.equalsIgnoreCase("default")) {
        if (!runContext.Browser.equals((Browser.HtmlUnit))) {
            String browser_version;
            Capabilities cap;
            if (driver instanceof ExtendedHtmlUnitDriver) {
                cap = ((ExtendedHtmlUnitDriver) driver).getCapabilities();
            } else if (driver instanceof MobileDriver) {
                cap = ((RemoteWebDriver) driver).getCapabilities();
                Object pV = cap.getCapability("platformVersion");
                return pV == null ? "" : pV.toString();
            } else if (driver instanceof RemoteWebDriver) {
                cap = ((RemoteWebDriver) driver).getCapabilities();
            } else {
                return runContext.BrowserVersion;
            }
            String browsername = cap.getBrowserName();
            // This block to find out IE Version number
            if ("internet explorer".equalsIgnoreCase(browsername)) {
                String uAgent = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;");
                // uAgent return as "MSIE 8.0 Windows" for IE8
                if (uAgent.contains("MSIE") && uAgent.contains("Windows")) {
                    browser_version = uAgent.substring(uAgent.indexOf("MSIE") + 5, uAgent.indexOf("Windows") - 2);
                } else if (uAgent.contains("Trident/7.0")) {
                    browser_version = "11.0";
                } else {
                    browser_version = "0.0";
                }
            } else {
                // Browser version for Firefox and Chrome
                browser_version = cap.getVersion();
            }
            if (browser_version.contains(".") && browser_version.length() > 5) {
                return browser_version.substring(0, browser_version.indexOf("."));
            } else {
                return browser_version;
            }
        }
    }
    return runContext.BrowserVersion;
}