diff --git a/java-accessibility-checker/README-DEV.md b/java-accessibility-checker/README-DEV.md index 31dff9860..d1bcffc17 100644 --- a/java-accessibility-checker/README-DEV.md +++ b/java-accessibility-checker/README-DEV.md @@ -7,6 +7,11 @@ Run all tests: mvn test ``` +Run specific test +``` +mvn test -Dtest="TheSecondUnitTest#whenTestCase2_thenPrintTest2_1" +``` + Deploy ``` mvn -s ./settings.xml clean deploy -Dgpg.passphrase=yourpassphrase diff --git a/java-accessibility-checker/src/main/java/com/ibm/able/equalaccess/enginecontext/selenium/EngineContextSelenium.java b/java-accessibility-checker/src/main/java/com/ibm/able/equalaccess/enginecontext/selenium/EngineContextSelenium.java index 02c9705a8..778c1fc5b 100644 --- a/java-accessibility-checker/src/main/java/com/ibm/able/equalaccess/enginecontext/selenium/EngineContextSelenium.java +++ b/java-accessibility-checker/src/main/java/com/ibm/able/equalaccess/enginecontext/selenium/EngineContextSelenium.java @@ -78,7 +78,7 @@ public void loadEngine() { script.setAttribute('aChecker', 'ACE'); script.setAttribute('src', '%s/ace.js'); script.addEventListener('load', function() { - globalThis.ace_ibma = ace; + window.ace_ibma = globalThis.ace_ibma = ace; if ('undefined' !== typeof(ace)) { ace = ace_backup_in_ibma; } @@ -107,7 +107,7 @@ public void loadEngine() { } if ('undefined' === typeof (ace) || ace === null) { eval(%s) - globalThis.ace_ibma = ace; + window.ace_ibma = globalThis.ace_ibma = ace; if ('undefined' !== typeof(ace)) { ace = ace_backup_in_ibma; } @@ -188,7 +188,7 @@ else if (reportValue[1] === "POTENTIAL") { let policies = %s; let reportLevels = %s; - let checker = new window.ace_ibma.Checker(); + let checker = new (globalThis.ace_ibma || window.ace_ibma).Checker(); let customRulesets = []; customRulesets.forEach((rs) => checker.addRuleset(rs)); setTimeout(function() { @@ -223,7 +223,7 @@ else if (reportValue[1] === "POTENTIAL") { } // TODO: - // String getPolicies = "return new window.ace_ibma.Checker().rulesetIds;"; + // String getPolicies = "return new (globalThis.ace_ibma || window.ace_ibma).Checker().rulesetIds;"; // if (curPol != null && !checkPolicy) { // checkPolicy = true; // const valPolicies = ACEngineManager.customRulesets.map(rs => rs.id).concat(await browser.executeScript(getPolicies)); @@ -259,7 +259,7 @@ public Guideline[] getGuidelines() { String scriptStr = String.format(""" let cb = arguments[arguments.length - 1]; try { - let checker = new window.ace_ibma.Checker(); + let checker = new (globalThis.ace_ibma || window.ace_ibma).Checker(); let customRulesets = []; customRulesets.forEach((rs) => checker.addRuleset(rs)); cb(JSON.stringify(checker.getGuidelines())); @@ -276,7 +276,7 @@ public Rule[] getRules() { String scriptStr = String.format(""" let cb = arguments[arguments.length - 1]; try { - let checker = new window.ace_ibma.Checker(); + let checker = new (globalThis.ace_ibma || window.ace_ibma).Checker(); cb(JSON.stringify(checker.getRules())); } catch (e) { cb(e); diff --git a/java-accessibility-checker/src/test/java/com/ibm/able/equalaccess/AccessibilityCheckerSeleniumFFTest.java b/java-accessibility-checker/src/test/java/com/ibm/able/equalaccess/AccessibilityCheckerSeleniumFFTest.java new file mode 100644 index 000000000..8be5234c2 --- /dev/null +++ b/java-accessibility-checker/src/test/java/com/ibm/able/equalaccess/AccessibilityCheckerSeleniumFFTest.java @@ -0,0 +1,222 @@ +/****************************************************************************** + Copyright:: 2024- IBM, Inc + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + *****************************************************************************/ +package com.ibm.able.equalaccess; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.SessionNotCreatedException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.firefox.FirefoxOptions; + +import com.google.gson.Gson; +import com.ibm.able.equalaccess.config.ACConfigManager; +import com.ibm.able.equalaccess.engine.ACReport; +import com.ibm.able.equalaccess.engine.ACReport.Result; + +public class AccessibilityCheckerSeleniumFFTest { + public static class UnitTestInfoResult { + public String ruleId; + public String reasonId; + public String category; + public String message; + public String[] messageArgs; + public String[] value; + public Map path; + + public boolean matches(Result result) { + return ruleId.equals(result.ruleId) + && reasonId.equals(result.reasonId) + && category.equals(result.category) + && message.equals(result.message) + && value[1].equals(result.value[1]) + && path.get("dom").equals(result.path.get("dom")) + && path.get("aria").equals(result.path.get("aria")); + } + } + public static class UnitTestInfo { + public String[] ruleIds; + public UnitTestInfoResult[] results; + } + private static WebDriver driver; + + /** + * Setup a Selenium Chrome environment before tests + */ + @BeforeClass public static void setup() { + // Make sure we're starting with a clean config + File configFile = new File("achecker.json"); + configFile.delete(); + ACConfigManager.resetConfig(); + + try { + // ClientConfig timeoutConfig = ClientConfig.defaultConfig().readTimeout(Duration.ofMinutes(60)); + FirefoxOptions options = new FirefoxOptions(); + driver = new FirefoxDriver(options); + } catch (SessionNotCreatedException e) { + System.out.println(e.getMessage()); + System.out.println(e.getAdditionalInformation()); + throw e; + } + } + + /** + * Close Selenium Chrome environment after tests + */ + @AfterClass public static void teardown() { + AccessibilityCheckerSeleniumFFTest.driver.close(); + AccessibilityChecker.close(); + } + + private void listFiles(File f, java.util.List retFiles) { + if (f.isFile() && f.exists() && (f.getName().endsWith("html") || f.getName().endsWith("htm"))) { + retFiles.add(f); + } else if (f.isDirectory()) { + for (File subF: f.listFiles((testFile, name) -> testFile.isDirectory() || name.endsWith(".htm") || name.endsWith(".html"))) { + listFiles(subF, retFiles); + } + } + + } + + @Test public void getCompliance() { + ACConfigManager.getConfig().label = new String[] { "IBMa-Java-TeSt" }; + AccessibilityCheckerSeleniumFFTest.driver.get("https://altoromutual.12mc9fdq8fib.us-south.codeengine.appdomain.cloud/"); + ACReport report = AccessibilityChecker.getCompliance(driver, "SeleniumFF_getComplianceTest"); + assertNotNull(report); + assertTrue(report.results.length > 0); + } + + public void getComplianceTestsuite() throws IOException { + ACConfigManager.resetConfig(); + File configFile = new File("achecker.json"); + try { + configFile.delete(); + FileWriter myWriter = new FileWriter("achecker.json"); + myWriter.write(""" +{ + "customRuleServer": true, + "rulePack": "https://localhost:9445/rules/archives/preview/js", + "ruleArchive": "preview", + "ignoreHTTPSErrors": true, + "policies": [ "IBM_Accessibility", "IBM_Accessibility_next"], + "failLevels": [ "violation", "potentialviolation" ], + "reportLevels": [ + "violation", + "potentialviolation", + "recommendation", + "potentialrecommendation", + "manual", + "pass" + ], + "outputFormat": [ "json" ], + "label": [ + "IBMa-Java-TeSt" + ] +} +"""); + myWriter.close(); + ACConfigManager.getConfig(); + + Gson gson = new Gson(); + File testRootDir = Paths.get(System.getProperty("user.dir"), "..","accessibility-checker-engine","test","v2","checker","accessibility","rules").toFile(); + ArrayList testFiles = new ArrayList<>(); + listFiles(testRootDir, testFiles); + + + // Skip test cases that don't work in this environment (e.g., can't disable meta refresh in chrome) + Set skipList = new HashSet<>(Arrays.asList(new File[] { + //not in karma conf file + Paths.get(testRootDir.getAbsolutePath(), "a_text_purpose_ruleunit", "A-hasTextEmbedded.html").toFile(), + // path.join(testRootDir, "a_text_purpose_ruleunit", "A-nonTabable.html"), + + // Meta refresh + Paths.get(testRootDir.getAbsolutePath(), "meta_refresh_delay_ruleunit", "Meta-invalidRefresh.html").toFile(), + Paths.get(testRootDir.getAbsolutePath(), "meta_refresh_delay_ruleunit", "Meta-validRefresh.html").toFile(), + Paths.get(testRootDir.getAbsolutePath(), "meta_redirect_optional_ruleunit", "Meta-RefreshZero.html").toFile(), + + // CSS test issues + Paths.get(testRootDir.getAbsolutePath(), "style_color_misuse_ruleunit","D543.html").toFile(), + Paths.get(testRootDir.getAbsolutePath(), "style_before_after_review_ruleunit","D100.html").toFile(), + + // Firefox + Paths.get(testRootDir.getAbsolutePath(), "aria_role_redundant_ruleunit", "Fail.html").toFile(), + Paths.get(testRootDir.getAbsolutePath(), "style_focus_visible_ruleunit", "CSS-used.html").toFile(), + Paths.get(testRootDir.getAbsolutePath(), "object_text_exists_ruleunit", "act_fail_3.html").toFile() + + // Misc + // path.join(testRootDir, "aria_banner_label_unique_ruleunit", "validLandMarks-testCaseFromAnn.html"), + })); + + for (File testFile: testFiles) { + if (testFile.getAbsolutePath().contains("aria_attribute_valid_ruleunit")) continue; + if (testFile.getAbsolutePath().contains("target_spacing_sufficient_ruleunit")) continue; + if (testFile.getAbsolutePath().contains("aria_role_valid_ruleunit")) continue; + if (testFile.getAbsolutePath().contains("element_scrollable_tabbable_ruleunit")) continue; + if (testFile.getAbsolutePath().contains("fieldset_label_valid_ruleunit")) continue; + if (testFile.getAbsolutePath().contains("a_text_purpose_ruleunit")) continue; + if (skipList.contains(testFile)) continue; + AccessibilityCheckerSeleniumFFTest.driver.get("file://"+testFile.getAbsolutePath()); + ACReport report = AccessibilityChecker.getCompliance(driver, "Selenium_"+testFile.getAbsolutePath().substring(testRootDir.getAbsolutePath().length())); + String unitTestInfoStr = ((JavascriptExecutor)driver).executeScript("return JSON.stringify((typeof (window.UnitTest) !== 'undefined' && window.UnitTest))").toString(); + if (!"false".equals(unitTestInfoStr)) { + UnitTestInfo expectedInfo = gson.fromJson(unitTestInfoStr, UnitTestInfo.class); + List coveredRuleIds = Arrays.asList(expectedInfo.ruleIds); + if (expectedInfo != null && expectedInfo.ruleIds != null && expectedInfo.ruleIds.length > 0) { + System.out.println(testFile.getCanonicalPath()); + System.out.flush(); + List actualIssues = new LinkedList<>(Arrays.stream(report.results).filter(actualIssue -> coveredRuleIds.contains(actualIssue.ruleId)).toList()); + List expectedIssues = new LinkedList<>(Arrays.asList(expectedInfo.results)); + for (int idxActual=0; idxActual