diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs index 89428a0..b731af9 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/AxeBuilderTest.cs @@ -50,7 +50,7 @@ public void ShouldExecuteAxeScript() targetLocator.Setup(t => t.DefaultContent()).Returns(driver.Object); jsExecutor - .Setup(js => js.ExecuteAsyncScript(It.IsAny(), It.IsAny())) + .Setup(js => js.ExecuteScript(It.IsAny(), It.IsAny())) .Returns(new { violations = new object[] { }, @@ -62,6 +62,7 @@ public void ShouldExecuteAxeScript() }); var builder = new AxeBuilder(driver.Object); + builder.WaitForResult = false; var result = builder.Analyze(); result.Should().NotBeNull(); 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 3637eca..60e45a6 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 @@ -37,31 +37,33 @@ - ..\packages\Castle.Core.4.2.1\lib\net45\Castle.Core.dll + ..\packages\Castle.Core.4.3.1\lib\net45\Castle.Core.dll - - ..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.dll - True + + ..\packages\FluentAssertions.5.4.1\lib\net45\FluentAssertions.dll - - ..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.Core.dll - True + + ..\packages\Moq.4.9.0\lib\net45\Moq.dll - - ..\packages\Moq.4.7.142\lib\net45\Moq.dll - - - ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll - True + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.1\lib\netstandard1.0\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\netstandard1.0\System.ValueTuple.dll + - - ..\packages\Selenium.WebDriver.2.53.0\lib\net40\WebDriver.dll - True + + ..\packages\Selenium.WebDriver.3.14.0\lib\net45\WebDriver.dll @@ -78,7 +80,6 @@ - @@ -88,9 +89,8 @@ - - Designer - + + diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/app.config b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/app.config new file mode 100644 index 0000000..3fd2154 --- /dev/null +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/app.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/packages.config b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/packages.config index 0c8fb78..1e82fa6 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/packages.config +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe.Test/packages.config @@ -1,8 +1,11 @@  - - - - - + + + + + + + + \ 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 8334351..1d42a8e 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeBuilder.cs +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe/AxeBuilder.cs @@ -1,7 +1,7 @@ using Newtonsoft.Json.Linq; using OpenQA.Selenium; +using OpenQA.Selenium.Support.UI; using System; -using System.Net; namespace Globant.Selenium.Axe { @@ -14,15 +14,15 @@ public class AxeBuilder private readonly IWebDriver _webDriver; private readonly IncludeExcludeManager _includeExcludeManager = new IncludeExcludeManager(); - private static readonly AxeBuilderOptions DefaultOptions = new AxeBuilderOptions {ScriptProvider = new EmbeddedResourceAxeProvider()}; - - public string Options { get; set; } = "null"; + private static readonly AxeBuilderOptions DefaultOptions = new AxeBuilderOptions { ScriptProvider = new EmbeddedResourceAxeProvider() }; + public string Options { get; set; } = "null"; + public bool WaitForResult { get; set; } = true; /// /// Initialize an instance of /// /// Selenium driver to use - public AxeBuilder(IWebDriver webDriver): this(webDriver, DefaultOptions) + public AxeBuilder(IWebDriver webDriver) : this(webDriver, DefaultOptions) { } @@ -33,13 +33,12 @@ public AxeBuilder(IWebDriver webDriver): this(webDriver, DefaultOptions) /// Builder options public AxeBuilder(IWebDriver webDriver, AxeBuilderOptions options) { - if (webDriver == null) - throw new ArgumentNullException(nameof(webDriver)); - if (options == null) + { throw new ArgumentNullException(nameof(options)); + } - _webDriver = webDriver; + _webDriver = webDriver ?? throw new ArgumentNullException(nameof(webDriver)); _webDriver.Inject(options.ScriptProvider); } @@ -50,9 +49,17 @@ public AxeBuilder(IWebDriver webDriver, AxeBuilderOptions options) /// private AxeResult Execute(string command, params object[] args) { - object response = ((IJavaScriptExecutor)_webDriver).ExecuteAsyncScript(command, args); + ((IJavaScriptExecutor)_webDriver).ExecuteScript(command, args); + + if (WaitForResult) + { + var wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(60)); + wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.Id("accessibilityResultReady"))); + } + + object response = ((IJavaScriptExecutor)_webDriver).ExecuteScript("return window.promiseResultAccessibility;", args); var jObject = JObject.FromObject(response); - return new AxeResult(jObject); + return new AxeResult(jObject); } /// @@ -62,7 +69,7 @@ private AxeResult Execute(string command, params object[] args) /// public AxeBuilder Include(params string[] selectors) { - _includeExcludeManager.Include(selectors); + _includeExcludeManager.IncludeToAnalyzer(selectors); return this; } @@ -74,7 +81,7 @@ public AxeBuilder Include(params string[] selectors) /// public AxeBuilder Exclude(params string[] selectors) { - _includeExcludeManager.Exclude(selectors); + _includeExcludeManager.ExcludeToAnalyzer(selectors); return this; } @@ -85,8 +92,20 @@ public AxeBuilder Exclude(params string[] selectors) /// An aXe results document public AxeResult Analyze(IWebElement context) { - string command = string.Format("axe.a11yCheck(arguments[0], {0}, arguments[arguments.length - 1]);", Options); - return Execute(command, context); + var id = context.GetAttribute("id"); + if (string.IsNullOrEmpty(id)){ + throw new ArgumentException("WebElement must have a valid id"); + } + + var commandParams = Options == "null" ? $"document.getElementById('{id}')" : $"document.getElementById('{id}'), {Options}"; + var fullCommand = @"window.promiseResultAccessibility = null; + axe.run([params]).then(function (result) { + promiseResultAccessibility = result; + $(document).add('div').attr('id','accessibilityResultReady'); + }); + "; + fullCommand = fullCommand.Replace("[params]", commandParams); + return Execute(fullCommand, context); } /// @@ -95,23 +114,29 @@ public AxeResult Analyze(IWebElement context) /// An aXe results document public AxeResult Analyze() { - string command; + string commandParams; if (_includeExcludeManager.HasMoreThanOneSelectorsToIncludeOrSomeToExclude()) { - command = $"axe.a11yCheck({_includeExcludeManager.ToJson()}, {Options}, arguments[arguments.length - 1]);"; + commandParams = Options == "null" ? $"{ _includeExcludeManager.ToJson()}" : $"{ _includeExcludeManager.ToJson()}, {Options}"; } else if (_includeExcludeManager.HasOneItemToInclude()) { string itemToInclude = _includeExcludeManager.GetFirstItemToInclude().Replace("'", ""); - command = $"axe.a11yCheck('{itemToInclude}', {Options}, arguments[arguments.length - 1]);"; + commandParams = Options == "null" ? $"'{itemToInclude}'" : $"'{itemToInclude}', {Options}"; } else { - command = $"axe.a11yCheck(document, {Options}, arguments[arguments.length - 1]);"; + commandParams = Options == "null" ? $"document" : $"document, {Options}"; } - - return Execute(command); + var fullCommand = @"window.promiseResultAccessibility = null; + axe.run([params]).then(function (result) { + promiseResultAccessibility = result; + $(document).add('div').attr('id','accessibilityResultReady'); + }); + "; + fullCommand = fullCommand.Replace("[params]", commandParams); + return Execute(fullCommand); } } } \ No newline at end of file diff --git a/Globant.Selenium.Axe/Globant.Selenium.Axe/Globant.Selenium.Axe.csproj b/Globant.Selenium.Axe/Globant.Selenium.Axe/Globant.Selenium.Axe.csproj index cb6e271..0db6fb3 100644 --- a/Globant.Selenium.Axe/Globant.Selenium.Axe/Globant.Selenium.Axe.csproj +++ b/Globant.Selenium.Axe/Globant.Selenium.Axe/Globant.Selenium.Axe.csproj @@ -31,9 +31,11 @@ 4 - - ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll - True + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + + ..\packages\DotNetSeleniumExtras.WaitHelpers.3.11.0\lib\net45\SeleniumExtras.WaitHelpers.dll @@ -44,9 +46,8 @@ - - ..\packages\Selenium.WebDriver.2.53.0\lib\net40\WebDriver.dll - True + + ..\packages\Selenium.WebDriver.3.14.0\lib\net45\WebDriver.dll @@ -72,9 +73,6 @@ - - - ResXFileCodeGenerator @@ -82,8 +80,12 @@ + + + +