Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
marcelocecin opened this issue Jun 15, 2021 · 1 comment
Labels

Comments

@marcelocecin
Copy link

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 !

@kensoh kensoh added the feature label Jul 4, 2021
@kensoh kensoh changed the title Add auto-screenshot of webpage or screen if element is missing Error handling example, eg auto-screenshot of webpage or screen if element is missing [done] Jul 4, 2021
@kensoh kensoh changed the title Error handling example, eg auto-screenshot of webpage or screen if element is missing [done] Error handling example, eg auto-screenshot of webpage if element is missing [done] Jul 4, 2021
@kensoh
Copy link
Member

kensoh commented Jul 4, 2021

Hi @marcelocecin thanks for sharing your solution! I've added a link from readme to your implementation - https://github.com/tebelorg/RPA-Python#api-reference

I can't merge to master because different users will have different ways to handle the error, to a different API endpoint for eg. However, if having this style of error handling is preferred by many users, then I could explore adding with a dummy function or let the function read from a predefined error-handler Python script.

Below are some possible iterations to the version that you are using. For eg, taking snapshot of webpage only or the entire screen when visual automation is used. Also, replacing spaces in the error input parameter may be a good practice to deal with spaces in the given identifier because filenames with spaces are not ideal on Linux / macOS.

if not _visual():
     snap('page', error+'_error.png')
else:
     snap('page.png', error+'_error.png')

@kensoh kensoh closed this as completed Jul 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants