using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace OpenQA.Selenium.Appium
{
///
/// Generic driver options
///
public class AppiumOptions : DriverOptions
{
private const string VendorPrefix = "appium";
private const string AutomationNameOption = "appium:automationName";
private const string DeviceNameOption = "appium:deviceName";
private const string AppOption = "appium:app";
private const string PlatformVersionOption = "appium:platformVersion";
private readonly Dictionary additionalAppiumOptions = new Dictionary();
///
/// Initializes a new instance of the class.
///
public AppiumOptions() : base()
{
AddKnownCapabilityName(AutomationNameOption, "AutomationName property");
AddKnownCapabilityName(DeviceNameOption, "DeviceName property");
AddKnownCapabilityName(AppOption, "Application property");
AddKnownCapabilityName(PlatformVersionOption, "PlatformVersion property");
AddKnownCapabilityName("app", "Application property");
}
///
/// Gets or sets the AutoamtionName of the Appium browser's (e.g. Appium, Selendroid and so on) setting.
///
public string AutomationName { get; set; }
///
/// Gets or sets the DeviceName of the Appium browser's (e.g. Pixel 3XL, Galaxy S20 and so on) setting.
///
public string DeviceName { get; set; }
///
/// Gets or sets the Capability name used for the application setting.
///
public string App { get; set; }
///
/// Gets or sets the PlatformVersion of the Appium browser's (e.g. 10, 11 and so on) setting.
///
public string PlatformVersion { get; set; }
///
/// Gets or sets the Browser name of the Appium browser's (e.g. Chrome, Safari and so on) setting.
///
public new string BrowserName
{
get { return base.BrowserName; }
set { base.BrowserName = value; }
}
///
/// Provides a means to add additional capabilities not yet added as type safe options
/// for the Appium driver.
///
/// The name of the capability to add.
/// The value of the capability to add.
///
/// thrown when attempting to add a capability for which there is already a type safe option, or
/// when is or the empty string.
///
/// Calling
/// where has already been added will overwrite the
/// existing value with the new value in .
/// Calling this method adds capabilities to the Appium-specific options object passed to
/// webdriver executable.
public void AddAdditionalAppiumOption(string optionName, object optionValue)
{
string name = optionName.Contains(':') ? optionName : $"{VendorPrefix}:{optionName}";
ValidateCapabilityName(name);
additionalAppiumOptions[name] = optionValue;
}
///
/// This method is overridden to provide a clear exception indicating that
/// the method should be used for adding additional options.
///
/// The name of the additional option.
/// The value of the additional option.
///
/// Thrown to indicate that should be used for adding additional options.
///
public override void AddAdditionalOption(string optionName, object optionValue)
{
throw new NotImplementedException("Use the AddAdditionalAppiumOption method for adding additional options");
}
///
/// Turn the capabilities into a desired capability
///
/// A desired capability
public override ICapabilities ToCapabilities()
{
var capabilities = GenerateDesiredCapabilities(true);
foreach (var option in BuildAppiumOptionsDictionary())
{
capabilities.SetCapability(option.Key, option.Value);
}
return capabilities;
}
///
/// Builds a dictionary containing known Appium options with their corresponding values.
///
/// A dictionary representing known Appium options and their values.
protected virtual Dictionary BuildAppiumKnownOptionsDictionary()
{
Dictionary knownOptions = new Dictionary();
if (!string.IsNullOrEmpty(App))
{
knownOptions[AppOption] = App;
}
if (!string.IsNullOrEmpty(AutomationName))
{
knownOptions[AutomationNameOption] = AutomationName;
}
if (!string.IsNullOrEmpty(DeviceName))
{
knownOptions[DeviceNameOption] = DeviceName;
}
if (!string.IsNullOrEmpty(PlatformVersion))
{
knownOptions[PlatformVersionOption] = PlatformVersion;
}
return knownOptions;
}
private Dictionary BuildAppiumOptionsDictionary()
{
var appiumOptions = BuildAppiumKnownOptionsDictionary();
foreach (KeyValuePair pair in additionalAppiumOptions)
{
appiumOptions.Add(pair.Key, pair.Value);
}
return appiumOptions;
}
///
/// Converts the current instance of to a ReadOnlyDictionary.
///
/// A ReadOnlyDictionary representation of the .
public IDictionary ToDictionary()
{
IWritableCapabilities writeable = GenerateDesiredCapabilities(true);
var baseDict = (writeable.AsReadOnly() as ReadOnlyDesiredCapabilities).ToDictionary();
return MergeOptionsDictionary(baseDict);
}
///
/// Merges the provided dictionary with the dictionary.
///
/// The base dictionary.
/// A ReadOnlyDictionary representing the merged dictionary.
private IDictionary MergeOptionsDictionary(IDictionary baseDict)
{
Dictionary appiumOptionsDict = BuildAppiumOptionsDictionary();
var mergedDict = appiumOptionsDict.Concat(baseDict).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
return new ReadOnlyDictionary(mergedDict);
}
}
}