From 9ca65c186279629278aa4c5f49732233ac3238b4 Mon Sep 17 00:00:00 2001 From: Felix P Date: Mon, 18 Mar 2024 14:49:22 +0100 Subject: [PATCH] Add support for SAUCE_USERNAME and SAUCE_ACCESS_KEY --- visual-dotnet/SauceLabs.Visual/Region.cs | 14 +++++++++++ .../SauceLabs.Visual/VisualClient.cs | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/visual-dotnet/SauceLabs.Visual/Region.cs b/visual-dotnet/SauceLabs.Visual/Region.cs index 33e87cfe..d5c22392 100644 --- a/visual-dotnet/SauceLabs.Visual/Region.cs +++ b/visual-dotnet/SauceLabs.Visual/Region.cs @@ -43,6 +43,20 @@ public static Region FromName(string name) }; } + public static Region FromWebDriverUrl(string url) + { + var parsedUrl = new Uri(url); + return parsedUrl.Host switch + { + "ondemand.saucelabs.com" => UsWest1, + "ondemand.us-west-1.saucelabs.com" => UsWest1, + "ondemand.eu-central-1.saucelabs.com" => EuCentral1, + "ondemand.us-east-4.saucelabs.com" => UsEast4, + "ondemand.staging.saucelabs.net" => Staging, + _ => throw new VisualClientException($"{parsedUrl.Host} is not recognized as Sauce Labs region") + }; + } + public static Region UsWest1 => new Region("https://api.us-west-1.saucelabs.com/v1/visual/graphql"); public static Region UsEast4 => new Region("https://api.us-east-4.saucelabs.com/v1/visual/graphql"); public static Region EuCentral1 => new Region("https://api.eu-central-1.saucelabs.com/v1/visual/graphql"); diff --git a/visual-dotnet/SauceLabs.Visual/VisualClient.cs b/visual-dotnet/SauceLabs.Visual/VisualClient.cs index 252acc74..d55e9bd6 100644 --- a/visual-dotnet/SauceLabs.Visual/VisualClient.cs +++ b/visual-dotnet/SauceLabs.Visual/VisualClient.cs @@ -25,6 +25,25 @@ public class VisualClient : IDisposable private readonly bool _externalBuild; public bool CaptureDom { get; set; } = false; + /// + /// Creates a new instance of VisualClient + /// + /// the instance of the WebDriver session + /// the Sauce Labs region to connect to + public VisualClient(WebDriver wd, Region region) : this(wd, region, Environment.GetEnvironmentVariable("SAUCE_USERNAME"), Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY")) + { + } + + /// + /// Creates a new instance of VisualClient + /// + /// the instance of the WebDriver session + /// the Sauce Labs region to connect to + /// the options of the build creation + public VisualClient(WebDriver wd, Region region, CreateBuildOptions buildOptions): this(wd, region, Environment.GetEnvironmentVariable("SAUCE_USERNAME"), Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY"), buildOptions) + { + } + /// /// Creates a new instance of VisualClient /// @@ -46,6 +65,11 @@ public class VisualClient : IDisposable /// the options of the build creation public VisualClient(WebDriver wd, Region region, string username, string accessKey, CreateBuildOptions buildOptions) { + if (string.IsNullOrWhiteSpace(username) || string.IsNullOrEmpty(accessKey)) + { + throw new VisualClientException("Username or Access Key not set"); + } + _api = new VisualApi(wd, region, username, accessKey); _sessionId = wd.SessionId.ToString(); _jobId = wd.Capabilities.HasCapability("jobUuid") ? wd.Capabilities.GetCapability("jobUuid").ToString() : _sessionId;