Skip to content

Commit

Permalink
Auto capture screenshot if test failed (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
atthaboon authored Jul 15, 2020
1 parent 751a295 commit 3cf82e3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Examples/log-demo.robot
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ ${HOME_PAGE_URL} http://127.0.0.1:7272
No node found when click
Run Keyword And Expect Error REGEXP:.*No node found for selector: #login_button_error Click Element id:login_button_error

Test log error for sync keywords
Run Keyword And Ignore Error Click Element id:login_button_error

Test log error for async keywords
Run Keyword And Ignore Error Run Async Keywords
... Click Element id:login_button_error AND
... Click Element id:login_button_2

*** Keywords ***
Open browser to test page
${HEADLESS} Get variable value ${HEADLESS} ${False}
Expand Down
25 changes: 24 additions & 1 deletion PuppeteerLibrary/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import asyncio
from robot.api.deco import not_keyword
from robot.api import logger
from pyppeteer.browser import Browser
from robot.libraries.BuiltIn import BuiltIn

from PuppeteerLibrary.custom_elements.SPage import SPage
from robot.api.deco import not_keyword
from PuppeteerLibrary.base.robotlibcore import DynamicCore
from PuppeteerLibrary.keywords import (
AlertKeywords,
Expand Down Expand Up @@ -63,6 +66,8 @@ class PuppeteerLibrary(DynamicCore):
async_libraries = []

def __init__(self):
self.run_on_failure_keyword = 'Capture Page Screenshot'

libraries = [
AlertKeywords(self),
BrowserManagementKeywords(self),
Expand Down Expand Up @@ -104,3 +109,21 @@ def set_current_page(self, page) -> SPage:
@not_keyword
def get_browser(self) -> Browser:
return self.browser

@not_keyword
def run_keyword(self, name, args, kwargs):
self._running_keyword = name
try:
return DynamicCore.run_keyword(self, name, args, kwargs)
except Exception:
self.failure_occurred()
raise
finally:
self._running_keyword = None

def failure_occurred(self):
try:
BuiltIn().run_keyword(self.run_on_failure_keyword)
except Exception as err:
logger.warn("Keyword '%s' could not be run on failure: %s"
% (self.run_on_failure_keyword, err))

0 comments on commit 3cf82e3

Please sign in to comment.