Skip to content

Commit 8e0797f

Browse files
Rough commit of adding axe core to framework (#73)
Co-authored-by: damien-cooke <[email protected]>
1 parent a729b9f commit 8e0797f

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@
108108
<sonar.projectKey>digital-delivery-academy_selenium-pom-example</sonar.projectKey>
109109
<sonar.organization>digital-delivery-academy</sonar.organization>
110110
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
111+
<axe.core.version>4.1.2</axe.core.version>
111112
</properties>
112-
113113
<dependencies>
114114
<dependency>
115115
<groupId>org.seleniumhq.selenium</groupId>
@@ -207,6 +207,18 @@
207207
<version>${jetty.version}</version>
208208
<scope>test</scope>
209209
</dependency>
210+
<dependency>
211+
<groupId>com.deque.html.axe-core</groupId>
212+
<artifactId>selenium</artifactId>
213+
<version>${axe.core.version}</version>
214+
<scope>test</scope>
215+
</dependency>
216+
<dependency>
217+
<groupId>com.deque.html.axe-core</groupId>
218+
<artifactId>selenium</artifactId>
219+
<version>4.1.2</version>
220+
<scope>compile</scope>
221+
</dependency>
210222
</dependencies>
211223

212224
<build>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package uk.co.evoco.webdriver;
2+
3+
import com.deque.html.axecore.results.Results;
4+
import com.deque.html.axecore.results.Rule;
5+
import com.deque.html.axecore.selenium.AxeBuilder;
6+
import com.deque.html.axecore.selenium.AxeReporter;
7+
import org.junit.Assert;
8+
import org.openqa.selenium.WebDriver;
9+
import java.io.IOException;
10+
import java.util.List;
11+
12+
public class AccessibilityBase {
13+
private static final String reportPath = System.getProperty("user.dir") + "\\src\\test\\java\\reports\\";
14+
15+
public static void checkAccessibilityViolations(WebDriver webDriver) throws IOException {
16+
String reportFile = reportPath + "accessibilityReport";
17+
AxeBuilder builder = new AxeBuilder();
18+
Results results = builder.analyze(webDriver);
19+
saveReport(results, reportFile);
20+
}
21+
22+
private static void saveReport(Results results, String reportFile) {
23+
List<Rule> violations = results.getViolations();
24+
if (violations.size() == 0) {
25+
Assert.assertTrue("No violations found", true);
26+
} else {
27+
AxeReporter.writeResultsToJsonFile(reportFile, results);
28+
Assert.assertEquals(0, violations.size());
29+
}
30+
}
31+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package uk.co.evoco.webdriver;
2+
3+
import org.junit.jupiter.api.*;
4+
import uk.co.evoco.tests.BaseAbstractTest;
5+
import uk.co.evoco.webdriver.utils.EmbeddedJetty;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
11+
public class AccessibilityTest extends BaseAbstractTest{
12+
13+
private static String baseUrl;
14+
private static EmbeddedJetty embeddedJetty;
15+
private static final String reportPath = System.getProperty("user.dir") + "\\src\\test\\java\\reports\\accessibilityReport.json";
16+
private final File accessibilityReport = new File(reportPath);
17+
18+
19+
@BeforeAll
20+
public static void webDriverSetup() throws Exception {
21+
embeddedJetty = new EmbeddedJetty();
22+
embeddedJetty.start();
23+
String landingPage = "/index.html";
24+
baseUrl = "http://localhost:" + embeddedJetty.getPort() + landingPage;
25+
}
26+
27+
@AfterAll
28+
public static void webDriverTearDown() throws Exception {
29+
embeddedJetty.stop();
30+
}
31+
32+
@BeforeEach
33+
public void setUpWebApp() {
34+
webDriver.get(baseUrl);
35+
}
36+
37+
@AfterEach
38+
public void deleteReport() {
39+
accessibilityReport.delete();
40+
}
41+
42+
@Test
43+
public void testPageWithNoAccessibilityViolations() throws IOException {
44+
AccessibilityBase.checkAccessibilityViolations(webDriver);
45+
assertThat(accessibilityReport.exists(), is(false));
46+
}
47+
48+
//Add assertion to read the json file output to check it has found the violations
49+
//Add html page that has accessibility violations
50+
@Test
51+
public void testPageWithAccessibilityViolations() throws IOException {
52+
webDriver.navigate().to(baseUrl + "/accessibilityViolations.html");
53+
AccessibilityBase.checkAccessibilityViolations(webDriver);
54+
assertThat(accessibilityReport.exists(), is(true));
55+
}
56+
}

0 commit comments

Comments
 (0)