From e0345d9e243b8b14d74949a925671b358d9093aa Mon Sep 17 00:00:00 2001 From: Javier Novoa Date: Fri, 27 Oct 2017 13:30:43 -0500 Subject: [PATCH] Add some unit test Make all Built-in AxeScriptProviders public Move AxeResult population logic to it self --- .../AxeBuilderTest.cs | 83 +++++++++++++++++++ .../Globant.Selenium.Axe.Test.csproj | 8 ++ .../Globant.Selenium.Axe.Test/packages.config | 2 + .../Globant.Selenium.Axe/AxeBuilder.cs | 12 +-- .../Globant.Selenium.Axe/AxeResult.cs | 28 +++++-- .../Globant.Selenium.Axe/AxeResultItem.cs | 4 +- .../FileAxeScriptProvider.cs | 2 +- .../IncludeExcludeManager.cs | 5 +- appveyor.yml | 6 +- 9 files changed, 126 insertions(+), 24 deletions(-) create mode 100644 Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs new file mode 100644 index 0000000..89428a0 --- /dev/null +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OpenQA.Selenium; +using Moq; +using OpenQA.Selenium.Firefox; + +namespace Globant.Selenium.Axe.Test +{ + [TestClass] + public class AxeBuilderTest + { + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void ThrowWhenDriverIsNull() + { + //arrange / act /assert + var axeBuilder = new AxeBuilder(null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void ThrowWhenOptionsAreNull() + { + //arrange + var driver = new Mock(); + + // act / assert + var axeBuilder = new AxeBuilder(driver.Object, null); + } + + [TestMethod] + public void ShouldExecuteAxeScript() + { + //arrange + var driver = new Mock(); + var jsExecutor =driver.As(); + var targetLocator = new Mock(); + + driver + .Setup(d => d.FindElements(It.IsAny())) + .Returns(new ReadOnlyCollection(new List(0))); + + driver.Setup(d => d.SwitchTo()).Returns(targetLocator.Object); + targetLocator.Setup(t => t.DefaultContent()).Returns(driver.Object); + + jsExecutor + .Setup(js => js.ExecuteAsyncScript(It.IsAny(), It.IsAny())) + .Returns(new + { + violations = new object[] { }, + passes = new object[] { }, + inapplicable = new object[] { }, + incomplete = new object[] { }, + timestamp = DateTimeOffset.Now, + url = "www.test.com", + }); + + var builder = new AxeBuilder(driver.Object); + var result = builder.Analyze(); + + result.Should().NotBeNull(); + result.Inapplicable.Should().NotBeNull(); + result.Incomplete.Should().NotBeNull(); + result.Passes.Should().NotBeNull(); + result.Violations.Should().NotBeNull(); + + result.Inapplicable.Length.Should().Be(0); + result.Incomplete.Length.Should().Be(0); + result.Passes.Length.Should().Be(0); + result.Violations.Length.Should().Be(0); + + driver.VerifyAll(); + targetLocator.VerifyAll(); + jsExecutor.VerifyAll(); + } + } +} diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/Globant.Selenium.Axe.Test.csproj b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/Globant.Selenium.Axe.Test.csproj index 5377c08..3637eca 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/Globant.Selenium.Axe.Test.csproj +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/Globant.Selenium.Axe.Test.csproj @@ -36,6 +36,9 @@ 4 + + ..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll + ..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.dll True @@ -44,11 +47,15 @@ ..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.Core.dll True + + ..\packages\Moq.4.7.142\lib\net45\Moq.dll + ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll True + @@ -70,6 +77,7 @@ + diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/packages.config b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/packages.config index bdff642..0c8fb78 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/packages.config +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/packages.config @@ -1,6 +1,8 @@  + + \ No newline at end of file diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeBuilder.cs b/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeBuilder.cs index 22c65c0..8334351 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeBuilder.cs +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeBuilder.cs @@ -52,17 +52,7 @@ private AxeResult Execute(string command, params object[] args) { object response = ((IJavaScriptExecutor)_webDriver).ExecuteAsyncScript(command, args); var jObject = JObject.FromObject(response); - - if (jObject.SelectToken("violations") == null) - throw new InvalidOperationException("The response from browser is not valid."); - - if (jObject.SelectToken("passes") == null) - throw new InvalidOperationException("The response from browser is not valid."); - - JToken violationsToken = jObject.SelectToken("violations"); - JToken passesToken = jObject.SelectToken("passes"); - - return new AxeResult(violationsToken, passesToken); + return new AxeResult(jObject); } /// diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResult.cs b/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResult.cs index 325ce71..89da37d 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResult.cs +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResult.cs @@ -1,17 +1,33 @@ -using Newtonsoft.Json.Linq; +using System; +using Newtonsoft.Json.Linq; using System.Collections.Generic; namespace Globant.Selenium.Axe { public class AxeResult { - public IReadOnlyList Violations { get; private set; } - public IReadOnlyList Passes { get; private set; } + public AxeResultItem[] Violations { get; } + public AxeResultItem[] Passes { get; } + public AxeResultItem[] Inapplicable { get; } + public AxeResultItem[] Incomplete { get; } + public DateTimeOffset? Timestamp { get; private set; } + public string Url { get; private set; } - public AxeResult(JToken violationsToken, JToken passesToken) + public AxeResult(JObject results) { - Violations = violationsToken.ToObject>(); - Passes = passesToken.ToObject>(); + JToken violationsToken = results.SelectToken("violations"); + JToken passesToken = results.SelectToken("passes"); + JToken inapplicableToken = results.SelectToken("inapplicable"); + JToken incompleteToken = results.SelectToken("incomplete"); + JToken timestampToken = results.SelectToken("timestamp"); + JToken urlToken = results.SelectToken("url"); + + Violations = violationsToken?.ToObject(); + Passes = passesToken?.ToObject(); + Inapplicable = inapplicableToken?.ToObject(); + Incomplete = incompleteToken?.ToObject(); + Timestamp = timestampToken?.ToObject(); + Url = urlToken?.ToObject(); } } } diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResultItem.cs b/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResultItem.cs index 43e161b..f85c084 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResultItem.cs +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResultItem.cs @@ -9,7 +9,7 @@ public class AxeResultItem public string Help { get; set; } public string HelpUrl { get; set; } public string Impact { get; set; } - public List Tags { get; set; } - public List Nodes { get; set; } + public string[] Tags { get; set; } + public AxeResultNode[] Nodes { get; set; } } } diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe/FileAxeScriptProvider.cs b/Globant.Selenium.Axe/Globant.Selenium.Axe/FileAxeScriptProvider.cs index 8566496..877c135 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe/FileAxeScriptProvider.cs +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe/FileAxeScriptProvider.cs @@ -11,7 +11,7 @@ public class FileAxeScriptProvider : IAxeScriptProvider { private readonly string _filePath; - internal FileAxeScriptProvider(string filePath) + public FileAxeScriptProvider(string filePath) { if (string.IsNullOrEmpty(filePath)) throw new ArgumentNullException(nameof(filePath)); diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe/IncludeExcludeManager.cs b/Globant.Selenium.Axe/Globant.Selenium.Axe/IncludeExcludeManager.cs index a7c963b..a77f8b4 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe/IncludeExcludeManager.cs +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe/IncludeExcludeManager.cs @@ -12,8 +12,7 @@ namespace Globant.Selenium.Axe /// public class IncludeExcludeManager { - private static readonly object syncObject = new object(); - private static readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings + private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { Formatting = Formatting.None, ContractResolver = new CamelCasePropertyNamesContractResolver(), @@ -83,7 +82,7 @@ public string GetFirstItemToInclude() /// This instance serialized in JSON format public string ToJson() { - return JsonConvert.SerializeObject(this, jsonSerializerSettings); + return JsonConvert.SerializeObject(this, JsonSerializerSettings); } private static void ValidateParameters(string[] selectors) diff --git a/appveyor.yml b/appveyor.yml index b2f815d..d5f2ce2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -27,4 +27,8 @@ deploy: api_key: secure: wO5eiWkOc3xv1qM1mChKx6weYq9h795U6u5NJg6kqwdSgsXZVCXjQJk6G05MuPKC skip_symbols: false - artifact: /.*\.nupkg/ \ No newline at end of file + artifact: /.*\.nupkg/ + +only_commits: + files: + - Globant.Selenium.Axe/ \ No newline at end of file