Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Improve testability of notifications
Browse files Browse the repository at this point in the history
- wait for a specific notification message
- test multiple notifications in one test case
  • Loading branch information
chadwhitacre committed Jun 14, 2017
1 parent 1985e43 commit b5f4286
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
37 changes: 22 additions & 15 deletions gratipay/testing/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,34 @@ def wait_for(self, selector, timeout=2):
return element
raise NeverShowedUp(selector)

def wait_for_notification(self, type='notice'):
"""Wait for a certain ``type`` of notification. Dismiss the
notification and return the message.
def wait_for_notification(self, type='notice', message=None, timeout=2):
"""Wait for a certain ``type`` of notification, with a certain message
(if specified). Dismiss the notification and return the message.
"""
n_selector = '.notifications-fixed .notification-{}'.format(type)
m_selector = 'span.btn-close'
notification = self.wait_for(n_selector).first
message = notification.find_by_css('div').html
notification.find_by_css(m_selector).first.click()
self.wait_to_disappear(n_selector + ' ' + m_selector)
notification_selector = '.notifications-fixed .notification-{}'.format(type)
close_button_selector = 'span.btn-close'
end_time = time.time() + timeout
while time.time() < end_time:
notification = self.wait_for(notification_selector, timeout).first
candidate = notification.find_by_css('div').html
if message is None or message == candidate:
message = candidate
break
notification.find_by_css(close_button_selector).first.click()
self.wait_to_disappear('#{} {}'.format(notification['id'], close_button_selector))
return message

def wait_for_success(self):
"""Wait for a success notification. Dismiss it and return the message.
def wait_for_success(self, message=None):
"""Wait for a success notification, with a certain message (if
specified). Dismiss it and return the message.
"""
return self.wait_for_notification('success')
return self.wait_for_notification('success', message)

def wait_for_error(self):
"""Wait for an error notification. Dismiss it and return the message.
def wait_for_error(self, message=None):
"""Wait for an error notification, with a certain message (if
specified). Dismiss it and return the message.
"""
return self.wait_for_notification('error')
return self.wait_for_notification('error', message)

def __getattr__(self, name):
try:
Expand Down
8 changes: 5 additions & 3 deletions js/gratipay/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
Gratipay.notification = function(text, type, timeout, closeCallback) {
var type = type || 'notice';
var timeout = timeout || (type == 'error' ? 10000 : 5000);

var dialog = ['div', { 'class': 'notification notification-' + type }, [ 'div', text ]];
var id = Math.random().toString(36).substring(2, 100);
var placeholder = ['div', {'class': 'notification notification-' + type}, ['div', text]];
var dialog = ['div', {'class': 'notification notification-' + type, 'id': 'notification-'+id},
['div', text]];
var $dialog = $([
Gratipay.jsonml(dialog),
Gratipay.jsonml(placeholder),
Gratipay.jsonml(dialog)
]);

Expand Down

0 comments on commit b5f4286

Please sign in to comment.