|
| 1 | +## Accessibility Automation for Web Apps with Java and [Playwright](https://playwright.dev/). |
| 2 | + |
| 3 | +### This project uses [HTML CodeSniffer](https://squizlabs.github.io/HTML_CodeSniffer/) and [Deque Axe](https://www.deque.com/) |
| 4 | + |
| 5 | +**HTML CodeSniffer** : checks HTML source code and detects any Accessibility violations. Comes with standards that cover |
| 6 | +the three (A, AA & AAA) conformance levels of the W3C's Web Content Accessibility Guidelines (WCAG) 2.1 and the U.S. |
| 7 | +Section 508 legislation. |
| 8 | + |
| 9 | +**Deque Axe** : World’s leading digital accessibility toolkit. Powerful and accurate accessibility toolkit can get you |
| 10 | +to 80% issue coverage, or more, during development. |
| 11 | + |
| 12 | +[](http://www.oracle.com/technetwork/java/javase/downloads/index.html) |
| 13 | +[](https://www.codacy.com/gh/automated-a11y/java-a11y-playwright/dashboard?utm_source=github.com&utm_medium=referral&utm_content=automated-a11y/java-a11y-playwright&utm_campaign=Badge_Grade) |
| 14 | +[](https://www.codacy.com/gh/automated-a11y/java-a11y-playwright/dashboard?utm_source=github.com&utm_medium=referral&utm_content=automated-a11y/java-a11y-playwright&utm_campaign=Badge_Coverage) |
| 15 | +[](https://opensource.org/licenses/MIT) |
| 16 | +[](https://github.com/automated-a11y/java-a11y-playwright/graphs/contributors) |
| 17 | + |
| 18 | +### Features |
| 19 | + |
| 20 | +1. Simple & Easy to use |
| 21 | +2. No need of prior knowledge on Accessibility |
| 22 | +3. Works with Java [Playwright](https://playwright.dev/) |
| 23 | +4. Rich Reporting |
| 24 | +5. Open source |
| 25 | + |
| 26 | +### Getting Started |
| 27 | + |
| 28 | +#### Using HTML CodeSniffer |
| 29 | + |
| 30 | +Create object of `HtmlCsRunner` as below. `page` is instance of your single tab or a popup window within a browser |
| 31 | +context. |
| 32 | + |
| 33 | +```java |
| 34 | +HtmlCsRunner htmlCsRunner=new HtmlCsRunner(page); |
| 35 | +``` |
| 36 | + |
| 37 | +Once after you navigated to any page/popup with Playwright execute Accessibility on that particular page/popup |
| 38 | + |
| 39 | +```java |
| 40 | +htmlCsRunner.execute(); |
| 41 | +``` |
| 42 | + |
| 43 | +The above `execute` will also generate `JSON Report` on accessibility issues at page/popup level |
| 44 | + |
| 45 | +Once after all the tests executed, you can call the below method to generate consolidated `HTML Report` on accessibility |
| 46 | +issues |
| 47 | + |
| 48 | +```java |
| 49 | +htmlCsRunner.generateHtmlReport(); |
| 50 | +``` |
| 51 | + |
| 52 | +Below is junit example with reporting. |
| 53 | + |
| 54 | +```java |
| 55 | +import com.microsoft.playwright.Browser; |
| 56 | +import com.microsoft.playwright.BrowserContext; |
| 57 | +import com.microsoft.playwright.Page; |
| 58 | +import com.microsoft.playwright.Playwright; |
| 59 | +import io.github.sridharbandi.pw.HtmlCsRunner; |
| 60 | +import org.junit.jupiter.api.*; |
| 61 | + |
| 62 | +import java.io.IOException; |
| 63 | + |
| 64 | +public class HtmlcsTest { |
| 65 | + private static HtmlCsRunner htmlCsRunner; |
| 66 | + static Playwright playwright; |
| 67 | + static Browser browser; |
| 68 | + |
| 69 | + BrowserContext context; |
| 70 | + Page page; |
| 71 | + |
| 72 | + @BeforeAll |
| 73 | + static void launchBrowser() { |
| 74 | + playwright = Playwright.create(); |
| 75 | + browser = playwright.chromium().launch(); |
| 76 | + } |
| 77 | + |
| 78 | + @AfterAll |
| 79 | + static void closeBrowser() throws IOException { |
| 80 | + playwright.close(); |
| 81 | + htmlCsRunner.generateHtmlReport(); |
| 82 | + } |
| 83 | + |
| 84 | + @BeforeEach |
| 85 | + void createContextAndPage() { |
| 86 | + context = browser.newContext(); |
| 87 | + page = context.newPage(); |
| 88 | + htmlCsRunner = new HtmlCsRunner(page); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterEach |
| 92 | + void closeContext() throws IOException { |
| 93 | + htmlCsRunner.execute(); |
| 94 | + context.close(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void googleTest() { |
| 99 | + page.navigate("https://www.google.com/"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void stockTest() { |
| 104 | + page.navigate("https://www.istockphoto.com/"); |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +By default, it will check against `WCAG2AA` standards. However, you can configure it to standard you want to test with |
| 110 | + |
| 111 | +```java |
| 112 | +htmlCsRunner.setStandard(HTMLCS.WCAG2A); |
| 113 | +``` |
| 114 | + |
| 115 | +HTML Reports will be generated under `./target/java-a11y/htmlcs` folder. |
| 116 | + |
| 117 | +Below are the report screenshots |
| 118 | + |
| 119 | +Consolidated Report |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +Page Report |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +#### Using Deque Axe |
| 128 | + |
| 129 | +Create object of `AxeRunner` as below. `page` is instance of your single tab or a popup window within a browser context. |
| 130 | + |
| 131 | +```java |
| 132 | +AxeRunner axeRunner=new AxeRunner(page); |
| 133 | +``` |
| 134 | + |
| 135 | +Once after you navigated to any page/popup with Playwright execute Accessibility on that particular page/popup |
| 136 | + |
| 137 | +```java |
| 138 | +axeRunner.execute(); |
| 139 | +``` |
| 140 | + |
| 141 | +The above `execute` will also generate `JSON Report` on accessibility issues at page/popup level |
| 142 | + |
| 143 | +Once after all the tests executed, you can call the below method to generate consolidated `HTML Report` on accessibility |
| 144 | +issues |
| 145 | + |
| 146 | +```java |
| 147 | +axeRunner.generateHtmlReport(); |
| 148 | +``` |
| 149 | + |
| 150 | +Below is junit example with reporting. |
| 151 | + |
| 152 | +```java |
| 153 | +import com.microsoft.playwright.Browser; |
| 154 | +import com.microsoft.playwright.BrowserContext; |
| 155 | +import com.microsoft.playwright.Page; |
| 156 | +import com.microsoft.playwright.Playwright; |
| 157 | +import io.github.sridharbandi.pw.AxeRunner; |
| 158 | +import org.junit.jupiter.api.*; |
| 159 | + |
| 160 | +import java.io.IOException; |
| 161 | + |
| 162 | +public class AxeTest { |
| 163 | + private static AxeRunner axeRunner; |
| 164 | + static Playwright playwright; |
| 165 | + static Browser browser; |
| 166 | + |
| 167 | + BrowserContext context; |
| 168 | + Page page; |
| 169 | + |
| 170 | + @BeforeAll |
| 171 | + static void launchBrowser() { |
| 172 | + playwright = Playwright.create(); |
| 173 | + browser = playwright.chromium().launch(); |
| 174 | + } |
| 175 | + |
| 176 | + @AfterAll |
| 177 | + static void closeBrowser() throws IOException { |
| 178 | + playwright.close(); |
| 179 | + axeRunner.generateHtmlReport(); |
| 180 | + } |
| 181 | + |
| 182 | + @BeforeEach |
| 183 | + void createContextAndPage() { |
| 184 | + context = browser.newContext(); |
| 185 | + page = context.newPage(); |
| 186 | + axeRunner = new AxeRunner(page); |
| 187 | + } |
| 188 | + |
| 189 | + @AfterEach |
| 190 | + void closeContext() throws IOException { |
| 191 | + axeRunner.execute(); |
| 192 | + context.close(); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void googleTest() { |
| 197 | + page.navigate("https://www.google.com/"); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void stockTest() { |
| 202 | + page.navigate("https://www.istockphoto.com/"); |
| 203 | + } |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +HTML Reports will be generated under `./target/java-a11y/axe` folder. |
| 208 | + |
| 209 | +Below are the report screenshots |
| 210 | + |
| 211 | +Consolidated Report |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | +Page Report |
| 216 | + |
| 217 | + |
0 commit comments