Skip to content

Commit

Permalink
Add support for SAUCE_USERNAME and SAUCE_ACCESS_KEY
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel committed Mar 18, 2024
1 parent d8792f6 commit 9ca65c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions visual-dotnet/SauceLabs.Visual/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
24 changes: 24 additions & 0 deletions visual-dotnet/SauceLabs.Visual/VisualClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ public class VisualClient : IDisposable
private readonly bool _externalBuild;
public bool CaptureDom { get; set; } = false;

/// <summary>
/// Creates a new instance of <c>VisualClient</c>
/// </summary>
/// <param name="wd">the instance of the WebDriver session</param>
/// <param name="region">the Sauce Labs region to connect to</param>
public VisualClient(WebDriver wd, Region region) : this(wd, region, Environment.GetEnvironmentVariable("SAUCE_USERNAME"), Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY"))
{
}

/// <summary>
/// Creates a new instance of <c>VisualClient</c>
/// </summary>
/// <param name="wd">the instance of the WebDriver session</param>
/// <param name="region">the Sauce Labs region to connect to</param>
/// <param name="buildOptions">the options of the build creation</param>
public VisualClient(WebDriver wd, Region region, CreateBuildOptions buildOptions): this(wd, region, Environment.GetEnvironmentVariable("SAUCE_USERNAME"), Environment.GetEnvironmentVariable("SAUCE_ACCESS_KEY"), buildOptions)
{
}

/// <summary>
/// Creates a new instance of <c>VisualClient</c>
/// </summary>
Expand All @@ -46,6 +65,11 @@ public class VisualClient : IDisposable
/// <param name="buildOptions">the options of the build creation</param>
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<WebDriver>(wd, region, username, accessKey);
_sessionId = wd.SessionId.ToString();
_jobId = wd.Capabilities.HasCapability("jobUuid") ? wd.Capabilities.GetCapability("jobUuid").ToString() : _sessionId;
Expand Down

0 comments on commit 9ca65c1

Please sign in to comment.