Skip to content

Commit

Permalink
Update image capture
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhsmith committed Feb 21, 2022
1 parent 2d54fce commit b9b5d0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions openassessit/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def capture_screenshot(assets_dir, url, sleep, driver):
im = im.resize([int(0.35 * s) for s in im.size], Image.ANTIALIAS)
# im.save(os.path.join(assets_dir,'screenshot.png'))
# logging.info('Created: screenshot.png')
shot_name = generate_img_filename(url, '_screenshot_')
shot_name = generate_img_filename(url, 'cat', '_screenshot_')
print(shot_name)
im.save(os.path.join(assets_dir,shot_name))
logging.info('Created: ' + shot_name)
Expand All @@ -78,15 +78,15 @@ def capture_screenshot(assets_dir, url, sleep, driver):
logging.debug(ex)


def capture_element_pic(input_file, assets_dir, url, elem_identifier, sleep, driver):
def capture_element_pic(input_file, assets_dir, url, elem_identifier, lhIdee, sleep, driver):
""" Capture image of element and save """
try:
driver.get(url)
driver.set_window_size(1400, driver.execute_script("return document.body.parentNode.scrollHeight"))
elem = driver.find_element_by_css_selector(elem_identifier) # find element
location = elem.location
size = elem.size
elem_image_name = generate_img_filename(url, elem_identifier)
elem_image_name = generate_img_filename(url, elem_identifier, lhIdee)

if (size == {'height': 0.0, 'width': 0.0}):
create_backup_image(assets_dir, elem_identifier, elem_image_name)
Expand All @@ -96,10 +96,10 @@ def capture_element_pic(input_file, assets_dir, url, elem_identifier, sleep, dri
logging.warning('Skipping element because the webdriver could not locate the element to create image.')
else:
im = Image.open(BytesIO(driver.get_screenshot_as_png())) # uses PIL library to open image in memory
im = im.crop((location['x'] -2,
location['y'] -2,
location['x'] + size['width'] +4,
location['y'] + size['height'] +4
im = im.crop((location['x'] -4,
location['y'] -4,
location['x'] + size['width'] +8,
location['y'] + size['height'] +8
))
im.save(os.path.join(assets_dir,elem_image_name)) # saves new cropped image
logging.info('Created: %s' % (elem_image_name))
Expand All @@ -125,7 +125,7 @@ def identifier_generator(data, *auditref_whitelist):
if item['node']['selector'] == ':root':
logging.warning('Selector returned as ":root", no image will be created.') # If Axe returns ":root" it does not create a helpful screenshot
else:
yield item['node']['selector']
yield item['node']['selector'], item['node']['lhId']


def main():
Expand All @@ -148,8 +148,8 @@ def main():
data = json.load(json_file)
detect_full_html_loaded(driver)
capture_screenshot(assets_dir, data['finalUrl'], sleep, driver)
for sel in identifier_generator(data, 'color-contrast', 'link-name', 'button-name', 'image-alt', 'input-image-alt', 'label', 'accesskeys', 'frame-title', 'list', 'listitem', 'definition-list', 'dlitem', 'aria-allowed-attr', 'aria-required-attr', 'aria-required-children', 'aria-required-parent', 'aria-roles', 'aria-valid-attr-value', 'aria-valid-attr'):
capture_element_pic(input_file, assets_dir, data['finalUrl'], sel, sleep, driver)
for sel,lhIdee in identifier_generator(data, 'color-contrast', 'link-name', 'button-name', 'image-alt', 'input-image-alt', 'label', 'accesskeys', 'frame-title', 'list', 'listitem', 'definition-list', 'dlitem', 'aria-allowed-attr', 'aria-required-attr', 'aria-required-children', 'aria-required-parent', 'aria-roles', 'aria-valid-attr-value', 'aria-valid-attr'):
capture_element_pic(input_file, assets_dir, data['finalUrl'], sel, lhIdee, sleep, driver)
finally:
driver.quit()
logging.info('Image creation complete in: "%s"' % (assets_dir))
Expand Down
4 changes: 2 additions & 2 deletions openassessit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def initialize_logger(module, output_dir):
logger.addHandler(handler)


def generate_img_filename(url, identifier):
def generate_img_filename(url, identifier, lhIdee):
""" Generate useful filename with a max of 260 chars """
url = re.sub(r"https?://(www\.)?", '', url)
identifier = re.sub(r'\W+', '-', identifier)
return re.sub(r'\W+', '-', '%s%s' % (url[0:30], identifier[-200:-1])) + '.png'
return re.sub(r'\W+', '-', '%s%s%s' % (url[0:100], identifier[-100:0], lhIdee)) + '.png'


def scroll_down(driver, value):
Expand Down

0 comments on commit b9b5d0f

Please sign in to comment.