Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XWIKI-22260: WCAG automatic testing for modals #3293

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
*/
package org.xwiki.test.ui.po;

import com.deque.html.axecore.results.Results;
import com.deque.html.axecore.selenium.AxeBuilder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.test.ui.WCAGContext;
import org.xwiki.test.ui.XWikiWebDriver;

/**
* The base class for a bootstrap modal.
Expand All @@ -32,6 +38,8 @@
*/
public class BaseModal extends BaseElement
{
private static final Logger LOGGER = LoggerFactory.getLogger(BaseModal.class);

protected WebElement container;

/**
Expand All @@ -51,6 +59,9 @@ public BaseModal(By selector)
String className = this.container.getAttribute("class");
className = className.replace("fade", "");
getDriver().executeScript("arguments[0].setAttribute(\"class\",arguments[1])", this.container, className);
// Once we're sure the modal is loaded, we can validate its content in regards to accessibility
className = this.container.getAttribute("class").split(" ")[0];
validateWCAG(getUtil().getWCAGUtils().getWCAGContext(), true, "." + className);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels dangerous to rely on the className only: what if you have a page with 3 modals having the class modal, and only one is loaded? Right now the validation will be executed on the 3 modals AFAIU.
Feels like you could artificially add a new specific class (pretty much like we remove the fade class) just to identify the actual modal.

}

public String getTitle()
Expand All @@ -69,6 +80,42 @@ public void close()
waitForClosed();
}

public void validateWCAG(WCAGContext wcagContext, boolean checkCache, String cssSelector) {
if (!wcagContext.isWCAGEnabled()) {
// Block WCAG validation if it is not enabled, in all cases.
return;
}

try {
long startTime = System.currentTimeMillis();
// Run WCAG tests on the current UI page if the current URL + PO class name are not in the cache, or if checking
// the cache is disabled.
XWikiWebDriver driver = this.getDriver();
if (!checkCache || wcagContext.isNotCached(driver.getCurrentUrl(), this.getClass().getName())) {
AxeBuilder axeBuilder = wcagContext.getAxeBuilder();
// Only analyze the modal itself
axeBuilder.include(cssSelector);
Results axeResult = axeBuilder.analyze(driver);
wcagContext.addWCAGResults(driver.getCurrentUrl(), this.getClass().getName(), axeResult);
long stopTime = System.currentTimeMillis();
long deltaTime = stopTime - startTime;
LOGGER.info("[{} : {}] WCAG Validation on this element took [{}] ms.",
driver.getCurrentUrl(), this.getClass().getName(), deltaTime);
wcagContext.addWCAGTime(deltaTime);
} else {
// If the identifying pair is already in the cache, don't perform accessibility validation.
LOGGER.debug("[{} : {}] This combination of URL:class was already WCAG-checked.",
driver.getCurrentUrl(), this.getClass().getName());
}
} catch (Exception e) {
if (wcagContext.shouldWCAGStopOnError()) {
throw e;
} else {
LOGGER.debug("Error during WCAG execution, but ignored thanks to wcagStopOnError flag: ", e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better be an info than a debug here no? We don't enable debug often, and it could be useful to still see those, or could that be an issue on the CI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in line with

LOGGER.debug("Error during WCAG execution, but ignored thanks to wcagStopOnError flag: ", e);

}
}
}

/**
* The modal may have a fade out effect on close which means it may not disappear instantly. It's safer to wait for
* the modal to disappear when closed, before proceeding with next actions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ public void validateWCAG(WCAGContext wcagContext, boolean checkCache)
XWikiWebDriver driver = this.getDriver();
AxeBuilder axeBuilder = wcagContext.getAxeBuilder();
Results axeResult = axeBuilder.analyze(driver);
wcagContext.addWCAGResults(driver.getCurrentUrl(), this.getClass().getName(), axeResult);
wcagContext.addWCAGResults(this.getPageURL(), this.getClass().getName(), axeResult);
long stopTime = System.currentTimeMillis();
long deltaTime = stopTime - startTime;
LOGGER.debug("[{} : {}] WCAG Validation on this element took [{}] ms.",
Expand Down