Skip to content
This repository has been archived by the owner on Apr 30, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into Accessibility_FileManagement
Browse files Browse the repository at this point in the history
  • Loading branch information
jonreding2010 committed Oct 15, 2021
2 parents a1dfa0a + cd1478c commit ce7fc30
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* Utilities class for Accessibility Functionality.
*/
public class AccessibilityUtilities {

/**
* private class constructor.
*/
Expand Down Expand Up @@ -80,7 +81,7 @@ public static void checkAccessibility(WebDriver webDriver, Logger logger, boolea
* @param logger Where you want the check logged to
* @param checkType What kind of check is being run
* @param getResults Function for getting Axe results
* @param loggingLevel What level should logging the check take,
* @param loggingLevel What level should log check take,
* this gets used if the check doesn't throw an exception
* @param throwOnResults Throw error if any results are found
*/
Expand All @@ -99,7 +100,7 @@ public static void checkAccessibility(WebDriver webDriver, Logger logger, String
* Run axe accessibility and log the results.
* @param webDriver The web driver that is on the page you want to run the accessibility check on
* @param logger Where you want the check logged to
* @param loggingLevel What level should logging the check take,
* @param loggingLevel What level should log check take,
* this gets used if the check doesn't throw an exception
*/
public static void checkAccessibilityPasses(WebDriver webDriver, Logger logger, MessageType loggingLevel) {
Expand All @@ -111,7 +112,7 @@ public static void checkAccessibilityPasses(WebDriver webDriver, Logger logger,
* Run axe accessibility and log the results.
* @param webDriver The web driver that is on the page you want to run the accessibility check on
* @param logger Where you want the check logged to
* @param loggingLevel What level should logging the check take,
* @param loggingLevel What level should log check take,
* this gets used if the check doesn't throw an exception
* @param throwOnInapplicable Should inapplicable cause an exception to be thrown
*/
Expand All @@ -125,7 +126,7 @@ public static void checkAccessibilityInapplicable(WebDriver webDriver, Logger lo
* Run axe accessibility and log the results.
* @param webDriver The web driver that is on the page you want to run the accessibility check on
* @param logger Where you want the check logged to
* @param loggingLevel What level should logging the check take,
* @param loggingLevel What level should log check take,
* this gets used if the check doesn't throw an exception
* @param throwOnIncomplete Should incomplete cause an exception to be thrown
*/
Expand All @@ -139,7 +140,7 @@ public static void checkAccessibilityIncomplete(WebDriver webDriver, Logger logg
* Run axe accessibility and log the results.
* @param webDriver The web driver that is on the page you want to run the accessibility check on
* @param logger Where you want the check logged to
* @param loggingLevel What level should logging the check take,
* @param loggingLevel What level should log check take,
* this gets used if the check doesn't throw an exception
* @param throwOnViolation Should violations cause an exception to be thrown
*/
Expand All @@ -150,7 +151,7 @@ public static void checkAccessibilityViolations(WebDriver webDriver, Logger logg
}

/**
* Create a HTML accessibility report for an entire web page.
* Create an HTML accessibility report for an entire web page.
* @param testObject The TestObject to associate the report with
* @param throwOnViolation Should violations cause an exception to be thrown
*/
Expand All @@ -161,7 +162,7 @@ public static void createAccessibilityHtmlReport(SeleniumTestObject testObject,
}

/**
* Create a HTML accessibility report for an entire web page.
* Create an HTML accessibility report for an entire web page.
* @param testObject The TestObject to associate the report with
* @param throwOnViolation Should violations cause an exception to be thrown
*/
Expand All @@ -172,7 +173,7 @@ public static void createAccessibilityHtmlReport(SeleniumTestObject testObject,
}

/**
* Create a HTML accessibility report for an entire web page.
* Create an HTML accessibility report for an entire web page.
* @param testObject The TestObject to associate the report with
* @param throwOnViolation Should violations cause an exception to be thrown
*/
Expand All @@ -184,7 +185,7 @@ public static void createAccessibilityHtmlReport(SeleniumTestObject testObject,
}

/**
* Create a HTML accessibility report for an entire web page.
* Create an HTML accessibility report for an entire web page.
* @param testObject The TestObject to associate the report with
* @param throwOnViolation Should violations cause an exception to be thrown
*/
Expand All @@ -206,7 +207,7 @@ public static void createAccessibilityHtmlReport(SeleniumTestObject testObject,
}

/**
* Create a HTML accessibility report.
* Create an HTML accessibility report.
* @param testObject The TestObject to associate the report with
* @param getResults Function for getting the accessibility scan results
* @param throwOnViolation Should violations cause an exception to be thrown
Expand Down Expand Up @@ -242,7 +243,7 @@ public static void createAccessibilityHtmlReport(SeleniumTestObject testObject,
testObject.getLogger().logMessage(MessageType.INFORMATION,
"Ran accessibility check and created HTML report: " + report + " ");

// Throw exception if we found violations and we want that to cause an error
// Throw exception if we found violations when we want that to cause an error
if (throwOnViolation && !getResults.get().getViolations().isEmpty()) {
throw new AxeRuntimeException(new Exception(System.lineSeparator()
+ "Accessibility violations, see: " + report + " for more details."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrapsElement;

/**
* The HTML reporter class.
*/
public class HtmlReporter {

private static final String classString = "class";

private static final String resourcesFile = "../jmaqs-accessibility/src/main/resources/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.magenic.jmaqs.selenium.BaseSeleniumTest;
import com.magenic.jmaqs.selenium.LazyWebElement;
import com.magenic.jmaqs.selenium.SeleniumConfig;
import com.magenic.jmaqs.selenium.UIWait;
import com.magenic.jmaqs.selenium.factories.UIWaitFactory;
import com.magenic.jmaqs.utilities.helper.TestCategories;
import com.magenic.jmaqs.utilities.logging.FileLogger;
Expand All @@ -29,11 +28,13 @@
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
* Accessibility HTML unit tests.
*/
public class AccessibilityHTMLUnitTest extends BaseSeleniumTest {

/**
* The file that has a sample result with an error.
*/
Expand All @@ -49,33 +50,20 @@ public class AccessibilityHTMLUnitTest extends BaseSeleniumTest {
*/
private static final String TestSiteAutomationUrl = TestSiteUrl + "Automation/";

private UIWait wait;

@BeforeMethod
public void setup() {
this.getWebDriver().navigate().to(TestSiteUrl);
wait = UIWaitFactory.getWaitDriver(this.getWebDriver());
UIWaitFactory.getWaitDriver(this.getWebDriver()).waitForPageLoad();
}

@AfterMethod
public void cleanUp() {
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}


/**
* Verify we can create and associate an accessibility HTML report.
* @throws IOException if an exception is thrown
*/
@Test(groups = TestCategories.ACCESSIBILITY)
public void testAccessibilityHtmlReport() throws IOException, ParseException {
setup(TestSiteUrl);
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(),
new AxeBuilder().analyze(this.getWebDriver()), false);

String filePath = Arrays.stream(this.getTestObject().getArrayOfAssociatedFiles())
.filter(x -> x.contains(".html")).findFirst().map(Object::toString).orElse("");
Assert.assertTrue(filePath.length() > 0, "Accessibility report is empty");
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

/**
Expand All @@ -84,6 +72,8 @@ public void testAccessibilityHtmlReport() throws IOException, ParseException {
*/
@Test(groups = TestCategories.ACCESSIBILITY)
public void accessibilityMultipleHtmlReports() throws IOException, ParseException {
setup(TestSiteUrl);

// Create 3 reports
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(), false);
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(), false);
Expand All @@ -93,30 +83,31 @@ public void accessibilityMultipleHtmlReports() throws IOException, ParseExceptio
.filter(x -> x.contains(".html")).count();
Assert.assertEquals(count, 3,
"Expected 3 accessibility reports but see " + count + " instead");
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

/**
* Verify we throw an exception if the scan has an error.
*/
@Test(groups = TestCategories.ACCESSIBILITY, expectedExceptions = AxeRuntimeException.class)
public void accessibilityHtmlReportWithError() throws IOException, ParseException {
setup(TestSiteUrl);
String axeResultWithError = FileUtils.readFileToString(axeResultWithErrorFile, StandardCharsets.UTF_8);
Results results = new ObjectMapper().readValue(axeResultWithError, Results.class);
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(), results, false);

String filePath = Arrays.stream(this.getTestObject().getArrayOfAssociatedFiles())
.filter(x -> x.contains(".html")).findFirst().map(Object::toString).orElse("");
Assert.assertTrue(filePath.length() > 0, "Accessibility report is empty");
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

/**
* Verify we throw an exception if the scan has an error and are using lazy elements.
*/
@Test(groups = TestCategories.ACCESSIBILITY, expectedExceptions = AxeRuntimeException.class)
public void accessibilityHtmlReportWithErrorFromLazyElement() throws IOException, ParseException {
this.getWebDriver().navigate().to(TestSiteAutomationUrl);
wait.waitForPageLoad();

setup(TestSiteAutomationUrl);
String axeResultWithError = FileUtils.readFileToString(axeResultWithErrorFile, StandardCharsets.UTF_8);
Results error = new ObjectMapper().readValue(axeResultWithError, Results.class);
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(), error,false);
Expand All @@ -125,26 +116,27 @@ public void accessibilityHtmlReportWithErrorFromLazyElement() throws IOException
.filter(x -> x.contains(".html")).findFirst().map(Object::toString).orElse("");

Assert.assertTrue(filePath.length() > 0, "Accessibility report is empty");
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

/**
* Verify we throw an exception if there are violations and we choose the throw exception option.
* Verify we throw an exception if there are violations, and we choose the throw exception option.
* @throws IOException if an exception is thrown
*/
@Test(groups = TestCategories.ACCESSIBILITY, expectedExceptions = RuntimeException.class)
public void accessibilityHtmlReportWithViolation() throws IOException, ParseException {
setup(TestSiteUrl);
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(), true);
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

/**
* Verify we can create an accessibility HTML report off a lazy element.
* Verify we can create an accessibility HTML report file with a lazy element.
* @throws IOException if exception is thrown
*/
@Test(groups = TestCategories.ACCESSIBILITY)
public void accessibilityHtmlReportWithLazyElement() throws IOException, ParseException {
this.getWebDriver().get(TestSiteAutomationUrl);
wait.waitForPageLoad();

setup(TestSiteAutomationUrl);
LazyWebElement foodTable = new LazyWebElement(this.getTestObject(),
By.id("FoodTable"), "Food Table");

Expand All @@ -154,26 +146,24 @@ public void accessibilityHtmlReportWithLazyElement() throws IOException, ParseEx
String filePath = Arrays.stream(this.getTestObject().getArrayOfAssociatedFiles())
.filter(x -> x.contains(".html")).findFirst().map(Object::toString).orElse("");
Assert.assertFalse(filePath.isEmpty(), "Accessibility report is empty");
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

/**
* Verify we can create an accessibility HTML report off a normal web element.
* Verify we can create an accessibility HTML report file off a normal web element.
* @throws IOException if exception is thrown
*/
@Test(groups = TestCategories.ACCESSIBILITY)
public void accessibilityHtmlReportWithElement() throws IOException, ParseException {
this.getWebDriver().navigate().to(TestSiteAutomationUrl);
wait.waitForPageLoad();

setup(TestSiteAutomationUrl);
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(),
this.getWebDriver().findElement(By.id("FoodTable")), false);

String filePath = Arrays.stream(this.getTestObject().getArrayOfAssociatedFiles())
.filter(x -> x.contains(".html")).findFirst().map(Object::toString).orElse("");

File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
String fileString = IOUtils.toString(fis, StandardCharsets.UTF_8);
String fileString = IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8);
Assert.assertFalse(fileString.isEmpty(), "Accessibility report is empty");
}

Expand All @@ -186,16 +176,15 @@ public void accessibilityHtmlLogSuppression() throws IOException, ParseException
// Make sure we are not using verbose logging
this.getLogger().setLoggingLevel(MessageType.INFORMATION);

this.getWebDriver().navigate().to(TestSiteAutomationUrl);
wait.waitForPageLoad();

setup(TestSiteAutomationUrl);
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(), false);

// The script executed message should be suppressed when we run the accessibility check
FileInputStream fis = new FileInputStream(((FileLogger)this.getLogger()).getFilePath());
String file = IOUtils.toString(fis, StandardCharsets.UTF_8);

Assert.assertFalse(file.contains("Script executed"), "Logging was not suppressed as expected.");
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

/**
Expand All @@ -205,6 +194,8 @@ public void accessibilityHtmlLogSuppression() throws IOException, ParseException
*/
@Test(groups = TestCategories.ACCESSIBILITY)
public void accessibilityHtmlReportViolationsOnly() throws IOException, ParseException {
setup(TestSiteUrl);

// Make sure we are not using verbose logging
this.getLogger().setLoggingLevel(MessageType.INFORMATION);

Expand All @@ -217,31 +208,37 @@ public void accessibilityHtmlReportViolationsOnly() throws IOException, ParseExc
Assert.assertFalse(fileString.contains("Passes "), "Passes were still in the report");
Assert.assertFalse(fileString.contains("Inapplicable "), "Inapplicable were still in the report");
Assert.assertFalse(fileString.contains("Incomplete "), "Incomplete were still in the report");
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

/**
* Verify we can create an accessibility HTML report off a normal web element.
* Verify we can create an accessibility HTML report file off a normal web element.
* @throws IOException if exception is thrown
*/
@Test(groups = TestCategories.ACCESSIBILITY)
public void accessibilityHtmlViolationsReportWithElement() throws IOException, ParseException {
this.getWebDriver().navigate().to(TestSiteAutomationUrl);
wait.waitForPageLoad();

setup(TestSiteAutomationUrl);
AccessibilityUtilities.createAccessibilityHtmlReport(this.getTestObject(),
this.getWebDriver().findElement(By.id("FoodTable")),
false, EnumSet.of(ResultType.Violations));

String filePath = Arrays.stream(this.getTestObject().getArrayOfAssociatedFiles())
.filter(x -> x.contains(".html")).findFirst().map(Object::toString).orElse("");
Assert.assertFalse(filePath.isEmpty(), "Accessibility report is empty");
deleteFiles(Arrays.asList(this.getTestObject().getArrayOfAssociatedFiles()));
}

public void setup(String url) {
this.getWebDriver().navigate().to(url);
UIWaitFactory.getWaitDriver(this.getWebDriver()).waitForPageLoad();
}

private void deleteFiles(List<String> files) {
for (String file : files) {
File filePath = new File(file).getAbsoluteFile();
if (filePath.exists()) {
filePath.delete();
Assert.assertTrue(filePath.delete(), filePath.getAbsolutePath());
Assert.assertFalse(filePath.exists());
}
}
}
Expand Down
Loading

0 comments on commit ce7fc30

Please sign in to comment.