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 test_mozillians_search_context(self, selenium): """ Perform search using keystrokes, and run axe on single element of page. """ script_url = 'src/axe.min.js' axe = Axe(selenium, script_url) 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(1) axe = Axe(selenium, script_url) response = axe.execute(selenium, '#main > p.alert > a') # convert array to dictionary violations = dict((k['id'], k) for k in response['violations']) # write result to file axe.write_results('mozillians_search_context.json', violations) # assert response exists assert len(violations) == 0, 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 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 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 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 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_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_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 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 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 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 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
print(res) json_save = 'data.json' with open(json_save, 'w') as outfile: links = json.dump(res, outfile) full_json, violations_arr, url_arr, max_url, count_arr = save_as_json( full_json) json_save_path = './data/careers_future.json' axe.write_results(full_json, json_save_path) des_arr = [] for items in full_json.values(): # print(items['violations']) for item in items['violations']: des_arr.append(item['description']) driver.close() driver.quit() time_taken = (time.time() - start_time) plot_visualisations(count_arr, violations_arr, url_arr, des_arr, max_url, json_save_path)
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')