Skip to content

Commit

Permalink
Merge branch 'main' into IRIS-913-selective-regions-java
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel authored May 3, 2024
2 parents 121eb50 + 0c8a520 commit a24c011
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ internal class CreateSnapshotFromWebDriverIn
public string BuildUuid { get; }
[JsonProperty("diffingMethod")]
public DiffingMethod DiffingMethod { get; }
[JsonProperty("diffingOptions")]
public DiffingOptionsIn? DiffingOptions { get; set; }
[JsonProperty("ignoreRegions")]
public RegionIn[] IgnoreRegions { get; }
[JsonProperty("jobId")]
Expand Down Expand Up @@ -43,7 +45,8 @@ public CreateSnapshotFromWebDriverIn(
string? clipSelector,
string? suiteName,
string? testName,
FullPageConfigIn? fullPageConfig
FullPageConfigIn? fullPageConfig,
DiffingOptionsIn? diffingOptions
)
{
BuildUuid = buildUuid;
Expand All @@ -58,6 +61,7 @@ public CreateSnapshotFromWebDriverIn(
SuiteName = suiteName;
TestName = testName;
FullPageConfig = fullPageConfig;
DiffingOptions = diffingOptions;
}
}
}
30 changes: 30 additions & 0 deletions visual-dotnet/SauceLabs.Visual/GraphQL/DiffingOptionsIn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Newtonsoft.Json;

namespace SauceLabs.Visual.GraphQL
{
public class DiffingOptionsIn
{
[JsonProperty("content")]
public bool Content { get; set; }
[JsonProperty("dimensions")]
public bool Dimensions { get; set; }
[JsonProperty("position")]
public bool Position { get; set; }
[JsonProperty("structure")]
public bool Structure { get; set; }
[JsonProperty("style")]
public bool Style { get; set; }
[JsonProperty("visual")]
public bool Visual { get; set; }

public DiffingOptionsIn(bool defaultValue)
{
Content = defaultValue;
Dimensions = defaultValue;
Position = defaultValue;
Structure = defaultValue;
Style = defaultValue;
Visual = defaultValue;
}
}
}
19 changes: 19 additions & 0 deletions visual-dotnet/SauceLabs.Visual/GraphQL/RegionIn.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using OpenQA.Selenium;
using SauceLabs.Visual.Models;
using SauceLabs.Visual.Utils;

namespace SauceLabs.Visual.GraphQL
{
Expand All @@ -16,6 +17,8 @@ internal class RegionIn
public int Width { get; }
[JsonProperty("height")]
public int Height { get; }
[JsonProperty("diffingOptions")]
public DiffingOptionsIn? DiffingOptions { get; }

public RegionIn(int x, int y, int width, int height)
{
Expand All @@ -29,10 +32,26 @@ public RegionIn(string name, int x, int y, int width, int height) : this(x, y, w
Name = name;
}

public RegionIn(int x, int y, int width, int height, DiffingOptionsIn diffingOptions) : this(x, y, width, height)
{
DiffingOptions = diffingOptions;
}
public RegionIn(IWebElement input) : this(input.Location.X, input.Location.Y, input.Size.Width, input.Size.Height)
{ }

public RegionIn(IWebElement input, DiffingOptionsIn? options) : this(input.Location.X, input.Location.Y,
input.Size.Width, input.Size.Height)
{
DiffingOptions = options;
}

public RegionIn(IgnoreRegion input) : this(input.X, input.Y, input.Width, input.Height)
{ }

public RegionIn(SauceLabs.Visual.Models.Region input, DiffingOptionsIn? options) : this(input.X, input.Y, input.Width,
input.Height)
{
DiffingOptions = options;
}
}
}
3 changes: 2 additions & 1 deletion visual-dotnet/SauceLabs.Visual/Models/DiffingMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace SauceLabs.Visual.Models
public enum DiffingMethod
{
Simple,
Experimental
Experimental,
Balanced,
}
}
16 changes: 16 additions & 0 deletions visual-dotnet/SauceLabs.Visual/Models/DiffingOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace SauceLabs.Visual.Models
{
[Flags]
public enum DiffingOption
{
None = 0,
Content = 1 << 0,
Dimensions = 1 << 1,
Position = 1 << 2,
Structure = 1 << 3,
Style = 1 << 4,
Visual = 1 << 5,
}
}
18 changes: 18 additions & 0 deletions visual-dotnet/SauceLabs.Visual/Models/Region.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace SauceLabs.Visual.Models
{
public class Region
{
public int X { get; }
public int Y { get; }
public int Width { get; }
public int Height { get; }

public Region(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
}
}
99 changes: 99 additions & 0 deletions visual-dotnet/SauceLabs.Visual/Models/SelectiveRegion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using SauceLabs.Visual.GraphQL;
using SauceLabs.Visual.Utils;

namespace SauceLabs.Visual.Models
{
/// <summary>
/// SelectiveRegion describe a region where change kind can be filtered.
/// </summary>
[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public class SelectiveRegion
{
internal Region? Region { get; }
internal IWebElement? Element { get; }

internal DiffingOption? EnableOnly { get; }
internal DiffingOption? DisableOnly { get; }

private SelectiveRegion(IWebElement element, DiffingOption? enableOnly, DiffingOption? disableOnly)
{
Element = element;
EnableOnly = enableOnly;
DisableOnly = disableOnly;
}

private SelectiveRegion(Region region, DiffingOption? enableOnly, DiffingOption? disableOnly)
{
Region = region;
EnableOnly = enableOnly;
DisableOnly = disableOnly;
}

[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public static SelectiveRegion EnabledFor(IWebElement element)
{
return new SelectiveRegion(element, null, null);
}

[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public static SelectiveRegion EnabledFor(IWebElement element, DiffingOption flags)
{
return new SelectiveRegion(element, flags, null);
}

[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public static SelectiveRegion EnabledFor(Region region)
{
return new SelectiveRegion(region, DiffingOption.None, null);
}

[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public static SelectiveRegion EnabledFor(Region region, DiffingOption flags)
{
return new SelectiveRegion(region, flags, null);
}

[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public static SelectiveRegion DisabledFor(IWebElement element)
{
return new SelectiveRegion(element, null, DiffingOption.None);
}

[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public static SelectiveRegion DisabledFor(IWebElement element, DiffingOption flags)
{
return new SelectiveRegion(element, null, flags);
}

[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public static SelectiveRegion DisabledFor(Region region)
{
return new SelectiveRegion(region, null, DiffingOption.None);
}

[Obsolete("WARNING: This API is currently unstable. It may be changed at anytime")]
public static SelectiveRegion DisabledFor(Region region, DiffingOption flags)
{
return new SelectiveRegion(region, null, flags);
}

internal RegionIn ToRegionIn()
{
var diffingOptions = DiffingOptionsInHelper.CreateFromEnableOnlyDisable(EnableOnly, DisableOnly);
if (Region != null)
{
return new RegionIn(Region, diffingOptions);
}

if (Element != null)
{
return new RegionIn(Element, diffingOptions);
}

throw new VisualClientException("No Element nor Region has been passed");
}
}
}
2 changes: 1 addition & 1 deletion visual-dotnet/SauceLabs.Visual/SauceLabs.Visual.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<language>en-US</language>
<NeutralLanguage>en</NeutralLanguage>
<PackageId>SauceLabs.Visual</PackageId>
<Version>0.4.0</Version>
<Version>0.5.1</Version>
<Title>Sauce Labs Visual Binding</Title>
<PackageTags>saucelabs sauce labs visual testing screenshot capture dom</PackageTags>
<RepositoryUrl>https://github.com/saucelabs/visual-sdks</RepositoryUrl>
Expand Down
64 changes: 64 additions & 0 deletions visual-dotnet/SauceLabs.Visual/Utils/DiffingOptionsInHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using SauceLabs.Visual.GraphQL;
using SauceLabs.Visual.Models;

namespace SauceLabs.Visual.Utils
{
public static class DiffingOptionsInHelper
{
private static DiffingOptionsIn SetOptions(DiffingOptionsIn opts, DiffingOption flags, bool value)
{
if (flags.HasFlag(DiffingOption.Content))
{
opts.Content = value;
}

if (flags.HasFlag(DiffingOption.Dimensions))
{
opts.Dimensions = value;
}

if (flags.HasFlag(DiffingOption.Position))
{
opts.Position = value;
}

if (flags.HasFlag(DiffingOption.Structure))
{
opts.Structure = value;
}

if (flags.HasFlag(DiffingOption.Style))
{
opts.Style = value;
}

if (flags.HasFlag(DiffingOption.Visual))
{
opts.Visual = value;
}

return opts;
}

internal static DiffingOptionsIn? CreateFromEnableOnlyDisable(DiffingOption? enableOnly, DiffingOption? disableOnly)
{
if (enableOnly.HasValue)
{
var options = new DiffingOptionsIn(false);
options = SetOptions(options, enableOnly.Value, true);
return options;
}

if (disableOnly.HasValue)
{
var options = new DiffingOptionsIn(true);
options = SetOptions(options, disableOnly.Value, false);
return options;
}

return null;
}
}
}
15 changes: 15 additions & 0 deletions visual-dotnet/SauceLabs.Visual/VisualCheckOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public class VisualCheckOptions
public FullPageConfig? FullPageConfig { get; set; }
public string? ClipSelector { get; set; }

/// <summary>
/// <c>EnableOnly</c> allows to specify which changes to consider globaly.
/// </summary>
public DiffingOption? EnableOnly { get; set; }

/// <summary>
/// <c>Disable</c> allows to specify which changes to ignore globally.
/// </summary>
public DiffingOption? DisableOnly { get; set; }

/// <summary>
/// <c>Regions</c> allows to specify what kind of checks needs to be done in a specific region.
/// </summary>
public SelectiveRegion[]? Regions { get; set; }

Check warning on line 35 in visual-dotnet/SauceLabs.Visual/VisualCheckOptions.cs

View workflow job for this annotation

GitHub Actions / build

'SelectiveRegion' is obsolete: 'WARNING: This API is currently unstable. It may be changed at anytime'

Check warning on line 35 in visual-dotnet/SauceLabs.Visual/VisualCheckOptions.cs

View workflow job for this annotation

GitHub Actions / build

'SelectiveRegion' is obsolete: 'WARNING: This API is currently unstable. It may be changed at anytime'

/// <summary>
/// <c>SuiteName</c> manually set the SuiteName of the Test.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion visual-dotnet/SauceLabs.Visual/VisualClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private async Task<string> VisualCheckAsync(string name, VisualCheckOptions opti
var ignored = new List<RegionIn>();
ignored.AddRange(options.IgnoreRegions?.Select(r => new RegionIn(r)) ?? new List<RegionIn>());
ignored.AddRange(options.IgnoreElements?.Select(r => new RegionIn(r)) ?? new List<RegionIn>());
ignored.AddRange(options.Regions?.Select(r => r.ToRegionIn()) ?? new List<RegionIn>());

FullPageConfigIn? fullPageConfigIn = null;
if (options.FullPage == true)
Expand All @@ -163,7 +164,8 @@ private async Task<string> VisualCheckAsync(string name, VisualCheckOptions opti
clipSelector: options.ClipSelector,
suiteName: options.SuiteName,
testName: options.TestName,
fullPageConfig: fullPageConfigIn
fullPageConfig: fullPageConfigIn,
diffingOptions: DiffingOptionsInHelper.CreateFromEnableOnlyDisable(options.EnableOnly, options.DisableOnly)
))).EnsureValidResponse();
result.Result.Diffs.Nodes.ToList().ForEach(d => _screenshotIds.Add(d.Id));
return result.Result.Id;
Expand Down

0 comments on commit a24c011

Please sign in to comment.