Skip to content

Error handling example, eg auto-screenshot of webpage if element is missing [done] #269

Closed
@marcelocecin

Description

@marcelocecin

hi Ken
as suggested here: aisingapore/TagUI#939
I also suggest creating this function in this repository.
suggestion to change tagui.py

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
def rpa_error_cannot_find(error):
    snap('page', error+'_error.png')
    url = 'https://webhook.site/53388dfe-1308-45ae-8c88-c31b89c10cbb'
    m = MultipartEncoder( fields={'error': '[RPA][ERROR] - cannot find ' + error, 'image': (error+'_error.png', open(error+'_error.png', 'rb'), 'text/plain')} )
    r = requests.post(url, data=m, headers={'Content-Type': m.content_type})
    print('[RPA][ERROR] - cannot find ' + error)

substitute in all functions that used the print('[RPA][ERROR] - cannot find ' + element_identifier) function for rpa_error_cannot_find(element_identifier), example:

def click(element_identifier = None, test_coordinate = None):
    if not _started():
        print('[RPA][ERROR] - use init() before using click()')
        return False

    if element_identifier is None or element_identifier == '':
        print('[RPA][ERROR] - target missing for click()')
        return False

    if test_coordinate is not None and isinstance(test_coordinate, int):
        element_identifier = coord(element_identifier, test_coordinate)

    if not exist(element_identifier):
        rpa_error_cannot_find(element_identifier)
        return False

    elif not send('click ' + _sdq(element_identifier)):
        return False

    else:
        return True

example sample.py

import tagui as r
r.init(visual_automation = True)
r.url('https://ca.yahoo.com')
r.click('webhook_test')
r.type('ybar-sbq', 'github')
search_text = r.read('ybar-sbq')
print(search_text)
r.click('ybar-search')
r.wait(6.6)
r.snap('page', 'results.png')
r.snap('logo', 'logo.png')
r.url('https://duckduckgo.com')
r.type('search_form_input_homepage', 'The search engine that doesn\'t track you.')
r.snap('page', 'duckduckgo.png')
r.wait(4.4)
r.close()

results console:

[RPA][ERROR] - cannot find webhook_test
github

result received at https://webhook.site

thanks !

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions