Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main changes to SauceOptions and SauceSession #209

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions dotnet/Simple.Sauce/Browser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
namespace Sauce.Bindings
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Safari;

namespace Sauce.Bindings
{
public class Browser
{
Expand All @@ -8,8 +14,9 @@ private Browser(string browserName)
}

public string Value { get; set; }

public static Browser Chrome => new Browser("chrome");
public static Browser Edge => new Browser("edge");
public static Browser Firefox => new Browser("firefox");
public static Browser Safari => new Browser("safari");
}
}
4 changes: 2 additions & 2 deletions dotnet/Simple.Sauce/DataCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ private DataCenter(string value)

public string Value { get; set; }

public static DataCenter UsWest => new DataCenter("https://ondemand.saucelabs.com/wd/hub");
public static DataCenter UsWest => new DataCenter("https://ondemand.us-west-1.saucelabs.com/wd/hub");

public static DataCenter UsEast => new DataCenter("https://ondemand.us-east-1.saucelabs.com/wd/hub");

public static object EuCental => new DataCenter("https://ondemand.eu-central-1.saucelabs.com/wd/hub");
public static DataCenter EuCental => new DataCenter("https://ondemand.eu-central-1.saucelabs.com/wd/hub");
}
}
17 changes: 9 additions & 8 deletions dotnet/Simple.Sauce/EdgeVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
{
public class EdgeVersion
{
private EdgeVersion(string value)
private EdgeVersion(string version)
{
Value = value;
Value = version;
}

public string Value { get; set; }

public static EdgeVersion Latest => new EdgeVersion("latest");
public static EdgeVersion _85 => new EdgeVersion("85.0");
public static EdgeVersion _84 => new EdgeVersion("84.0");
public static EdgeVersion _83 => new EdgeVersion("83.0");
public static EdgeVersion _81 => new EdgeVersion("81.0");
public static EdgeVersion _80 => new EdgeVersion("80.0");
public static EdgeVersion _79 => new EdgeVersion("79.0");
public static EdgeVersion _18 => new EdgeVersion("18.17763");
public static EdgeVersion _17 => new EdgeVersion("17.17134");

public static EdgeVersion _16 => new EdgeVersion("16.16299");

public static EdgeVersion _15 => new EdgeVersion("15.15063");

public static EdgeVersion _14 => new EdgeVersion("14.14393");
public static EdgeVersion _13 => new EdgeVersion("13.10586");
public static EdgeVersion Latest => new EdgeVersion("latest");

}
}
3 changes: 3 additions & 0 deletions dotnet/Simple.Sauce/ISauceRemoteDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ namespace Sauce.Bindings
public interface ISauceRemoteDriver : IJavaScriptExecutor, IWebDriver
{
IWebDriver CreateRemoteWebDriver(DriverOptions options);
IWebDriver CreateRemoteWebDriver(DriverOptions options, int commandTimeout);
IWebDriver CreateRemoteWebDriver(DataCenter dataCenter, DriverOptions options);
IWebDriver CreateRemoteWebDriver(DataCenter dataCenter, DriverOptions options, int commandTimeout);
}
}
2 changes: 2 additions & 0 deletions dotnet/Simple.Sauce/IncorrectSafariVersionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ namespace Sauce.Bindings
{
public class IncorrectSafariVersionException : Exception
{
public IncorrectSafariVersionException() { }
public IncorrectSafariVersionException(string invalidVersion) : base("Incorrect version specified => " + invalidVersion) { }
}
}
22 changes: 22 additions & 0 deletions dotnet/Simple.Sauce/JsonUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;

namespace Sauce.Bindings
{
public static class JsonUtils
{
public static JsonSerializerSettings SerializerSettings()
{
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new List<JsonConverter> { new StringEnumConverter() }
};

return settings;
}
}
}
5 changes: 4 additions & 1 deletion dotnet/Simple.Sauce/Platforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ private Platforms(string value)
}

public string Value { get; set; }

public static Platforms MacOsCatalina => new Platforms("macOS 10.15");
public static Platforms MacOsMojave => new Platforms("macOS 10.14");
public static Platforms MacOsHighSierra => new Platforms("macOS 10.13");
public static Platforms MacOsSierra => new Platforms("macOS 10.12");
public static Platforms MacOsxElCapitan => new Platforms("OS X 10.11");
public static Platforms MacOsxYosemite => new Platforms("OS X 10.10");
public static Platforms Windows10 => new Platforms("Windows 10");
public static Platforms Windows8_1 => new Platforms("Windows 8.1");
public static Platforms Windows8 => new Platforms("Windows 8");
public static Platforms Windows7 => new Platforms("Windows 7");
}
}
28 changes: 22 additions & 6 deletions dotnet/Simple.Sauce/SauceDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@ namespace Sauce.Bindings
{
public class SauceDriver : ISauceRemoteDriver
{
private IWebDriver _driver;
public IWebDriver CreateRemoteWebDriver(DriverOptions browserOptions)
private IWebDriver _driver;

public IWebDriver CreateRemoteWebDriver(DriverOptions options)
{
return CreateRemoteWebDriver(DataCenter.UsWest, options, 600);
}

public IWebDriver CreateRemoteWebDriver(DriverOptions options, int commandTimeout)
{
_driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"),
browserOptions.ToCapabilities(), TimeSpan.FromSeconds(600));
return CreateRemoteWebDriver(DataCenter.UsWest, options, commandTimeout);
}

public IWebDriver CreateRemoteWebDriver(DataCenter dataCenter, DriverOptions options)
{
return CreateRemoteWebDriver(dataCenter, options, 600);
}

public IWebDriver CreateRemoteWebDriver(DataCenter dataCenter, DriverOptions options, int commandTimeout)
{
_driver = new RemoteWebDriver(new Uri(dataCenter.Value),
options.ToCapabilities(), TimeSpan.FromSeconds(commandTimeout));
return _driver;
}

public object ExecuteScript(string script, params object[] args)
{
return ((IJavaScriptExecutor) _driver).ExecuteScript(script, args);
return ((IJavaScriptExecutor)_driver).ExecuteScript(script, args);
}

public object ExecuteAsyncScript(string script, params object[] args)
Expand All @@ -37,7 +53,7 @@ public ReadOnlyCollection<IWebElement> FindElements(By @by)

public void Dispose()
{
throw new NotImplementedException();
_driver?.Dispose();
}

public void Close()
Expand Down
147 changes: 91 additions & 56 deletions dotnet/Simple.Sauce/SauceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
Expand All @@ -21,30 +22,23 @@ public SauceOptions()
Timeout = new Timeout();
}

public EdgeOptions ConfiguredEdgeOptions { get; set; } = new EdgeOptions();
public ChromeOptions ConfiguredChromeOptions { get; private set; } = new ChromeOptions();
public SafariOptions ConfiguredSafariOptions { get; set; } = new SafariOptions();
public FirefoxOptions ConfiguredFirefoxOptions { get; set; } = new FirefoxOptions();
public Browser BrowserName { get; set; } = Browser.Chrome;
public String BrowserVersion { get; set; } = DEFAULT_BROWSER_VERSION;
public Platforms PlatformName { get; set; } = Platforms.Windows10;
public PageLoadStrategy PageLoadStrategy { get; set; }
public bool AcceptInsecureCerts { get; set; }
public bool SetWindowRect { get; set; }
public Timeout Timeout { get; set; }
[JsonIgnore]
public DriverOptions ConfiguredOptions { get; set; }

[JsonProperty("name")]
public string TestName { get; set; }

[JsonProperty("custom-data")]
public Dictionary<string, string> CustomData { get; set; }

public Proxy Proxy { get; set; }
public bool StrictFileInteractability { get; set; }
public UnhandledPromptBehavior UnhandledPromptBehavior { get; set; }
public Timeout Timeout { get; set; }
public bool SetWindowRect { get; set; }
public bool AvoidProxy { get; set; }
public string BuildName { get; set; }
public bool CapturePerformance { get; set; }
public string ChromedriverVersion { get; set; }
public Dictionary<string,string> CustomData { get; set; }
public bool ExtendedDebugging { get; set; }
public string IeDriverVersion { get; set; }
public string TestName { get; set; }
public string ParentTunnel { get; set; }
public string TunnelIdentifier { get; set; }
public Dictionary<string, object> Prerun { get; set; }
public int Priority { get; set; }
public TestVisibility TestVisibility { get; set; }
Expand All @@ -55,83 +49,124 @@ public SauceOptions()
public string SeleniumVersion { get; set; }
public IList<string> Tags { get; set; }
public string TimeZone { get; set; }
public string TunnelIdentifier { get; set; }
public bool VideoUploadOnPass { get; set; }

public void WithEdge()
public Dictionary<string, object> ToDictionary()
{
var options = new Dictionary<string, object>();

var addAuth = AddAuthentication();
foreach (var option in addAuth)
options.Add(option.Key, option.Value);

var addOptions = AddOptions();
foreach (var option in addOptions)
options.Add(option.Key, option.Value);

return options;
}

private Dictionary<string, object> AddAuthentication()
{
WithEdge(EdgeVersion.Latest);
var sauceUsername = Environment.GetEnvironmentVariable("SAUCE_USERNAME");
var sauceAccessKey = Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY");

return new Dictionary<string, object>
{
["username"] = sauceUsername,
["accessKey"] = sauceAccessKey
};
}

public void WithEdge(EdgeVersion edgeVersion)
private Dictionary<string, object> AddOptions()
{
if (edgeVersion == null)
throw new ArgumentNullException("Please supply a valid EdgeVersion. You suplied an invalid value=>" +
edgeVersion);
ConfiguredEdgeOptions.BrowserVersion = edgeVersion.Value;
ConfiguredEdgeOptions.PlatformName = DEFAULT_PLATFORM;
var options = JsonConvert.SerializeObject(this, JsonUtils.SerializerSettings());
var sauceOptions = JsonConvert.DeserializeObject<Dictionary<string, object>>(options, JsonUtils.SerializerSettings());

return sauceOptions;
}

public void WithChrome()
public SauceOptions WithChrome() => WithChrome(DEFAULT_BROWSER_VERSION, DEFAULT_PLATFORM);
public SauceOptions WithChrome(string version) => WithChrome(version, DEFAULT_PLATFORM);
public SauceOptions WithChrome(string version, string platform)
{
ConfiguredChromeOptions.BrowserVersion = DEFAULT_BROWSER_VERSION;
ConfiguredChromeOptions.PlatformName = DEFAULT_PLATFORM;
ConfiguredOptions = new ChromeOptions
{
BrowserVersion = version,
PlatformName = platform
};

return this;
}

public void WithChrome(string chromeVersion)
public SauceOptions WithEdge() => WithEdge(EdgeVersion.Latest, DEFAULT_PLATFORM);
public SauceOptions WithEdge(EdgeVersion version) => WithEdge(version, DEFAULT_PLATFORM);
public SauceOptions WithEdge(EdgeVersion edgeVersion, string platform)
{
ConfiguredChromeOptions.BrowserVersion = chromeVersion;
if (edgeVersion is null)
throw new ArgumentNullException("Please supply a valid Edge Version. You supplied an invalid value => " +
edgeVersion);

ConfiguredOptions = new EdgeOptions
{
BrowserVersion = edgeVersion.Value,
PlatformName = platform
};

return this;
}

public void WithSafari()
public SauceOptions WithFirefox() => WithFirefox(DEFAULT_BROWSER_VERSION, DEFAULT_PLATFORM);
public SauceOptions WithFirefox(string version) => WithFirefox(version, DEFAULT_PLATFORM);
public SauceOptions WithFirefox(string version, string platform)
{
WithSafari(DEFAULT_BROWSER_VERSION);
ConfiguredOptions = new FirefoxOptions
{
BrowserVersion = version,
PlatformName = platform
};

return this;
}

public void WithSafari(string safariVersion)
public SauceOptions WithSafari() => WithSafari(DEFAULT_BROWSER_VERSION);

public SauceOptions WithSafari(string version)
{
ConfiguredSafariOptions.BrowserVersion = safariVersion;
ConfiguredSafariOptions.PlatformName = MatchCorrectPlatformToBrowserVersion(safariVersion);
ConfiguredOptions = new SafariOptions
{
BrowserVersion = version,
PlatformName = MatchCorrectPlatformToBrowserVersion(version)
};

return this;
}

public string MatchCorrectPlatformToBrowserVersion(string safariBrowserVersion)
private string MatchCorrectPlatformToBrowserVersion(string safariBrowserVersion)
{
switch (safariBrowserVersion)
{
case "latest":
return Platforms.MacOsMojave.Value;
case "15.0":
return Platforms.MacOsCatalina.Value;
case "14.0":
case "12.0":
return Platforms.MacOsMojave.Value;
case "13.0":
return Platforms.MacOsHighSierra.Value;
case "12.1":
return Platforms.MacOsHighSierra.Value;
case "11.1":
return Platforms.MacOsHighSierra.Value;
case "11.0":
return Platforms.MacOsSierra.Value;
case "10.1":
return Platforms.MacOsSierra.Value;
case "9.0":
return Platforms.MacOsxElCapitan.Value;
case "10.0":
case "9.0":
return Platforms.MacOsxElCapitan.Value;
case "8.0":
return Platforms.MacOsxYosemite.Value;
default:
throw new IncorrectSafariVersionException();
throw new IncorrectSafariVersionException(safariBrowserVersion);
}
}

public void WithFirefox()
{
WithFirefox(DEFAULT_BROWSER_VERSION);
}

public void WithFirefox(string version)
{
ConfiguredFirefoxOptions.BrowserVersion = version;
ConfiguredFirefoxOptions.PlatformName = DEFAULT_PLATFORM;
}
}
}
Loading