Skip to content

Commit

Permalink
Add some unit test
Browse files Browse the repository at this point in the history
Make all Built-in AxeScriptProviders public
Move AxeResult population logic to it self
  • Loading branch information
javnov committed Oct 27, 2017
1 parent 8aef032 commit e0345d9
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 24 deletions.
83 changes: 83 additions & 0 deletions Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -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<IWebDriver>();

// act / assert
var axeBuilder = new AxeBuilder(driver.Object, null);
}

[TestMethod]
public void ShouldExecuteAxeScript()
{
//arrange
var driver = new Mock<IWebDriver>();
var jsExecutor =driver.As<IJavaScriptExecutor>();
var targetLocator = new Mock<ITargetLocator>();

driver
.Setup(d => d.FindElements(It.IsAny<By>()))
.Returns(new ReadOnlyCollection<IWebElement>(new List<IWebElement>(0)));

driver.Setup(d => d.SwitchTo()).Returns(targetLocator.Object);
targetLocator.Setup(t => t.DefaultContent()).Returns(driver.Object);

jsExecutor
.Setup(js => js.ExecuteAsyncScript(It.IsAny<string>(), It.IsAny<object[]>()))
.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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="FluentAssertions, Version=4.19.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.dll</HintPath>
<Private>True</Private>
Expand All @@ -44,11 +47,15 @@
<HintPath>..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.7.142.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.7.142\lib\net45\Moq.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -70,6 +77,7 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="AxeBuilderTest.cs" />
<Compile Include="IntegrationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.2.1" targetFramework="net45" />
<package id="FluentAssertions" version="4.19.2" targetFramework="net45" />
<package id="Moq" version="4.7.142" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="Selenium.WebDriver" version="2.53.0" targetFramework="net45" />
</packages>
12 changes: 1 addition & 11 deletions Globant.Selenium.Axe/Globant.Selenium.Axe/AxeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
28 changes: 22 additions & 6 deletions Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResult.cs
Original file line number Diff line number Diff line change
@@ -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<AxeResultItem> Violations { get; private set; }
public IReadOnlyList<AxeResultItem> 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<List<AxeResultItem>>();
Passes = passesToken.ToObject<List<AxeResultItem>>();
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<AxeResultItem[]>();
Passes = passesToken?.ToObject<AxeResultItem[]>();
Inapplicable = inapplicableToken?.ToObject<AxeResultItem[]>();
Incomplete = incompleteToken?.ToObject<AxeResultItem[]>();
Timestamp = timestampToken?.ToObject<DateTimeOffset>();
Url = urlToken?.ToObject<string>();
}
}
}
4 changes: 2 additions & 2 deletions Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResultItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AxeResultItem
public string Help { get; set; }
public string HelpUrl { get; set; }
public string Impact { get; set; }
public List<string> Tags { get; set; }
public List<AxeResultNode> Nodes { get; set; }
public string[] Tags { get; set; }
public AxeResultNode[] Nodes { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace Globant.Selenium.Axe
/// </summary>
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(),
Expand Down Expand Up @@ -83,7 +82,7 @@ public string GetFirstItemToInclude()
/// <returns>This instance serialized in JSON format</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, jsonSerializerSettings);
return JsonConvert.SerializeObject(this, JsonSerializerSettings);
}

private static void ValidateParameters(string[] selectors)
Expand Down
6 changes: 5 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ deploy:
api_key:
secure: wO5eiWkOc3xv1qM1mChKx6weYq9h795U6u5NJg6kqwdSgsXZVCXjQJk6G05MuPKC
skip_symbols: false
artifact: /.*\.nupkg/
artifact: /.*\.nupkg/

only_commits:
files:
- Globant.Selenium.Axe/

0 comments on commit e0345d9

Please sign in to comment.