def test_mozillians_search_full(self, selenium): """ Perform search using keystrokes, and run axe on results page. """ script_url = 'src/axe.min.js' selenium.get('https://mozillians.org') html = selenium.switch_to.active_element elem = html['value'] elem.send_keys(Keys.TAB) elem.send_keys('mbrandt') elem.send_keys(Keys.ENTER) # wait for page load time.sleep(2) axe = Axe(selenium, script_url) response = axe.execute(selenium) # convert array to dictionary violations = dict((k['id'], k) for k in response['violations']) # write result to file axe.write_results('mozillians_search_full.json', violations) # assert response exists assert len(violations) == 0, violations
def axe_core_get_rules(): """Run aXe to get a current list of accessibility rules.""" os.environ["MOZ_HEADLESS"] = "1" driver = webdriver.Firefox() axe = Axe(driver) axe.inject() rules = driver.execute_script("return axe.getRules()") driver.close() return rules
def test_site(self, site): driver = webdriver.Firefox() driver.get(site) axe = Axe(driver) # Inject axe-core javascript into page. axe.inject() # Run axe accessibility checks. results = axe.run() # Write results to file #axe.write_results(results, 'a11y.json') driver.close() return results.get('violations')
def test_execute(self, selenium, base_url, pytestconfig): """Run axe against base_url and verify JSON output.""" script_url = './axe_selenium_python/tests/src/axe.min.js' selenium.get(base_url) axe = Axe(selenium, script_url) response = axe.execute(selenium) pytestconfig.axe = axe pytestconfig.test_results = response # convert array to dictionary pytestconfig.violations = dict( (k['id'], k) for k in response['violations']) # assert response exists assert response is not None, response
def test_mozillians_context(self, selenium): """Run axe against a specific element of mozillians.org.""" script_url = 'src/axe.min.js' selenium.get('https://mozillians.org') axe = Axe(selenium, script_url) response = axe.execute(selenium, '#language') # convert array to dictionary violations = dict((k['id'], k) for k in response['violations']) # write result to file axe.write_results('mozillians_context.json', violations) # assert response exists assert len(violations) == 0, violations
def test_mozillians_full(self, selenium): """Run axe against mozillians.org and assert no violations found.""" script_url = 'src/axe.min.js' selenium.get('https://mozillians.org') axe = Axe(selenium, script_url) response = axe.execute(selenium) # convert array to dictionary violations = dict((k['id'], k) for k in response['violations']) # write result to file axe.write_results('mozillians_full.json', violations) # assert response exists assert len(violations) == 0, violations
def generate_report(self): # Set logging to only warnings or above to cut down on console clutter # https://stackoverflow.com/q/11029717/#answer-11029841 webdriver_logger.setLevel(logging.WARNING) logging.getLogger("urllib3").setLevel(logging.WARNING) # Run headless chrome_options = chrome.options.Options() chrome_options.add_argument("--headless") # Use webdriver_manager to deal with version issues: # https://github.com/formulafolios/ann-arbor/issues/2 driver_manager = ChromeDriverManager().install() # Set up Axe with Chrome driver driver = webdriver.Chrome(driver_manager, chrome_options=chrome_options) driver.get(self.url) axe = Axe(driver) # Inject axe-core javascript into page and run checks. axe.inject() results = axe.run() # Write results to file path = pathjoin(self.report_dir, self.report_file_name("json")) axe.write_results(results, path) driver.close() return path
def run_axe(self): axe = Axe(self.driver) axe.inject() results = axe.run() # If no tags provided then attach all violations to report if not self.scanned_tags(): [ self.full_report.append(violation) for violation in results['violations'] ] # Loop through violations that contain tags provided and attach them to # report else: [ self.full_report.append(violation) for violation in results['violations'] if not set(violation['tags']).isdisjoint(self.scanned_tags()) ] if len(self.full_report) == 0: self.parsed_report.append({ "No a11y violations found for the following tags": list(set(self.scanned_tags())) }) else: self.parse_report(self.full_report) axe.write_results(self.parsed_report, self.file_name(self.link)) self.driver.close()
def accesibility_check(self, url): self.open(url) axe = Axe(self.driver) # Inject axe-core javascript into page. axe.inject() # Run axe accessibility checks. results = axe.run() # Write results to file axe.write_results(results, 'a11y.json') # Assert no violations are found assert len(results["violations"]) == 0, axe.report( results["violations"])
def axe_runner(self, driver, axe_url, destination_folder, browser): # Load AXE driver try: axe = Axe(driver) results = axe results_path = os.path.join(destination_folder, (urllib.parse.quote_plus(axe_url) + '.json')) try: axe.inject() # Inject axe-core javascript into page. results = axe.run() # Run axe accessibility checks. # Write results to file axe.write_results(results, results_path) except Exception as e: msg = e.__str__() + ' AXE RUNNER:01' print(msg) utils.logline(os.path.join(self.log, browser, '_axe_log.txt'), msg) finally: pass driver.close() # Assert no violations are found # assert len(results["violations"]) == 0, axe.report(results["violations"]) # Define CSV csv_row = [] dict_json = ['test', 'browser', 'url', 'score', 'title', 'description'] csv_row.insert(0, dict_json) # Look for violations for violation in results['violations']: dict_json = ['axe', browser, axe_url, '0', violation['help'], violation['description']] csv_row.insert(csv_row.__len__() + 1, dict_json) # Write violations report CSV; create report folder/file; check for exists write_header = False report_path = os.path.join(destination_folder, (browser + '_REPORT.csv')) if not os.path.exists(report_path): write_header = True else: write_header = False # Write AXE_REPORT # os.chdir(os.path.join(destination_folder, 'AXE')) with open(report_path, 'a', encoding='utf8', newline='') as csv_file: for i in range(csv_row.__len__()): writer = csv.writer(csv_file, quoting=csv.QUOTE_ALL) writer.dialect.lineterminator.replace('\n', '') if i == 0: if write_header: writer.writerow(csv_row[i]) write_header = False continue else: writer.writerow(csv_row[i]) msg = datetime.datetime.now().__str__()[:-7] + ' ' + csv_row[i].__str__() print(msg) utils.logline(os.path.join(self.log, '_axe_log.txt'), msg) os.remove(results_path) except Exception as e: msg = e.__str__() + ' AXE RUNNER:02' utils.logline(os.path.join(self.log, browser, '_axe_log.txt'), msg) print(msg)
def test_google(): driver = webdriver.Firefox() driver.get("http://www.google.com") axe = Axe(driver) # Inject axe-core javascript into page. axe.inject() # Run axe accessibility checks. results = axe.run() # Write results to file axe.write_results(results, 'a11y.json') driver.close() # Assert no violations are found assert len(results["violations"]) == 0, axe.report(results["violations"])
def inject_wcag2aa(driver, timestamp, file_name, site): """ This method inject axe js into website and scans it against wcag2aa rule :param driver: webdriver :param timestamp: timestamp of current scan YYYY-MM-DD :param file_name: file name where to save results :param site: url to scan :return: """ options = """ { runOnly: { type: 'tag', values: ['wcag2aa'] } }""" axe = Axe(driver) axe.inject() results = axe.run(context=None, options=options) f = open(f"accessibility_results/{timestamp}_wcag2aa/" + file_name.replace("/", "_"), "w", encoding="utf-8") f.write(site + "\n") f.write(axe.report(results["violations"])) f.close()
def test_contrast(driver: webdriver.Chrome): axe = Axe(driver) # Inject axe-core javascript into page. axe.inject() # Run axe accessibility checks. results = axe.run( options={'runOnly': { 'type': 'rule', 'values': ['color-contrast'] }}) # Write results to file axe.write_results(results, 'a11y.json') # Assert no violations are found logging.info('results:\n%s', json.dumps(results, indent=4, sort_keys=True)) assert len(results["violations"]) == 0, axe.report(results["violations"])
def test_site(url: str): # Add the path the geckodriver you downloaded earlier # The following is an example driver = webdriver.Firefox() driver.get(url) axe = Axe(driver) # Inject axe-core javascript into page. axe.inject() # Run axe accessibility checks. results = axe.run() # Write results to file axe.write_results(results, 'pleasework.json') driver.close() # Assert no violations are found assert len(results["violations"]) == 0, axe.report(results["violations"])
def inject_axe(driver, site, file_name): """Using selenium driver, axe injects its own js to the specific site and writes WCAG validations to file""" axe = Axe(driver) axe.inject() results = axe.run() f = open("results/" + file_name, "w", encoding="utf-8") f.write(site + "\n") f.write(axe.report(results["violations"])) f.close()
def inject_all_rules(driver, timestamp, culture, file_name, site): axe = Axe(driver) axe.inject() results = axe.run() f = open( f"results/{timestamp}_all/{culture}/{file_name.replace('/', '_')}", "w", encoding="utf-8") f.write(site + "\n") f.write(axe.report(results["violations"])) f.close()
def test_accessibility(self): """ Test: Page contains no axe accessibility violations for tags accessibility_test_tags and excluding the rules in accessibility_test_ignore_rules When: Page has AccessibilityMixin """ self.page.launch() axe = Axe(self.driver) axe.inject() results = axe.run(options=self._build_axe_options()) now = datetime.datetime.now() report_name = f"{type(self.page).__name__}-{now.strftime('%Y-%m-%d_%H-%M-%S')}.json" a11y_reports_folder = Path(AUTOREDUCE_HOME_ROOT, "selenium_tests", "a11y_reports") a11y_reports_folder.mkdir(parents=True, exist_ok=True) report_path = str(a11y_reports_folder / report_name) axe.write_results(results, report_path) self.assertEqual(len(results['violations']), 0, axe.report(results["violations"]))
def test_accessibility(self): urls_to_test = self.get_urls() for url in urls_to_test: self.driver.get(url) axe = Axe(self.driver) axe.inject() results = axe.run() import time assert len(results["violations"]) == 0, ( axe.report(results["violations"]), url, )
class test_accessibility(unittest.TestCase): with open('C:/Users/kam/PycharmProjects/Accessibility/URLEntries.json', encoding='utf-8') as data_file: data = json.loads(data_file.read()) driver = webdriver.Chrome() for i in data['urls']: driver.get(i["url"]) axe = Axe(driver) # Inject axe-core javascript into page. axe.inject() # Run axe accessibility checks. results = axe.run() # Write results to file axe.write_results(results, i["file"]) driver.close() if __name__ == "__main__": unittest.main()
def save_accessibility_logs(self, filename=''): """ Perform and save accessibility checks basd on the axe engine. The checks are run automatically for web pages handled by a welkin page object model. Results are written to the current test runs output/accessibility folder. NOTE: these checks may slow down the perceived page performance around page load in the context of test runs. TODO: refine the generated accessibility data: we really just want to report problems. The filename can be specified by the calling code, but the expectation is that this is either: 1. an event string, because the trigger for running these checks could be a page object load or reload 2. the default of the page object's name :param filename: str filename for the log file; defaults to PO name :return: None """ # instantiate axe axe = Axe(self.driver) # inject axe.core into page axe.inject() # run the axe accessibility checks axe_results = axe.run() # axe.run() caused the page to scroll to the bottom; scroll back to top utils_selenium.scroll_to_top_of_page(self.driver) # set the cleaned file name fname = filename if filename else self.name # write the results to a file path = pytest.custom_namespace['testrun_accessibility_log_folder'] filename = f"{path}/{utils.path_proof_name(fname)}.json" logger.info(f"Writing accessibility logs to {filename}") axe.write_results(axe_results, filename)
def accessibility(context, acc_standard): axe = Axe(context.browser) axe.inject() results = axe.run( options={'runOnly': {'type': 'tag', 'values': [acc_standard]}}) current_url = context.browser.current_url try: assert len(results["violations"]) == 0 context.test_case.test_result = 'pass' except AssertionError as ae: context.test_case.test_result = 'fail' violations = str(axe.report(results["violations"])) message = 'Page at url: %s failed accesibility with: %s ' %\ (current_url, violations) BehaveStepHelper.save_accessibility_report(message) raise AssertionError(message)
def test_site(site,folderName): options = webdriver.ChromeOptions() options.add_argument("headless") driver = webdriver.Chrome(executable_path='/Users/fw7424/Documents/chromedriver', options=options) driver.get(site) axe = Axe(driver) axe_options = {"resultTypes": ["violations"]} #'reporter':'no-passes' # Inject axe-core javascript into page. axe.inject() results = axe.run(options=axe_options) # Write results to file page_name = (site.split('.local/',1)[1]).replace('/','_') file_location = ('{}/Accessibility_Audit/{}/{}.json'.format(str(Path.home()), folderName, page_name)) try: os.makedirs('{}/Accessibility_Audit/{}'.format(str(Path.home()), folderName)) except OSError: pass # page_list.append('{}.json'.format(page_name)) axe.write_results(results, file_location) driver.close()
def inject_only_wcag2aa(driver, timestamp, culture, file_name, site): options = """ { runOnly: { type: 'tag', values: ['wcag2aa','wcag2a'] } }""" axe = Axe(driver) axe.inject() results = axe.run(context=None, options=options) f = open( f"results/{timestamp}_wcag2aa/{culture}/{file_name.replace('/', '_')}", "w", encoding="utf-8") f.write(site + "\n") f.write(axe.report(results["violations"])) f.close()
def inject_all(driver, timestamp, file_name, site): """ This method inject axe js into website and scans it against all accessibility tags https://github.com/dequelabs/axe-core/blob/a5ebc936a53bf8a716fed35fe7ec72b2436eab8e/doc/API.md :param driver: webdriver :param timestamp: timestamp of current scan YYYY-MM-DD :param file_name: file name where to save results :param site: url to scan :return: """ axe = Axe(driver) axe.inject() results = axe.run() f = open(f"accessibility_results/{timestamp}_all/" + file_name.replace("/", "_"), "w", encoding="utf-8") f.write(site + "\n") f.write(axe.report(results["violations"])) f.close()
def test_page(page): options = Options() options.headless = True driver = webdriver.Firefox(options=options) driver.get(page[1]) axe = Axe(driver) # Inject axe-core javascript into page. axe.inject() # Run axe accessibility checks. results = axe.run() # Relevant tags tags = ['wcag2aa', 'wcag2aa', 'best-practice'] report =\ [violation for violation in results['violations'] if bool(set(violation['tags']) & set(tags))] # Write results to file axe.write_results(report, axe_path + 'reports/' + page[0] + '.json') driver.close() return True
# log in on singpass main_url = "https://www.google.com" urls = {"https://www.mycareersfuture.sg", "https://www.mycareersfuture.sg/search"} driver.get(main_url) # Thread sleep # time.sleep(10) axe = Axe(driver) full_json = dict() full_set = get_all_links(urls) print(full_set) full_set = remove_invalid(full_set) print(full_set) res = dict.fromkeys(full_set, 0) print(res)
def axe(selenium, base_url, script_url): """Return an Axe instance based on context and options.""" selenium.get(base_url) yield Axe(selenium, script_url)
def save_as_json(final_set, final_json): count_passes = 0 count_incomplete = 0 count_max = 0 violations_array = [] url_array = [] # -------- Python Selenium -------- # for link in final_set: print(link) driver.get(link) axe = Axe(driver) # try options # full_options = { 'xpath : True } # Inject axe-core javascript into page. axe.inject() # Run axe accessibility checks. try: results = axe.run() except: break # driver.get(link) # axe=Axe(driver) # results=axe.run() if (results is None): break url = results['url'] # -------- Python Selenium -------- # # TODO: Can use dict for violations and url array, using array now for simplicity/pyplot violations_array = np.append(violations_array, len(results['violations'])) url_array = np.append(url_array, url) if (len(results['violations']) > count_max): count_max = len(results['violations']) max_url_name = url count_passes += len(results['passes']) count_incomplete += len(results['incomplete']) print(len(results['incomplete'])) # print(type(results)) # print(results.get('violations').count("critical")) # print('violations: ', count_violations) # print('critical violations: ', count_critical) final_json[url] = results print("done") count_array = [count_incomplete, sum(violations_array), count_passes] print('Number of violations: ', sum(violations_array)) return final_json, violations_array, url_array, max_url_name, count_array
t_filename = no_http_target.rstrip('/') else: t_filename = no_http_target.replace("/", "_") if '.' in t_filename: no_dot_filename = t_filename.replace('.', '_') else: no_dot_filename = t_filename if not os.path.exists(foldername): os.makedirs(foldername) target_file = open(f'{foldername}/{no_dot_filename}-a11y.txt', 'w') target_file.write(f'Page tested: {target}') chrome_options = Options() chrome_options.add_argument("--headless") driver = webdriver.Chrome(options=chrome_options) print(target) driver.get(target) axe = Axe(driver) axe.inject() results = axe.run() for result in results["violations"]: target_file.write(f'\n\nDescription:\n{result["description"]}\n') target_file.write(f'Help URL:\n{result["helpUrl"]}\n\n') target_file.write("Messages:\n") for node in result["nodes"]: for n_any in node["any"]: target_file.write(f'{n_any["message"]}\n') driver.close()
def test_sel(self, server): self.driver.get(server.host) axe = Axe(self.driver) axe.inject() results = axe.run() axe.write_results(results, 'a11y.json')