Created
June 22, 2016 13:22
-
-
Save absent1706/1722193e10fffbbcb4c259aabf1a97b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
!!! Run tests by just typing 'python <this file>', not throw nosetests/your IDE | |
Before run these tests: | |
1) make sure that PhantomJS executable is in your PATH (type phantomjs in CLI) | |
for custom phantomjs location change PHANTOMJS_PATH | |
2) run app dev server and make sure that BASE_URL is equal to real URL | |
""" | |
import unittest | |
from selenium import webdriver | |
from selenium.common.exceptions import NoSuchElementException | |
PHANTOMJS_PATH = 'phantomjs' | |
BASE_URL = 'http://localhost:8080' | |
class BaseJsTest(unittest.TestCase): | |
url = '' | |
def setUp(self, *args, **kwargs): | |
super(BaseJsTest, self).setUp(*args, **kwargs) | |
self.driver = webdriver.PhantomJS(PHANTOMJS_PATH) | |
def test_no_errors(self, authorized = True): | |
if self.__class__.__name__ != 'BaseJsTest': | |
self.driver.delete_all_cookies() | |
timeout = 2 | |
self.driver.implicitly_wait(timeout) | |
self.driver.get(BASE_URL + self.url) | |
# force to wait 'timeout' seconds or until error indicator appear | |
try: | |
self.driver.find_element_by_id('error-indicator') | |
except NoSuchElementException: | |
pass | |
logs = self.driver.get_log('browser') | |
errors = [log['message'] for log in logs if log['level'] != 'INFO'] | |
errors = ';\n'.join(errors) | |
self.assertEqual(errors, '') | |
def tearDown(self): | |
self.driver.close() | |
class UiHomeJsTest(BaseJsTest): | |
url = '/' | |
class UiSignInJsTest(BaseJsTest): | |
url = '/sign-in' | |
class UiSignUpJsTest(BaseJsTest): | |
url = '/sign-up' | |
if __name__ == '__main__': | |
unittest.main() | |
""" | |
in JS: | |
var _consoleError = console.error; | |
console.error = function (message) { | |
var errorIndicator = document.createElement('div'); | |
errorIndicator.id = 'error-indicator'; | |
document.body.appendChild(errorIndicator); | |
_consoleError.apply(console, arguments); | |
}; | |
window.onerror = function (errorMsg, url, lineNumber) { | |
console.error(errorMsg + ' - '+url+':'+lineNumber) | |
return false; | |
} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment