Skip to content

Commit e0345d9

Browse files
committed
Add some unit test
Make all Built-in AxeScriptProviders public Move AxeResult population logic to it self
1 parent 8aef032 commit e0345d9

File tree

9 files changed

+126
-24
lines changed

9 files changed

+126
-24
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using FluentAssertions;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using OpenQA.Selenium;
10+
using Moq;
11+
using OpenQA.Selenium.Firefox;
12+
13+
namespace Globant.Selenium.Axe.Test
14+
{
15+
[TestClass]
16+
public class AxeBuilderTest
17+
{
18+
[TestMethod]
19+
[ExpectedException(typeof(ArgumentNullException))]
20+
public void ThrowWhenDriverIsNull()
21+
{
22+
//arrange / act /assert
23+
var axeBuilder = new AxeBuilder(null);
24+
}
25+
26+
[TestMethod]
27+
[ExpectedException(typeof(ArgumentNullException))]
28+
public void ThrowWhenOptionsAreNull()
29+
{
30+
//arrange
31+
var driver = new Mock<IWebDriver>();
32+
33+
// act / assert
34+
var axeBuilder = new AxeBuilder(driver.Object, null);
35+
}
36+
37+
[TestMethod]
38+
public void ShouldExecuteAxeScript()
39+
{
40+
//arrange
41+
var driver = new Mock<IWebDriver>();
42+
var jsExecutor =driver.As<IJavaScriptExecutor>();
43+
var targetLocator = new Mock<ITargetLocator>();
44+
45+
driver
46+
.Setup(d => d.FindElements(It.IsAny<By>()))
47+
.Returns(new ReadOnlyCollection<IWebElement>(new List<IWebElement>(0)));
48+
49+
driver.Setup(d => d.SwitchTo()).Returns(targetLocator.Object);
50+
targetLocator.Setup(t => t.DefaultContent()).Returns(driver.Object);
51+
52+
jsExecutor
53+
.Setup(js => js.ExecuteAsyncScript(It.IsAny<string>(), It.IsAny<object[]>()))
54+
.Returns(new
55+
{
56+
violations = new object[] { },
57+
passes = new object[] { },
58+
inapplicable = new object[] { },
59+
incomplete = new object[] { },
60+
timestamp = DateTimeOffset.Now,
61+
url = "www.test.com",
62+
});
63+
64+
var builder = new AxeBuilder(driver.Object);
65+
var result = builder.Analyze();
66+
67+
result.Should().NotBeNull();
68+
result.Inapplicable.Should().NotBeNull();
69+
result.Incomplete.Should().NotBeNull();
70+
result.Passes.Should().NotBeNull();
71+
result.Violations.Should().NotBeNull();
72+
73+
result.Inapplicable.Length.Should().Be(0);
74+
result.Incomplete.Length.Should().Be(0);
75+
result.Passes.Length.Should().Be(0);
76+
result.Violations.Length.Should().Be(0);
77+
78+
driver.VerifyAll();
79+
targetLocator.VerifyAll();
80+
jsExecutor.VerifyAll();
81+
}
82+
}
83+
}

Globant.Selenium.Axe/Globant.Selenium.Axe.Test/Globant.Selenium.Axe.Test.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<WarningLevel>4</WarningLevel>
3737
</PropertyGroup>
3838
<ItemGroup>
39+
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
40+
<HintPath>..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll</HintPath>
41+
</Reference>
3942
<Reference Include="FluentAssertions, Version=4.19.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
4043
<HintPath>..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.dll</HintPath>
4144
<Private>True</Private>
@@ -44,11 +47,15 @@
4447
<HintPath>..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.Core.dll</HintPath>
4548
<Private>True</Private>
4649
</Reference>
50+
<Reference Include="Moq, Version=4.7.142.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
51+
<HintPath>..\packages\Moq.4.7.142\lib\net45\Moq.dll</HintPath>
52+
</Reference>
4753
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
4854
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
4955
<Private>True</Private>
5056
</Reference>
5157
<Reference Include="System" />
58+
<Reference Include="System.Configuration" />
5259
<Reference Include="System.Drawing" />
5360
<Reference Include="System.Xml" />
5461
<Reference Include="System.Xml.Linq" />
@@ -70,6 +77,7 @@
7077
</Otherwise>
7178
</Choose>
7279
<ItemGroup>
80+
<Compile Include="AxeBuilderTest.cs" />
7381
<Compile Include="IntegrationTests.cs" />
7482
<Compile Include="Properties\AssemblyInfo.cs" />
7583
</ItemGroup>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Castle.Core" version="4.2.1" targetFramework="net45" />
34
<package id="FluentAssertions" version="4.19.2" targetFramework="net45" />
5+
<package id="Moq" version="4.7.142" targetFramework="net45" />
46
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
57
<package id="Selenium.WebDriver" version="2.53.0" targetFramework="net45" />
68
</packages>

Globant.Selenium.Axe/Globant.Selenium.Axe/AxeBuilder.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,7 @@ private AxeResult Execute(string command, params object[] args)
5252
{
5353
object response = ((IJavaScriptExecutor)_webDriver).ExecuteAsyncScript(command, args);
5454
var jObject = JObject.FromObject(response);
55-
56-
if (jObject.SelectToken("violations") == null)
57-
throw new InvalidOperationException("The response from browser is not valid.");
58-
59-
if (jObject.SelectToken("passes") == null)
60-
throw new InvalidOperationException("The response from browser is not valid.");
61-
62-
JToken violationsToken = jObject.SelectToken("violations");
63-
JToken passesToken = jObject.SelectToken("passes");
64-
65-
return new AxeResult(violationsToken, passesToken);
55+
return new AxeResult(jObject);
6656
}
6757

6858
/// <summary>
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1-
using Newtonsoft.Json.Linq;
1+
using System;
2+
using Newtonsoft.Json.Linq;
23
using System.Collections.Generic;
34

45
namespace Globant.Selenium.Axe
56
{
67
public class AxeResult
78
{
8-
public IReadOnlyList<AxeResultItem> Violations { get; private set; }
9-
public IReadOnlyList<AxeResultItem> Passes { get; private set; }
9+
public AxeResultItem[] Violations { get; }
10+
public AxeResultItem[] Passes { get; }
11+
public AxeResultItem[] Inapplicable { get; }
12+
public AxeResultItem[] Incomplete { get; }
13+
public DateTimeOffset? Timestamp { get; private set; }
14+
public string Url { get; private set; }
1015

11-
public AxeResult(JToken violationsToken, JToken passesToken)
16+
public AxeResult(JObject results)
1217
{
13-
Violations = violationsToken.ToObject<List<AxeResultItem>>();
14-
Passes = passesToken.ToObject<List<AxeResultItem>>();
18+
JToken violationsToken = results.SelectToken("violations");
19+
JToken passesToken = results.SelectToken("passes");
20+
JToken inapplicableToken = results.SelectToken("inapplicable");
21+
JToken incompleteToken = results.SelectToken("incomplete");
22+
JToken timestampToken = results.SelectToken("timestamp");
23+
JToken urlToken = results.SelectToken("url");
24+
25+
Violations = violationsToken?.ToObject<AxeResultItem[]>();
26+
Passes = passesToken?.ToObject<AxeResultItem[]>();
27+
Inapplicable = inapplicableToken?.ToObject<AxeResultItem[]>();
28+
Incomplete = incompleteToken?.ToObject<AxeResultItem[]>();
29+
Timestamp = timestampToken?.ToObject<DateTimeOffset>();
30+
Url = urlToken?.ToObject<string>();
1531
}
1632
}
1733
}

Globant.Selenium.Axe/Globant.Selenium.Axe/AxeResultItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class AxeResultItem
99
public string Help { get; set; }
1010
public string HelpUrl { get; set; }
1111
public string Impact { get; set; }
12-
public List<string> Tags { get; set; }
13-
public List<AxeResultNode> Nodes { get; set; }
12+
public string[] Tags { get; set; }
13+
public AxeResultNode[] Nodes { get; set; }
1414
}
1515
}

Globant.Selenium.Axe/Globant.Selenium.Axe/FileAxeScriptProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class FileAxeScriptProvider : IAxeScriptProvider
1111
{
1212
private readonly string _filePath;
1313

14-
internal FileAxeScriptProvider(string filePath)
14+
public FileAxeScriptProvider(string filePath)
1515
{
1616
if (string.IsNullOrEmpty(filePath))
1717
throw new ArgumentNullException(nameof(filePath));

Globant.Selenium.Axe/Globant.Selenium.Axe/IncludeExcludeManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ namespace Globant.Selenium.Axe
1212
/// </summary>
1313
public class IncludeExcludeManager
1414
{
15-
private static readonly object syncObject = new object();
16-
private static readonly JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
15+
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
1716
{
1817
Formatting = Formatting.None,
1918
ContractResolver = new CamelCasePropertyNamesContractResolver(),
@@ -83,7 +82,7 @@ public string GetFirstItemToInclude()
8382
/// <returns>This instance serialized in JSON format</returns>
8483
public string ToJson()
8584
{
86-
return JsonConvert.SerializeObject(this, jsonSerializerSettings);
85+
return JsonConvert.SerializeObject(this, JsonSerializerSettings);
8786
}
8887

8988
private static void ValidateParameters(string[] selectors)

appveyor.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ deploy:
2727
api_key:
2828
secure: wO5eiWkOc3xv1qM1mChKx6weYq9h795U6u5NJg6kqwdSgsXZVCXjQJk6G05MuPKC
2929
skip_symbols: false
30-
artifact: /.*\.nupkg/
30+
artifact: /.*\.nupkg/
31+
32+
only_commits:
33+
files:
34+
- Globant.Selenium.Axe/

0 commit comments

Comments
 (0)